Documentation Index
Fetch the complete documentation index at: https://mintlify.com/platforma-dev/platforma/llms.txt
Use this file to discover all available pages before exploring further.
Quick Start Guide
This guide walks you through creating a fully functional HTTP server with Platforma. You’ll learn the core concepts while building something you can immediately run and expand.This tutorial assumes you’ve already installed Platforma. If not, complete the installation first.
What You’ll Build
A simple HTTP API server with:- Multiple endpoints with different routes
- Handler groups for organizing routes
- Middleware for request logging and trace IDs
- Graceful shutdown handling
- Proper application lifecycle management
Initialize the Application
Create a new Platforma application. This is the core container that manages your services:The
main.go
Application type handles:- Service lifecycle management
- Graceful shutdown coordination
- Health check aggregation
- Startup task execution
Create an HTTP Server
Create an HTTP server with a specific port and shutdown timeout:The parameters are:
main.go
"8080"- The port to listen on3*time.Second- Maximum time to wait for connections to close during shutdown
Add Your First Endpoint
Add a simple This uses Go’s standard
/ping endpoint that responds with “pong”:main.go
http.HandlerFunc signature—Platforma works seamlessly with the standard library.Add Middleware for Request Tracing
Add middleware to inject trace IDs into logs and response headers:Trace IDs help you correlate logs from a single request across your application. They’re automatically generated for each request and included in:
main.go
- Log output (via
log.InfoContext,log.ErrorContext, etc.) - Response headers (as
X-Trace-ID)
NewTraceIDMiddleware accepts:- First parameter: optional trace ID generator function (nil uses default UUID)
- Second parameter: optional custom header name (empty string uses
X-Trace-ID)
Create a Handler Group
Organize related endpoints into a handler group with shared middleware:Now the
main.go
/clock endpoint is accessible at /subApi/clock, and every request to that group logs the remote address.Register the HTTP Server
Register your HTTP server with the application:The first parameter (
main.go
"api") is the service name, used for:- Health check identification
- Log messages
- Service status tracking
Complete Code
Here’s the complete working example:cmd/api/main.go
This example is adapted from the real demo-app included in the Platforma repository at
demo-app/cmd/api/main.goUnderstanding What Happened
Let’s break down the key concepts:Application Lifecycle
TheApplication manages the entire lifecycle:
- Registration: You register services before running
- Startup: When you call
app.Run(), it starts all registered services concurrently - Shutdown: On interrupt signal (Ctrl+C), it gracefully shuts down all services
Service Interface
Any type implementing theRunner interface can be registered as a service:
Graceful Shutdown
When you press Ctrl+C:- The context is canceled
- The HTTP server stops accepting new connections
- Existing connections have up to
shutdownTimeoutto complete - The application exits cleanly
Middleware Pattern
Middleware wraps handlers to add cross-cutting concerns:- Log requests
- Authenticate users
- Add trace IDs
- Recover from panics
- Measure request duration
Next Steps
Now that you have a working server, explore more features:Add a Database
Connect to PostgreSQL and run migrations
Create a Domain
Build a complete domain with repository and service
Add Background Jobs
Process async tasks with the queue processor
Schedule Tasks
Run periodic jobs with the scheduler
Common Patterns
Adding More Endpoints
Extracting Handlers
Using Context Values
Troubleshooting
Port Already in Use
If port 8080 is busy:Missing Command
If you see usage information instead of your server starting, remember to use:Graceful Shutdown Not Working
Increase the shutdown timeout if your requests take longer:Learn More
Dive deeper into domains, repositories, and architecture patterns