Files
golib/httpserver
nabbar fa8adbe7c8 Package Socket:
- config Server: change time duration to golib duration to simplify
  marshal string form
- adjust test following update of config server
- fix test in socket package to use BDD framework & gherkin form
- adjust documentation & test

Package HTTPServer:
- Fix bug in PortUse & PortNotUse
- Move function PortUse & PortNotUse as alone function
- Add test & documentation
- Unify test & documentation following other packages
2025-12-23 16:27:47 +01:00
..
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00
2025-12-23 16:27:47 +01:00

HTTP Server Package

License Go Version Coverage

Production-grade HTTP server management with lifecycle control, TLS support, pool orchestration, and integrated monitoring.


Table of Contents


Overview

The httpserver package provides comprehensive HTTP/HTTPS server management for Go applications with emphasis on production readiness, lifecycle control, and multi-server orchestration through a unified pool interface.

Why Use httpserver?

Standard Go's http.Server provides basic HTTP serving but lacks production-ready abstractions:

Limitations of http.Server:

  • No lifecycle management: Manual start/stop coordination required
  • No configuration validation: Runtime errors from misconfiguration
  • No multi-server orchestration: Managing multiple servers is manual
  • No monitoring integration: Health checks and metrics require custom code
  • Static handler: Handler changes require server restart
  • Complex TLS setup: Certificate management is low-level

How httpserver Extends http.Server:

  • Complete lifecycle API: Start, Stop, Restart with context-aware operations
  • Configuration validation: Pre-flight checks with detailed error reporting
  • Pool management: Unified operations across multiple server instances
  • Built-in monitoring: Health checks and metrics collection ready
  • Dynamic handlers: Hot-swap handlers without restart
  • Integrated TLS: Certificate management with optional/mandatory modes

Internally, httpserver wraps http.Server while adding lifecycle management, configuration validation, and pool orchestration capabilities for production deployments.

Design Philosophy

  1. Lifecycle First: Complete control over server start, stop, and restart operations with proper cleanup.
  2. Configuration-Driven: Declarative configuration with validation before server creation.
  3. Thread-Safe: Atomic operations and mutex protection for concurrent access.
  4. Production-Ready: Monitoring, logging, graceful shutdown, and error handling built-in.
  5. Composable: Pool management for coordinating multiple server instances with filtering.
  6. Zero-Panic: Defensive programming with safe defaults and error propagation.

Key Features

  • Lifecycle Control: Start, stop, restart servers with context-aware operations
  • Configuration Validation: Built-in validation with detailed error reporting
  • TLS/HTTPS Support: Integrated certificate management with optional/mandatory modes
  • Pool Management: Coordinate multiple servers with unified operations
  • Handler Management: Dynamic handler registration with key-based routing
  • Monitoring Integration: Built-in health checks and metrics collection
  • Thread-Safe Operations: Atomic values and mutex protection
  • Port Conflict Detection: Automatic port availability checking
  • Extensive Testing: 65.0% coverage with race detection and 246 test specs

Architecture

Component Diagram

┌────────────────────────────────────┐
│         Application Layer          │
│   (Your HTTP Handlers & Routes)    │
└──────────────────┬─────────────────┘
                   │
         ┌─────────▼───────┐
         │   httpserver    │
         │   Package API   │
         └────────┬────────┘
                  │
    ┌─────────────┼─────────────┐
    │             │             │
┌───▼───┐    ┌────▼────┐    ┌───▼────┐
│Server │    │  Pool   │    │ Types  │
│       │    │         │    │        │
│Config │◄───┤ Manager │    │Handler │
│Run    │    │ Filter  │    │Fields  │
│Monitor│    │ Clone   │    │Const   │
└───┬───┘    └────┬────┘    └────────┘
    │             │
    └──────┬──────┘
           │
    ┌──────▼──────┐
    │  Go stdlib  │
    │ http.Server │
    └─────────────┘

Package Structure

httpserver/
├── httpserver           # Core server implementation
│   ├── config.go        # Configuration and validation
│   ├── server.go        # Server lifecycle management
│   ├── run.go           # Start/stop execution logic
│   ├── handler.go       # Handler registration
│   ├── monitor.go       # Monitoring integration
│   └── interface.go     # Public interfaces
├── pool/                # Multi-server orchestration
│   ├── interface.go     # Pool interfaces
│   ├── server.go        # Pool operations
│   ├── list.go          # Filtering and listing
│   └── config.go        # Pool configuration
└── types/               # Shared type definitions
    ├── handler.go       # Handler types
    ├── fields.go        # Field type constants
    └── const.go         # Package constants

Thread Safety

Component Mechanism Concurrency Model
Server State atomic.Value Lock-free reads, atomic writes
Pool Map sync.RWMutex Multiple readers, exclusive writers
Handler Registry atomic.Value Lock-free handler swapping
Logger atomic.Value Thread-safe logging
Runner atomic.Value + sync.WaitGroup Lifecycle synchronization

Performance

Server Operations

