commit b151230e4885540f0f86412a260e17ae474489c5
parent 4a09ac69fddabcaa088765a17ed1b5174c1d41db
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Sun, 20 Oct 2024 23:35:03 +0200
feat(env): use API_URI/PORT for backend
Diffstat:
2 files changed, 32 insertions(+), 34 deletions(-)
diff --git a/dev/env b/dev/env
@@ -1,54 +1,53 @@
-##########
-# Shared #
-##########
-
-# Full URI to backend http API
-VITE_API_URI=http://127.0.0.1
-# Backend API http port
-VITE_API_PORT=8000
-
############
# Frontend #
############
+# Full URI to backend http API
+export VITE_API_URI=http://127.0.0.1
+# Backend API http port
+export VITE_API_PORT=8000
# Application window title
-VITE_APP_DOC_TITLE='MyHeats'
+export VITE_APP_DOC_TITLE='MyHeats'
# Cookie TTL in seconds (session ttl in the admin/judges frontend), 60m
-VITE_SESSION_TTL=3600 #1h
+export VITE_SESSION_TTL=3600 #1h
# Full URI to backend websocket API
-VITE_WS_URI=ws://127.0.0.1
+export VITE_WS_URI=ws://127.0.0.1
# Backend API websocket port
-VITE_WS_PORT=8000
+export VITE_WS_PORT=8000
# Locale for frontend date formatting
-VITE_LOCALE=en-US
+export VITE_LOCALE=en-US
###########
# Backend #
###########
+# API server protocol and bind address
+export API_URI=http://127.0.0.1
+# Bind port for API server
+export API_PORT=8000
# Redirect url for magic links
-API_REDIRECT_URI=http://127.0.0.1:5173/authverify
+export API_REDIRECT_URI=http://127.0.0.1:5173/authverify
# Backend API CORS allowed origins
-API_CORS_ALLOW_ORIGIN=http://127.0.0.1:5173
+export API_CORS_ALLOW_ORIGIN=http://127.0.0.1:5173
# JWT secret for magic links (`openssl rand -base64 48`)
-API_JWT_SECRET=
+export API_JWT_SECRET=
# JWT TTL in seconds, make it biger than VITE_SESSION_TTL, 60m
-API_JWT_TTL=3600
+export API_JWT_TTL=3600
# SMTP settings for sending magic links
-SMTP_HOST=
+export SMTP_HOST=
# upgrade later with STARTTLS
-SMTP_STARTTLS=
-SMTP_PORT=
-SMTP_USER=
-SMTP_FROM=
-SMTP_PASSWORD=
+export SMTP_STARTTLS=
+export SMTP_PORT=
+export SMTP_USER=
+export SMTP_FROM=
+export SMTP_PASSWORD=
# PostgreSQL environment variables for psql client
# https://github.com/porsager/postgres?tab=readme-ov-file#environmental-variables
# PGHOST, PGPORT, PGDATABASE, PGUSERNAME, PGPASSWORD, etc.
-PGHOST=127.0.0.1
-PGPORT=5432
-PGDATABASE=postgres
-PGUSERNAME=postgres
-PGPASSWORD=example
-\ No newline at end of file
+export PGHOST=127.0.0.1
+export PGPORT=5432
+export PGDATABASE=postgres
+export PGUSERNAME=postgres
+export PGPASSWORD=example
diff --git a/src/api/server.cjs b/src/api/server.cjs
@@ -19,8 +19,8 @@ require('dotenv').config({
});
// Read environment and set defaults
-api_uri = process.env.VITE_API_URI;
-api_port = process.env.VITE_API_PORT;
+api_uri = process.env.API_URI;
+api_port = process.env.API_PORT;
redirect_uri = process.env.API_REDIRECT_URI;
cors_allow_origin = process.env.API_CORS_ALLOW_ORIGIN;
jwt_secret = process.env.API_JWT_SECRET;
@@ -95,7 +95,7 @@ const server = http.createServer();
// Listen on request events
server.on('request', async (req, res) => {
- const url = new URL(`${api_uri}:${api_port}${req.url}`);
+ const url = new URL(`${req.protocol}://${req.host}${req.url}`);
const search_params = url.searchParams;
console.log('> Path', url.pathname);
@@ -827,7 +827,7 @@ wss1.on('connection', function connection(sock) {
// Listen to upgrade event
server.on('upgrade', function upgrade(req, sock, head) {
- const url = new URL(`${api_uri}:${api_port}${req.url}`);
+ const url = new URL(`${req.protocol}://${req.host}${req.url}`);
console.log('> WS path', url.pathname);