Skip to main content
Platforma’s HTTP routing is built on Go’s http.ServeMux with enhanced support for middleware, route grouping, and domain organization.

HTTPServer

The HTTPServer type from httpserver/httpserver.go:18 is a service that runs an HTTP server with graceful shutdown:

Creating an HTTP Server

From httpserver/httpserver.go:25:
The shutdown timeout controls how long the server waits for in-flight requests to complete during graceful shutdown.

Registering with Application

HTTP servers implement the Runner interface and can be registered as services:
The server starts when you run ./myapp run and shuts down gracefully on interrupt signals.

Route Registration

Handle() - Register Handlers

Use the Handle() method from httpserver/handlergroup.go:31 with Go 1.22+ routing syntax:
The pattern "METHOD /path" syntax requires Go 1.22 or later. This gives you method-specific routing without external libraries.

HandleFunc() - Register Functions

From httpserver/handlergroup.go:36, for simple function handlers:

Path Parameters

Extract path parameters using Go’s standard library:

HandlerGroup

The HandlerGroup type from httpserver/handlergroup.go:8 organizes routes that share common middleware:

Creating Handler Groups

From httpserver/handlergroup.go:14:

Mounting Handler Groups

Use HandleGroup() from httpserver/handlergroup.go:41 to mount groups at a path prefix:
HandleGroup() automatically strips the prefix before routing. Routes in the group should be defined without the mount prefix.

Complete Example

From demo-app/cmd/api/main.go:13, showing server setup with groups:
This creates:
  • GET /ping - Top-level endpoint with trace ID middleware
  • GET /long - Top-level endpoint (tests graceful shutdown)
  • GET /subApi/clock - Group endpoint with both trace ID and logging middleware

Mounting Domain APIs

Domains expose their HTTP API via a HandlerGroup:
Mount it in your main application:
This pattern keeps domain routing logic inside the domain while giving you flexibility in how you expose it.

Middleware Application

Server-Level Middleware

Applies to all routes on the server:

Group-Level Middleware

Applies only to routes in the group from httpserver/handlergroup.go:19:

UseFunc() - Inline Middleware

From httpserver/handlergroup.go:24, add middleware as a function:

Middleware Ordering

Middleware is applied in reverse order from httpserver/middleware.go:26:
The first registered middleware is the outermost wrapper. Request flow: trace ID → logging → auth → handler. Response flow is reversed.

Route Organization Patterns

Pattern 1: Versioned APIs

Pattern 2: Public vs Protected

Pattern 3: Domain-Based

Graceful Shutdown

From httpserver/httpserver.go:30, the server handles shutdown automatically:
In-flight requests have shutdownTimeout duration to complete before the server force-closes connections.

Health Checks

The HTTPServer implements Healthchecker from httpserver/httpserver.go:62:
This is automatically included when you register the server:

Advanced Routing

Wildcard Paths

Method-Specific Routing

Multiple Path Params

Next Steps

Middleware

Learn how to create and apply HTTP middleware

Domains

Organize routes by business domain

Application Lifecycle

Register and run HTTP servers