Operation Time Memory Notes
Config Validation ~100ns O(1) Field validation only
Server Creation <1ms ~5KB Includes initialization
Start Server 1-5ms ~10KB Port binding overhead
Stop Server <5s O(1) Graceful shutdown timeout
Pool Operations O(n) ~1KB/server Linear scaling

Throughput

  • HTTP Requests: Limited by Go's http.Server (typically 50k+ req/s)
  • HTTPS/TLS: ~20-30k req/s depending on cipher suite
  • Pool Management: Negligible overhead (<1% per server)

Scalability

  • Single Server: ~10-15KB baseline + handler memory
  • Pool with 10 Servers: ~150KB baseline
  • Scale: Linear growth with server count
  • Concurrency: Thread-safe for concurrent operations

Use Cases

1. Microservices Architecture

Run multiple API versions simultaneously with isolated configuration.

pool := pool.New(context.Background(), nil)
pool.ServerStore("api-v1", serverV1)
pool.ServerStore("api-v2", serverV2)
pool.ServerStore("admin", adminServer)
pool.Start() // Start all servers

2. Multi-Tenant Systems

Dedicated server per tenant with different TLS certificates and configurations.

for _, tenant := range tenants {
    cfg := httpserver.Config{
        Name:   tenant.Name,
        Listen: tenant.BindAddr,
        TLS:    tenant.Certificate,
    }
    srv, _ := httpserver.New(cfg, tenant.Logger)
    pool.ServerStore(tenant.ID, srv)
}

3. Development & Testing

Start/stop servers dynamically in integration tests.

srv, _ := httpserver.New(testConfig, nil)
srv.Start(ctx)
defer srv.Stop(ctx)

// Run tests against http://localhost:port

Quick Start

Installation

go get github.com/nabbar/golib/httpserver

Single Server

package main

import (
    "context"
    "net/http"
    "github.com/nabbar/golib/httpserver"
)

func main() {
    cfg := httpserver.Config{
        Name:   "api-server",
        Listen: "127.0.0.1:8080",
        Expose: "http://localhost:8080",
    }
    
    cfg.RegisterHandlerFunc(func() map[string]http.Handler {
        return map[string]http.Handler{
            "": http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                w.Write([]byte("Hello World"))
            }),
        }
    })
    
    srv, _ := httpserver.New(cfg, nil)
    defer srv.Stop(context.Background())
    
    srv.Start(context.Background())
}

TLS Server

cfg := httpserver.Config{
    Name:   "secure-api",
    Listen: "127.0.0.1:8443",
    Expose: "https://localhost:8443",
    TLS:    tlsConfig, // libtls.Config
}

srv, _ := httpserver.New(cfg, nil)
srv.Start(ctx)

Server Pool

pool := pool.New(ctx, logger)

// Add multiple servers
pool.ServerStore("api", apiServer)
pool.ServerStore("metrics", metricsServer)
pool.ServerStore("admin", adminServer)

// Start all servers
pool.Start()

// Filter and operate
apiServers := pool.FilterServer(FieldName, "api", nil, nil)
apiServers.Stop()

Handler Management

// Dynamic handler registration
cfg.RegisterHandlerFunc(func() map[string]http.Handler {
    return map[string]http.Handler{
        "api-v1": apiV1Handler,
        "api-v2": apiV2Handler,
    }
})

// Use specific handler key
cfg.HandlerKey = "api-v2"

Best Practices

Testing

The package includes a comprehensive test suite with 65.0% code coverage and 246 test specifications using BDD methodology (Ginkgo v2 + Gomega).

Key test coverage:

  • Configuration validation and cloning
  • Server lifecycle (start, stop, restart)
  • Handler management and execution
  • Pool operations with filtering
  • TLS configuration and validation
  • Concurrent access with race detector (zero races detected)

For detailed test documentation, see TESTING.md.

DO

Use Configuration Validation:

// ✅ GOOD: Validate before creation
cfg := httpserver.Config{...}
if err := cfg.Validate(); err != nil {
    log.Fatal(err)
}
srv, _ := httpserver.New(cfg, nil)

Graceful Shutdown:

// ✅ GOOD: Use context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
srv.Stop(ctx)

Pool Management:

// ✅ GOOD: Use pool for multiple servers
pool := pool.New(ctx, logger)
pool.ServerStore("srv1", srv1)
pool.ServerStore("srv2", srv2)
pool.Start() // Starts all servers

DON'T

Don't skip validation:

// ❌ BAD: No validation
srv, _ := httpserver.New(invalidConfig, nil)
srv.Start(ctx) // May fail at runtime

// ✅ GOOD: Validate first
if err := cfg.Validate(); err != nil {
    return err
}

Don't block indefinitely:

// ❌ BAD: No timeout
srv.Stop(context.Background())

// ✅ GOOD: Use timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
srv.Stop(ctx)

Don't ignore errors:

// ❌ BAD: Ignore errors
srv.Start(ctx)

// ✅ GOOD: Handle errors
if err := srv.Start(ctx); err != nil {
    log.Printf("Failed to start: %v", err)
}

API Reference

Server Interface

