From 7b67dd5312930e3a24e6ed198b79b3390138ba68 Mon Sep 17 00:00:00 2001 From: avishek-sen-gupta Date: Mon, 4 Nov 2024 22:06:35 +0530 Subject: [PATCH] Making API port configurable --- .talismanrc | 2 ++ README.md | 16 ++++++++-------- .../src/main/java/org/smojol/api/ApiMain.java | 7 ++++++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/.talismanrc b/.talismanrc index fe0899bc..621dc4cc 100644 --- a/.talismanrc +++ b/.talismanrc @@ -71,3 +71,5 @@ fileignoreconfig: checksum: 9a1890534ecbc051bc3866d7cec20611f9a9b800eb0f189c270c683d521aa3bd - filename: README.md checksum: 6da2c89d18f9096c1547cae26762d8a9ec2d808e80a58c411e3362a69c0779d6 +- filename: README.md + checksum: e3e0192e7f8a73434e40b2820d41a5ee15b9f5290e033f492bf49446a19ae56c diff --git a/README.md b/README.md index c75b82f1..dc746302 100644 --- a/README.md +++ b/README.md @@ -1081,17 +1081,17 @@ Depending upon if you are developing the app or packaging it for production, you ### Deploy app for development -To run the app locally in development mode, go to ```smojol-app/cobol-lekt``` and run ```npm run serve```. This should start the app server (8080 by default). +Make sure that the app is already built as described in [How to Build](#how-to-build). -Then, run ```mvn clean verify -Dmaven.test.skip``` as described in [How to Build](#how-to-build). +To run the app locally in development mode, go to ```smojol-app/cobol-lekt``` and run ```npm run serve```. This should start the development server (8080 by default). -Finally, start the API server (starts on port 7070): +Finally, start the API server (starts on port 7070, if ```PORT``` is not specified): ``` -DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="" DATABASE_PASSWORD="" java -jar smojol-api/target/smojol-api.jar +PORT= DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="" DATABASE_PASSWORD="" java -jar smojol-api/target/smojol-api.jar ``` -The development server proxies calls to ```api/*``` to ```localhost:7070```, thus bypassing CORS restrictions. +The development server proxies calls to ```api/*``` to ```localhost:```, thus bypassing CORS restrictions. ### Deploy for production @@ -1101,15 +1101,15 @@ If you are deploying the UI to be served by the API itself, run: scripts/build-all.sh ``` -Start the API server (starts on port 7070): +Start the API server (starts on port 7070, if ```PORT``` is not specified): ``` -DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="" DATABASE_PASSWORD="" java -jar smojol-api/target/smojol-api.jar +PORT= DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="" DATABASE_PASSWORD="" java -jar smojol-api/target/smojol-api.jar ``` ## Test it out! -Hit ```localhost:7070```, and you should see the app. +Hit ```localhost:```, and you should see the app. The rest of this file is mostly technical notes for my personal documentation. diff --git a/smojol-api/src/main/java/org/smojol/api/ApiMain.java b/smojol-api/src/main/java/org/smojol/api/ApiMain.java index 50009b54..18981574 100644 --- a/smojol-api/src/main/java/org/smojol/api/ApiMain.java +++ b/smojol-api/src/main/java/org/smojol/api/ApiMain.java @@ -13,11 +13,16 @@ import static org.smojol.api.database.DbContext.fromSystemEnv; public class ApiMain { + public static final int DEFAULT_PORT = 7070; + public static void main(String[] args) throws IOException, URISyntaxException, SQLException { Gson gson = new GsonBuilder().create(); JsonMapper gsonMapper = getJsonMapper(gson); DbContext dbContext = fromSystemEnv(); - new ApiServer().runServer(7070, gsonMapper, gson, dbContext); + String portString = System.getenv("PORT"); + + int port = portString == null ? DEFAULT_PORT : Integer.parseInt(portString); + new ApiServer().runServer(port, gsonMapper, gson, dbContext); } private static JsonMapper getJsonMapper(Gson gson) {