Dev-server proxies /api and WS request to backend.

This commit is contained in:
2020-12-06 23:19:15 +01:00
parent 9265572e45
commit 1839810e66
7 changed files with 131 additions and 149 deletions

View File

@@ -11,7 +11,6 @@ try {
message: "INSERT MESSAGE",
date: 5,
hours: 15,
apiUrl: "http://localhost:30030",
gatewayToken: "asd"
};
}

View File

@@ -5,7 +5,6 @@ module.exports = {
message: "VINLOTTERI",
date: 5,
hours: 15,
apiUrl: undefined,
gatewayToken: undefined,
vinmonopoletToken: undefined,
googleanalytics_trackingId: undefined,

View File

@@ -81,7 +81,6 @@ const webpackConfig = function(isDev) {
__MESSAGE__: JSON.stringify(env.message),
__DATE__: env.date,
__HOURS__: env.hours,
__APIURL__: JSON.stringify(env.apiUrl),
__PUSHENABLED__: JSON.stringify(require("./defaults/push") != false),
__GA_TRACKINGID__: JSON.stringify(env.googleanalytics_trackingId),
__GA_COOKIELIFETIME__: env.googleanalytics_cookieLifetime

View File

@@ -33,11 +33,24 @@ let webpackConfig = merge(commonConfig(true), {
devServer: {
compress: true,
historyApiFallback: true,
host: "0.0.0.0",
hot: true,
overlay: true,
stats: {
normal: true
}
},
proxy: {
"/api": {
target: "http://localhost:30030",
changeOrigin: true
},
"/socket.io": {
target: "ws://localhost:30030",
changeOrigin: false,
ws: true
}
},
writeToDisk: false
}
});

View File

@@ -1,35 +1,29 @@
import fetch from "node-fetch";
const BASE_URL = __APIURL__ || window.location.origin;
const BASE_URL = window.location.origin;
const statistics = () => {
const url = new URL("/api/purchase/statistics", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/purchase/statistics")
.then(resp => resp.json());
};
const colorStatistics = () => {
const url = new URL("/api/purchase/statistics/color", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/purchase/statistics/color")
.then(resp => resp.json());
};
const highscoreStatistics = () => {
const url = new URL("/api/highscore/statistics", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/highscore/statistics")
.then(resp => resp.json());
};
const overallWineStatistics = () => {
const url = new URL("/api/wines/statistics/overall", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/wines/statistics/overall")
.then(resp => resp.json());
};
const allRequestedWines = () => {
const url = new URL("/api/request/all", BASE_URL);
return fetch(url.href)
const allRequestedWines = () => {;
return fetch("/api/request/all")
.then(resp => {
const isAdmin = resp.headers.get("vinlottis-admin") == "true";
return Promise.all([resp.json(), isAdmin]);
@@ -37,26 +31,21 @@ const allRequestedWines = () => {
};
const chartWinsByColor = () => {
const url = new URL("/api/purchase/statistics/color", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/purchase/statistics/color")
.then(resp => resp.json());
};
const chartPurchaseByColor = () => {
const url = new URL("/api/purchase/statistics", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/purchase/statistics")
.then(resp => resp.json());
};
const prelottery = () => {
const url = new URL("/api/wines/prelottery", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/wines/prelottery")
.then(resp => resp.json());
};
const sendLottery = sendObject => {
const url = new URL("/api/lottery", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -65,12 +54,11 @@ const sendLottery = sendObject => {
body: JSON.stringify(sendObject)
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/lottery", options)
.then(resp => resp.json());
};
const sendLotteryWinners = sendObject => {
const url = new URL("/api/lottery/winners", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -79,12 +67,11 @@ const sendLotteryWinners = sendObject => {
body: JSON.stringify(sendObject)
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/lottery/winners", options)
.then(resp => resp.json());
};
const addAttendee = sendObject => {
const url = new URL("/api/virtual/attendee/add", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -93,37 +80,31 @@ const addAttendee = sendObject => {
body: JSON.stringify(sendObject)
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/virtual/attendee/add", options)
.then(resp => resp.json());
};
const getVirtualWinner = () => {
const url = new URL("/api/virtual/winner/draw", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/virtual/winner/draw")
.then(resp => resp.json());
};
const attendeesSecure = () => {
const url = new URL("/api/virtual/attendee/all/secure", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/virtual/attendee/all/secure")
.then(resp => resp.json());
};
const winnersSecure = () => {
const url = new URL("/api/virtual/winner/all/secure", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/virtual/winner/all/secure")
.then(resp => resp.json());
};
const winners = () => {
const url = new URL("/api/virtual/winner/all", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/virtual/winner/all")
.then(resp => resp.json());
};
const deleteRequestedWine = wineToBeDeleted => {
const url = new URL("api/request/"+ wineToBeDeleted.id, BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -132,12 +113,11 @@ const deleteRequestedWine = wineToBeDeleted => {
body: JSON.stringify(wineToBeDeleted)
};
return fetch(url.href, options).then(resp => resp.json())
return fetch("api/request/" + wineToBeDeleted.id, options)
.then(resp => resp.json());
}
const deleteWinners = () => {
const url = new URL("/api/virtual/winner/all", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -145,12 +125,11 @@ const deleteWinners = () => {
method: "DELETE"
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/virtual/winner/all", options)
.then(resp => resp.json());
};
const deleteAttendees = () => {
const url = new URL("/api/virtual/attendee/all", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -158,13 +137,13 @@ const deleteAttendees = () => {
method: "DELETE"
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/virtual/attendee/all", options)
.then(resp => resp.json());
};
const attendees = () => {
const url = new URL("/api/virtual/attendee/all", BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch("/api/virtual/attendee/all")
.then(resp => resp.json());
};
const requestNewWine = (wine) => {
@@ -179,14 +158,11 @@ const requestNewWine = (wine) => {
method: "post"
}
const url = new URL("/api/request/new-wine", BASE_URL)
return fetch(url.href, options).then(resp => resp.json())
return fetch("/api/request/new-wine", options)
.then(resp => resp.json())
}
const logWines = wines => {
const url = new URL("/api/log/wines", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -195,7 +171,8 @@ const logWines = wines => {
body: JSON.stringify(wines)
};
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/log/wines", options)
.then(resp => resp.json());
};
const wineSchema = () => {
@@ -205,31 +182,29 @@ const wineSchema = () => {
};
const barcodeToVinmonopolet = id => {
const url = new URL("/api/wineinfo/" + id, BASE_URL);
return fetch(url.href).then(async resp => {
if (!resp.ok) {
if (resp.status == 404) {
throw await resp.json();
return fetch("/api/wineinfo/")
.then(async resp => {
if (!resp.ok) {
if (resp.status == 404) {
throw await resp.json();
}
} else {
return resp.json();
}
} else {
return resp.json();
}
});
});
};
const searchForWine = searchString => {
const url = new URL("/api/wineinfo/search?query=" + searchString, BASE_URL);
return fetch(url.href).then(async resp => {
if (!resp.ok) {
if (resp.status == 404) {
throw await resp.json();
return fetch("/api/wineinfo/search?query=" + searchString)
.then(async resp => {
if (!resp.ok) {
if (resp.status == 404) {
throw await resp.json();
}
} else {
return resp.json();
}
} else {
return resp.json();
}
});
});
};
@@ -243,7 +218,6 @@ const handleErrors = async resp => {
};
const login = (username, password) => {
const url = new URL("/api/login", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -252,17 +226,17 @@ const login = (username, password) => {
body: JSON.stringify({ username, password })
};
return fetch(url.href, options).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
return fetch("/api/login", options)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
};
const register = (username, password) => {
const url = new URL("/api/register", BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -271,13 +245,14 @@ const register = (username, password) => {
body: JSON.stringify({ username, password })
};
return fetch(url.href, options).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
return fetch("/api/register", options)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
};
const getChatHistory = (page=1, limit=10) => {
@@ -285,25 +260,25 @@ const getChatHistory = (page=1, limit=10) => {
if (!isNaN(page)) url.searchParams.append("page", page);
if (!isNaN(limit)) url.searchParams.append("limit", limit);
return fetch(url.href).then(resp => resp.json());
return fetch(url.href)
.then(resp => resp.json());
};
const finishedDraw = () => {
const url = new URL("/api/virtual/finish", BASE_URL);
const options = {
method: 'POST'
}
return fetch(url.href, options).then(resp => resp.json());
return fetch("/api/virtual/finish", options)
.then(resp => resp.json());
};
const getAmIWinner = id => {
const url = new URL(`/api/winner/${id}`, BASE_URL);
return fetch(url.href).then(resp => resp.json());
return fetch(`/api/winner/${id}`)
.then(resp => resp.json());
};
const postWineChosen = (id, wineName) => {
const url = new URL(`/api/winner/${id}`, BASE_URL);
const options = {
headers: {
"Content-Type": "application/json"
@@ -312,50 +287,49 @@ const postWineChosen = (id, wineName) => {
body: JSON.stringify({ wineName: wineName })
};
return fetch(url.href, options).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
return fetch(`/api/winner/${id}`, options)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
};
const historyAll = () => {
const url = new URL(`/api/lottery/all`, BASE_URL);
return fetch(url.href).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
return fetch(`/api/lottery/all`)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
}
const historyByDate = (date) => {
const url = new URL(`/api/lottery/by-date/${ date }`, BASE_URL);
return fetch(url.href).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
return fetch(`/api/lottery/by-date/${ date }`)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
});
}
const getWinnerByName = (name) => {
const encodedName = encodeURIComponent(name)
const url = new URL(`/api/lottery/by-name/${name}`, BASE_URL);
return fetch(url.href).then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
})
return fetch(`/api/lottery/by-name/${name}`)
.then(resp => {
if (resp.ok) {
return resp.json();
} else {
return handleErrors(resp);
}
})
}
export {

View File

@@ -74,8 +74,7 @@ export default {
this.track();
this.getAttendees();
this.getWinners();
const BASE_URL = __APIURL__ || window.location.origin;
this.socket = io(`${BASE_URL}`);
this.socket = io(window.location.origin);
this.socket.on("color_winner", msg => {});
this.socket.on("disconnect", msg => {

View File

@@ -94,8 +94,7 @@ export default {
this.socket = null;
},
mounted() {
const BASE_URL = __APIURL__ || window.location.origin;
this.socket = io(`${BASE_URL}`);
this.socket = io(window.location.origin);
this.socket.on("chat", msg => {
this.chatHistory.push(msg);
});