type Server interface {
    // Lifecycle
    Start(ctx context.Context) error
    Stop(ctx context.Context) error
    Restart(ctx context.Context) error
    IsRunning() bool
    
    // Configuration
    GetConfig() Config
    SetConfig(cfg Config) error
    Merge(src Server) error
    
    // Info
    GetName() string
    GetBindable() string
    GetExpose() *url.URL
    IsDisable() bool
    IsTLS() bool
    
    // Handler
    Handler(fct FuncHandler)
    
    // Monitoring
    MonitorName() string
}

Pool Interface

type Pool interface {
    // Server management
    ServerStore(name string, srv Server)
    ServerLoad(name string) Server
    ServerDelete(name string) bool
    ServerWalk(fct func(name string, srv Server) bool)
    ServerList() map[string]Server
    
    // Operations
    Start() []error
    Stop() []error
    Restart() []error
    IsRunning() bool
    
    // Filtering
    FilterServer(field FieldType, value string, 
                 exclude, disable []string) Pool
}

Configuration

type Config struct {
    Name         string        // Server name (required)
    Listen       string        // Listen address (required)
    Expose       string        // Expose URL (required)
    HandlerKey   string        // Handler map key
    Disabled     bool          // Disable flag
    TLSMandatory bool          // TLS mandatory
    TLS          libtls.Config // TLS configuration
    OptionServer optServer     // Server options
    OptionLogger optLogger     // Logger options
}

Error Codes

var (
    ErrorParamEmpty       = 1300 // Empty parameter
    ErrorConfigInvalid    = 1301 // Invalid configuration
    ErrorServerStart      = 1304 // Server start failure
    ErrorServerInvalid    = 1305 // Invalid server instance
    ErrorAddressInvalid   = 1306 // Invalid address
    ErrorServerPortInUse  = 1307 // Port already in use
)

Contributing

Contributions are welcome! Please follow these guidelines:

  1. Code Quality

    • Follow Go best practices and idioms
    • Maintain or improve code coverage (target: >65%)
    • Pass all tests including race detector
    • Use gofmt and golint
  2. AI Usage Policy

    • AI must NEVER be used to generate package code or core functionality
    • AI assistance is limited to:
      • Testing (writing and improving tests)
      • Debugging (troubleshooting and bug resolution)
      • Documentation (comments, README, TESTING.md)
    • All AI-assisted work must be reviewed and validated by humans
  3. Testing

    • Add tests for new features
    • Use Ginkgo v2 / Gomega for test framework
    • Ensure zero race conditions with go test -race
    • Update TESTING.md with new test IDs
  4. Documentation

    • Update GoDoc comments for public APIs
    • Add examples for new features
    • Update README.md and TESTING.md if needed
  5. Pull Request Process

    • Fork the repository
    • Create a feature branch
    • Write clear commit messages
    • Ensure all tests pass
    • Update documentation
    • Submit PR with description of changes

Improvements & Security

Current Status

The package is production-ready with no urgent improvements or security vulnerabilities identified.

Code Quality Metrics

  • 65.0% test coverage (target: >80%)
  • Zero race conditions detected with -race flag
  • Thread-safe implementation using atomic operations
  • Memory-safe with proper resource cleanup
  • 246 test specifications covering all major use cases

Future Enhancements (Non-urgent)

The following enhancements could be considered for future versions:

  1. HTTP/3 Support: Add QUIC protocol support for HTTP/3
  2. Automatic Certificate Rotation: Hot-reload TLS certificates without restart
  3. Advanced Metrics: Prometheus metrics export built-in
  4. Request Tracing: Distributed tracing integration (OpenTelemetry)
  5. Rate Limiting: Built-in rate limiting per server/pool

These are optional improvements and not required for production use. The current implementation is stable and performant.


Resources

Package Documentation

  • GoDoc - Complete API reference with function signatures, method descriptions, and runnable examples. Essential for understanding the public interface and usage patterns.

  • doc.go - In-depth package documentation including design philosophy, architecture explanation, lifecycle management, and implementation details. Provides detailed explanations of internal mechanisms and best practices for production use.

  • TESTING.md - Comprehensive test suite documentation covering test architecture, BDD methodology with Ginkgo v2, 65.0% coverage analysis, and guidelines for writing new tests. Includes troubleshooting and CI integration examples.

External References

  • http.Server - Go standard library's HTTP server. The httpserver package wraps http.Server with lifecycle management and configuration validation.

  • Effective Go - Official Go programming guide covering best practices for interfaces, error handling, and concurrency patterns. The httpserver package follows these conventions.

  • Go Concurrency Patterns - Official Go blog article explaining concurrency patterns. Relevant for understanding thread-safe server pool management.


AI Transparency

In compliance with EU AI Act Article 50.4: AI assistance was used for testing, documentation, and bug resolution under human supervision. All core functionality is human-designed and validated.


License

MIT License - See LICENSE file for details.

Copyright (c) 2025 Nicolas JUHEL


Maintained by: Nicolas JUHEL Package: github.com/nabbar/golib/httpserver Version: See releases for versioning