Home > Software design >  Setting host and port when executing a .jar
Setting host and port when executing a .jar

Time:01-05

I have an Angular Spring Boot app that's deployed as a single .jar file, some .properties files and a bash script to execute the .jar depending on what properties we want to use.

Right now, my .ts classes have host and port set statically, e.g.:

const PATH: string = 'http://localhost:8080/(...)';

Is there a way to set them dynamically while executing the .jar?

CodePudding user response:

There are many ways to do it

  1. environmental variables
  2. application arguments CommandLineRunner
  3. configuration file

CodePudding user response:

The question is very broad and could not be answered definitely with such minimal information. I want to point some things though which you must consider in what ever solution you apply to this problem.

The backend which is a spring boot app, may as well run in localhost:8080 and that is fine. It is never however the case that an application backend is in the same machine as the Frontend. You have adopted angular as your frontend which means by default (if you haven't overridden this behavior) some static files would be served and executed in the browser of the end user.

So this in most cases means that the end user must know the ip-address or domain so that he can reach the machine that runs the backend server.

So the end situation you will have is backend running in for example localhost:8080 in some VM and frontend using a property like const PATH: string = 'http://my-backend-domain.com:8080/(...)'; so that an external user can reach the backend server from his browser.

In a very simplified scenario an effective solution would be to adapt your angular app with this answer then during deployment decide what is the domain for example where you are deploying the app like /my-backend-domain.com and go to assets/data/config.json and configure it there. This would not require any additional build of Frontend just a simple configuration.

  • Related