跳转至正文

HTTP Server ​

Introduction ​

PrismGo starts its HTTP server with go run . serve. During startup, the application runs bootstrap.NewApplication(), loads providers, routes, middleware, and exception handling, then builds a net/http.Server.

New projects register routes in bootstrap/app.go:

go
foundation.Configure(basePath...).
    WithRouting(func(r *foundation.Routing) {
        r.Routes(apphttp.RegisterRoutes)
    }).
    Create()

Starting the Server ​

bash
go run . serve

Specify a port:

bash
go run . serve --port=8000

--port only affects the current command. It does not edit .env or config/app.go.

Listen Address ​

The default config lives under app.server in config/app.go:

go
"server": map[string]interface{}{
    "host": Env("SERVER_HOST", ""),
    "port": Env("SERVER_PORT", 8080),
}

The matching .env values are:

dotenv
SERVER_HOST=127.0.0.1
SERVER_PORT=8080
Config PathEnvironment VariableDefaultDescription
app.server.hostSERVER_HOST""Listen host. An empty value lets Go listen on all addresses
app.server.portSERVER_PORT8080Listen port

Timeouts and Request Limits ​

Config PathEnvironment VariableDefaultDescription
app.server.timeoutSERVER_TIMEOUT15Legacy request timeout used as a fallback
app.server.read_timeoutSERVER_READ_TIMEOUT15sTimeout for reading the full request
app.server.read_header_timeoutSERVER_READ_HEADER_TIMEOUT5sTimeout for reading request headers
app.server.write_timeoutSERVER_WRITE_TIMEOUT30sResponse write timeout
app.server.idle_timeoutSERVER_IDLE_TIMEOUT60sKeep-alive idle timeout
app.server.shutdown_timeoutSERVER_SHUTDOWN_TIMEOUT15sGraceful shutdown timeout
app.server.max_header_bytesSERVER_MAX_HEADER_BYTES1048576Maximum request header bytes
app.server.max_multipart_memorySERVER_MAX_MULTIPART_MEMORY33554432Multipart form memory limit

Duration fields accept Go duration strings such as 15s, 1m, and 1m30s. Plain numbers are treated as seconds.

Proxies and Client IPs ​

Config PathEnvironment VariableDefaultDescription
app.server.trusted_proxiesSERVER_TRUSTED_PROXIES""Comma-separated trusted proxy list
app.server.client_ip_headersSERVER_CLIENT_IP_HEADERSX-Forwarded-For,X-Real-IPHeaders used to resolve the client IP

When production traffic passes through a reverse proxy, set trusted proxies explicitly so application code does not trust spoofed client IP headers.

Middleware Toggles ​

Config PathEnvironment VariableDefaultDescription
app.server.access_logSERVER_ACCESS_LOGtrueEnable Gin access logging
app.server.exception_handlerSERVER_EXCEPTION_HANDLERtrueMount the unified HTTP exception handler middleware

app.debug / APP_DEBUG controls whether exception responses expose internal details. It must be false in production.

Process Control ​

serve keeps a port-specific PID file. Control commands read the PID for the selected port:

CommandBehavior
go run . serve --stopGracefully stop the server on the current port
go run . serve --killForce stop the server on the current port
go run . serve --reloadStart a new process and gracefully stop the old process
go run . serve --restartKill the old process and start a new process

If the server was not started with serve, or if the PID file is missing, control commands report that the PID file cannot be read.