Skip to content

Commit

Permalink
Making API port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
avishek-sen-gupta committed Nov 4, 2024
1 parent ed4b3b6 commit 7b67dd5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ fileignoreconfig:
checksum: 9a1890534ecbc051bc3866d7cec20611f9a9b800eb0f189c270c683d521aa3bd
- filename: README.md
checksum: 6da2c89d18f9096c1547cae26762d8a9ec2d808e80a58c411e3362a69c0779d6
- filename: README.md
checksum: e3e0192e7f8a73434e40b2820d41a5ee15b9f5290e033f492bf49446a19ae56c
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<db_user>" DATABASE_PASSWORD="<db-password>" java -jar smojol-api/target/smojol-api.jar
PORT=<port> DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="<db_user>" DATABASE_PASSWORD="<db-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:<port>```, thus bypassing CORS restrictions.

### Deploy for production

Expand All @@ -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="<db_user>" DATABASE_PASSWORD="<db-password>" java -jar smojol-api/target/smojol-api.jar
PORT=<port> DATABASE_URL=jdbc:sqlite:/path/to/db/file DATABASE_USER="<db_user>" DATABASE_PASSWORD="<db-password>" java -jar smojol-api/target/smojol-api.jar
```

## Test it out!

Hit ```localhost:7070```, and you should see the app.
Hit ```localhost:<port>```, and you should see the app.


The rest of this file is mostly technical notes for my personal documentation.
Expand Down
7 changes: 6 additions & 1 deletion smojol-api/src/main/java/org/smojol/api/ApiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7b67dd5

Please sign in to comment.