Make server port configurable through environment variables - #2
Conversation
Issue
The server port number specified in the .env file was not being respected, as the port was hardcoded to 8080 in multiple places. This made it impossible to run multiple instances on different ports or customize the port through configuration.
Changes
main.go: Read port from environment variable with fallback to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Default port if not specified
}
Dockerfile: Make port configurable via environment variable
ENV PORT=8080
EXPOSE ${PORT}
docker-compose.yml: Use environment variable for port mapping
ports:
- "${PORT:-8080}:${PORT:-8080}"
environment:
- PORT=${PORT:-8080}
Testing Instructions
Without .env file (default behavior):
make run
# Server starts on port 8080
With custom port in .env:
echo "PORT=8081" >> .env
make run
# Server starts on port 8081
With Docker Compose:
PORT=8082 docker-compose up
# Server starts on port 8082
Additional Notes
If no port is specified, falls back to 8080 for backward compatibility
Configuration cascades through all infrastructure (app, Docker, Docker Compose)
Docker configuration uses the same environment variable, ensuring consistency
WalkthroughThis pull request updates the application's port configuration across multiple files. The Dockerfile now defines an environment variable ( Changes
Sequence Diagram(s)sequenceDiagram
participant DC as Docker Compose
participant DF as Dockerfile
participant APP as Application (main.go)
DC->>DF: Build with env var PORT (default 8080)
DF->>APP: Set ENV PORT in container
APP->>APP: Retrieve PORT from environment variables
APP->>APP: Start server listening on the determined port
Poem
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 golangci-lint (1.62.2)Error: can't load config: the Go language version (go1.23) used to build golangci-lint is lower than the targeted Go version (1.24) Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
main.go (1)
13-17: Swagger documentation still references hardcoded portThe Swagger documentation still specifies
localhost:8080as the host, which could be misleading if a different port is used.Consider updating the Swagger host annotation to reflect the dynamic port configuration:
// @title Web Server API // @version 1.0 // @description A REST API server written in Go using gin-gonic framework. -// @host localhost:8080 +// @host localhost:${PORT} // @BasePath /api/v1Or better yet, make it a runtime configuration in your Swagger initialization code.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
Dockerfile(1 hunks)docker-compose.yml(1 hunks)main.go(2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
main.go (1)
internal/infrastructure/server/server.go (1) (1)
NewServer(42:112)
🔇 Additional comments (7)
Dockerfile (2)
38-39: Good addition of configurable port environment variableSetting a default port as an environment variable provides flexibility for configuration. This aligns well with the PR objectives to make the server port configurable.
42-42: Good update to use dynamic port exposureChanging from a hardcoded port to the environment variable ensures that the exposed port matches the configured one. This allows for dynamic port configuration at container build or runtime.
main.go (3)
4-6: Appropriate import additionAdding the
ospackage is necessary for environment variable access.
19-22: Good implementation of environment variable-based port configurationThis implementation correctly reads the port from the PORT environment variable with a sensible fallback to "8080" if not specified.
25-25: Proper server start with dynamic portThe server now correctly starts on the dynamically configured port.
docker-compose.yml (2)
9-9: Good implementation of dynamic port mappingChanging from a static port mapping to a dynamic one that respects the PORT environment variable allows for flexible port configuration. Using the
${PORT:-8080}syntax provides a sensible default while allowing customization.
11-11: Appropriate environment variable injectionAdding the PORT environment variable to the container ensures that the application running inside can access the configured port value. This is consistent with the changes in main.go that read this environment variable.
Docstrings generation was requested by @ibhanu. * #2 (comment) The following files were modified: * `main.go`
|
Note Generated docstrings for this pull request at #3 |
Issue
The server port number specified in the .env file was not being respected, as the port was hardcoded to 8080 in multiple places. This made it impossible to run multiple instances on different ports or customize the port through configuration.
Fixed #1
Changes
main.go: Read port from environment variable with fallback to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Default port if not specified
}
Dockerfile: Make port configurable via environment variable
ENV PORT=8080
EXPOSE ${PORT}
docker-compose.yml: Use environment variable for port mapping
ports:
environment:
Testing Instructions
Without .env file (default behavior):
make run
Server starts on port 8080
With custom port in .env:
echo "PORT=8081" >> .env
make run
Server starts on port 8081
With Docker Compose:
PORT=8082 docker-compose up
Server starts on port 8082
Additional Notes
If no port is specified, falls back to 8080 for backward compatibility
Configuration cascades through all infrastructure (app, Docker, Docker Compose)
Docker configuration uses the same environment variable, ensuring consistency
Summary by CodeRabbit