Skip to main content

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
1

Create Your Main File

Create a new file cmd/api/main.go:
Add the basic imports and structure:
main.go
2

Initialize the Application

Create a new Platforma application. This is the core container that manages your services:
main.go
The Application type handles:
  • Service lifecycle management
  • Graceful shutdown coordination
  • Health check aggregation
  • Startup task execution
3

Create an HTTP Server

Create an HTTP server with a specific port and shutdown timeout:
main.go
The parameters are:
  • "8080" - The port to listen on
  • 3*time.Second - Maximum time to wait for connections to close during shutdown
The shutdown timeout ensures graceful handling of in-flight requests. Adjust it based on your longest request duration.
4

Add Your First Endpoint

Add a simple /ping endpoint that responds with “pong”:
main.go
This uses Go’s standard http.HandlerFunc signature—Platforma works seamlessly with the standard library.
5

Add Middleware for Request Tracing

Add middleware to inject trace IDs into logs and response headers:
main.go
Trace IDs help you correlate logs from a single request across your application. They’re automatically generated for each request and included in:
  • 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)
6

Create a Handler Group

Organize related endpoints into a handler group with shared middleware:
main.go
Now the /clock endpoint is accessible at /subApi/clock, and every request to that group logs the remote address.
Handler groups are perfect for:
  • API versioning (e.g., /v1, /v2)
  • Feature areas (e.g., /admin, /public)
  • Applying middleware to related routes
7

Register the HTTP Server

Register your HTTP server with the application:
main.go
The first parameter ("api") is the service name, used for:
  • Health check identification
  • Log messages
  • Service status tracking
8

Run the Application

Start the application with error handling:
main.go
Platforma applications use a command-based CLI. When running your app, you must specify the run command:
Not just:
9

Test Your Server

Run your application:
You should see log output like:
Test your endpoints:
You’ll see request logs for the /subApi/clock endpoint because of the logging middleware we added to that handler group.

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.go

Understanding What Happened

Let’s break down the key concepts:

Application Lifecycle

The Application manages the entire lifecycle:
  1. Registration: You register services before running
  2. Startup: When you call app.Run(), it starts all registered services concurrently
  3. Shutdown: On interrupt signal (Ctrl+C), it gracefully shuts down all services

Service Interface

Any type implementing the Runner interface can be registered as a service:
The HTTP server, queue processors, and schedulers all implement this interface.

Graceful Shutdown

When you press Ctrl+C:
  1. The context is canceled
  2. The HTTP server stops accepting new connections
  3. Existing connections have up to shutdownTimeout to complete
  4. The application exits cleanly

Middleware Pattern

Middleware wraps handlers to add cross-cutting concerns:
Middleware can:
  • 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