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", message: "INSERT MESSAGE",
date: 5, date: 5,
hours: 15, hours: 15,
apiUrl: "http://localhost:30030",
gatewayToken: "asd" gatewayToken: "asd"
}; };
} }

View File

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

View File

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

View File

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

View File

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

View File

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