Skip to content

Commit cac247e

Browse files
authored
feat: use environment variable for server port (#273)
Added environment variable support for server port. Signed-off-by: mephisto <22501790+pfmephisto@users.noreply.github.com>
1 parent 64a2163 commit cac247e

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

backend/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343

4444
if you want to run by `npm run dev`
4545

46+
### Optional: Custom Backend Port
47+
48+
You can set a custom port for the backend server using the `CCSYNC_PORT` environment variable:
49+
50+
```bash
51+
CCSYNC_PORT="8081"
52+
```
53+
54+
Note:
55+
56+
`CCSYNC_PORT` only affects the port of the backend process itself, not the Docker port mapping, and is mainly intended for use outside of a containerized environment to avoid port conflicts.
57+
If you are running the backend via Docker, the exposed ports are determined by the compose configuration. To use a different port in a Docker environment, you must manually update the docker-compose.yml file to adjust the container’s port mapping.
58+
Also, if you change `CCSYNC_PORT`, remember to update `CONTAINER_ORIGIN` accordingly.
59+
4660
- Run the application:
4761

4862
```bash

backend/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func main() {
5858
clientSecret := os.Getenv("CLIENT_SEC")
5959
redirectURL := os.Getenv("REDIRECT_URL_DEV")
6060

61+
// Get port from environment or default to 8000
62+
port := os.Getenv("CCSYNC_PORT")
63+
if port == "" {
64+
port = "8000"
65+
}
66+
6167
// OAuth2 configuration
6268
conf := &oauth2.Config{
6369
ClientID: clientID,
@@ -100,9 +106,9 @@ func main() {
100106
mux.HandleFunc("/api/docs/", httpSwagger.WrapHandler)
101107

102108
go controllers.JobStatusManager()
103-
utils.Logger.Info("Server started at :8000")
104-
utils.Logger.Info("API documentation available at http://localhost:8000/api/docs/index.html")
105-
if err := http.ListenAndServe(":8000", app.EnableCORS(mux)); err != nil {
109+
utils.Logger.Infof("Server started at :%s", port)
110+
utils.Logger.Infof("API documentation available at http://localhost:%s/api/docs/index.html", port)
111+
if err := http.ListenAndServe(":"+port, app.EnableCORS(mux)); err != nil {
106112
utils.Logger.Fatal(err)
107113
}
108114
}

0 commit comments

Comments
 (0)