server

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package server provides a production-ready Gin HTTP server for portsmith applications.

Creating a server

srv := server.New(server.Config{
    Port: 8080,
    Mode: "release",  // or "debug"
})

Registering routes

v1 := srv.Router().Group("/api/v1")
userHandler.Routes(v1)
orderHandler.Routes(v1)

Starting the server

if err := srv.Run(); err != nil {
    log.Fatal(err)
}

Built-in endpoints

GET /health  →  200 {"status":"ok"}

Middleware (applied automatically)

  • Recovery: catches panics → 500 with JSON body
  • RequestID: generates X-Request-ID header if not present
  • CORS: permissive defaults (restrict AllowOrigins in production)
  • Errors: converts apperrors.AppError → HTTP status + JSON body

Binding and validation

Use BindAndValidate in handlers instead of c.ShouldBindJSON:

var req CreateUserRequest
if err := server.BindAndValidate(c, &req); err != nil {
    return  // 400 already written
}

Error handling

Attach errors via c.Error() — the middleware handles the response:

user, err := svc.GetByID(ctx, id)
if err != nil {
    _ = c.Error(err)  // apperrors.NotFound → 404
    return
}

Package server provides a production-ready Gin HTTP server with:

  • Recovery middleware (panic → 500)
  • Request ID middleware (generates X-Request-ID per request)
  • CORS middleware
  • Error handling middleware (converts apperrors → HTTP status + JSON body)
  • /health endpoint out of the box
  • BindAndValidate helper for JSON body parsing + validation

Usage:

srv := server.New(server.Config{Port: 8080})

v1 := srv.Router().Group("/api/v1")
userHandler.Routes(v1)

if err := srv.Run(); err != nil {
    log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindAndValidate

func BindAndValidate(c *gin.Context, req any) error

BindAndValidate binds the JSON request body and runs validator tag checks. On failure it writes a 400 response and returns the error. The handler must return immediately when err != nil.

var req CreateUserRequest
if err := server.BindAndValidate(c, &req); err != nil {
    return
}

Types

type Config

type Config struct {
	// Port is the TCP port to listen on. 0 = OS-assigned (useful in tests).
	Port int

	// Mode sets the Gin mode: "debug", "release", "test". Defaults to "release".
	Mode string
}

Config holds server configuration.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server wraps a Gin engine with portsmith defaults.

func New

func New(cfg Config) *Server

New creates a new Server with all default middleware applied.

func (*Server) Router

func (s *Server) Router() *gin.Engine

Router returns the underlying *gin.Engine for route registration.

func (*Server) Run

func (s *Server) Run() error

Run starts the HTTP server. Blocks until the server exits.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL