Skip to content

About

Immutable HTTP server library for GALA — builder pattern, 35+ composable filters (auth, CORS, rate limiting, circuit breaker), sealed type enums, zero-reflection JSON, SSE, graceful shutdown. Inspired by Finagle + Echo.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

47 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GALA Server

A fast, immutable HTTP server library for the GALA language. Inspired by Twitter Finagle's filter model, Scala's immutable builders, and Go Echo's ergonomics -- with resilience features that go well beyond any Go framework.

Features

  • Immutable builder pattern -- every method returns a new Server (no mutation, no races)
  • Sealed types -- Method and StatusCode are exhaustive, pattern-matchable enums
  • 35+ built-in filters -- logging, auth (Bearer, Basic, JWT, API key), CORS, CSRF, rate limiting, gzip, proxy, security headers, circuit breaker, retry, bulkhead, ETag, caching, and more
  • Filter algebra -- compose, conditionally apply, or skip filters with ComposeFilters, WhenFilter, Skip
  • Type-safe extractors -- PathParam, QueryRequired, HeaderRequired, BodyAs[T] returning Try[T]
  • TLS/HTTPS -- ServeHTTPTLS, ServeHTTPGracefulTLS with cert/key
  • Server-Sent Events -- SSE() and SSEStream() for real-time push
  • Resilience -- CircuitBreaker (3-state), RetryFilter / RetryFilterWithBackoff, Bulkhead concurrency limiter
  • Prometheus metrics -- MetricsFilter tracks per-route request count, latency, status codes; WithMetricsEndpoint exposes /metrics
  • Session management -- cookie-based in-memory sessions with SessionFilter, SessionGet/Set/Delete, configurable expiry
  • Sliding window rate limiter -- RateLimitSlidingWindow and RateLimitSlidingWindowPerIP for accurate per-window throttling
  • Content negotiation -- Accepts, AcceptsJSON, Negotiate for multi-format APIs
  • Health & readiness -- WithHealthCheck, WithReadiness for container orchestration
  • Error mapping -- WithErrorMapper converts domain errors to HTTP responses
  • Zero-reflection JSON -- codec-based serialization via JsonFrom[T]
  • Base path -- WithBasePath("/api/v1") prefixes all routes
  • Graceful shutdown -- ServeHTTPGraceful() / ServeTCP() with configurable timeout
  • Warmup -- WithWarmup runs initialization before accepting traffic
  • Go bridge -- thin httpcore layer (~500 lines) is the only Go code

Quick Start

package main

import (
    . "github.com/martianoff/gala-server"
    "time"
)

func main() {
    // Prometheus metrics collector
    val stats = NewMetrics()

    // Cookie-based session store
    val sessions = NewSessions("my-secret-key").
        WithCookieName("sid").
        WithMaxAge(3600)

    // The protocol: routes, filters, everything HTTP-shaped.
    val app = NewHTTP().
        WithBasePath("/api").
        WithHealthCheck("/health").
        WithReadiness("/ready", () => true).
        WithMetricsEndpoint("/metrics", stats).
        // Routes
        GET("/", (req) => Ok("Hello, GALA!")).
        GET("/users/{id}", (req) => {
            val id = req.Param("id").GetOrElse("0")
            return JsonResponse(s"{\"id\": \"$id\", \"name\": \"User $id\"}")
        }).
        GET("/profile", (req) => {
            val name = req.SessionGet("username").GetOrElse("Guest")
            return Ok(s"Hello, $name!")
        }).
        POST("/login", (req) => {
            req.SessionSet("username", "Alice")
            return Ok("Logged in")
        }).
        // Global filters
        WithFilter(Logger()).
        WithFilter(Recovery()).
        WithFilter(Cors()).
        WithFilter(MetricsFilter(stats)).
        WithFilter(SessionFilter(sessions)).
        WithFilter(RateLimitSlidingWindowPerIP(100, 1 * time.Minute)).
        WithFilter(CircuitBreaker(maxFailures = 5, resetTimeout = 30 * time.Second))

    // The host: identity, port, lifecycle.
    NewServer().
        WithName("My API").
        WithPort(8080).
        ServeHTTPGraceful(app)
}

TLS / HTTPS

server.ServeHTTPTLS(app, "cert.pem", "key.pem")
server.ServeHTTPGracefulTLS(app, "cert.pem", "key.pem")

Type-Safe Extractors

Extractors return Try[T] for clean error handling via pattern matching:

func getUser(req Request) Response =
    PathParamInt(req, "id") match {
        case Success(id) => Ok(s"User $id")
        case Failure(err) => BadRequest(s"Invalid ID: $err")
    }

Available extractors: PathParam, PathParamInt, QueryRequired, QueryInt, HeaderRequired, BodyAs[T].

Server-Sent Events

server.GET("/events", (req) => {
    val events = ArrayOf(
        SSEEvent(Event = "message", Data = "Hello!", Id = "1", Retry = 0),
        SSEEvent(Event = "update", Data = "{\"count\": 42}", Id = "2", Retry = 0),
    )
    return SSE(events)
})

Resilience Filters

// Circuit breaker: opens after 5 failures, resets after 30s
server.WithFilter(CircuitBreaker(maxFailures = 5, resetTimeout = 30 * time.Second))

// Retry with exponential backoff
server.WithFilter(RetryFilterWithBackoff(maxRetries = 3, initialBackoff = 100 * time.Millisecond))

// Bulkhead: limit concurrent requests
server.WithFilter(Bulkhead(maxConcurrent = 100))

Filter Algebra

Compose and conditionally apply filters:

// Compose two filters into one
val secured = ComposeFilters(Auth(), RateLimit(100.0, 10.0))

// Apply filter only when predicate is true
val authWhenNotHealth = WhenFilter((req) => req.Path() != "/health", Auth())

// Skip filter for specific paths
val logExceptHealth = Skip(Logger(), "/health", "/ready")

Prometheus Metrics

val stats = NewMetrics()

val app = NewHTTP().
    WithMetricsEndpoint("/metrics", stats).
    GET("/users", userHandler).
    WithFilter(MetricsFilter(stats))

// GET /metrics returns Prometheus text format:
//   gala_requests_total 42
//   gala_responses_total{status="200"} 38
//   gala_route_requests_total{route="/users"} 15
//   gala_route_latency_avg_ms{route="/users"} 2.34
//   gala_uptime_seconds 3600

Session Management

val sessions = NewSessions("secret-key").
    WithCookieName("sid").
    WithMaxAge(3600).
    WithSecure(true)

server.
    WithFilter(SessionFilter(sessions)).
    GET("/profile", (req) => {
        val name = req.SessionGet("username").GetOrElse("Guest")
        return Ok(s"Hello, $name!")
    }).
    POST("/login", (req) => {
        req.SessionSet("username", "Alice")
        return Ok("Logged in")
    }).
    POST("/logout", (req) => {
        req.SessionDelete("username")
        return Ok("Logged out")
    })

// Periodic cleanup of expired sessions
sessions.Cleanup(1 * time.Hour)

Sliding Window Rate Limiter

// Global: 1000 requests per minute
server.WithFilter(RateLimitSlidingWindow(1000, 1 * time.Minute))

// Per-IP: 100 requests per minute per client
server.WithFilter(RateLimitSlidingWindowPerIP(100, 1 * time.Minute))

Content Negotiation

func handler(req Request) Response =
    Negotiate(req,
        () => JsonResponse("{\"msg\": \"hello\"}"),
        () => Html("<p>hello</p>"),
        () => Text("hello"),
    )

Health & Readiness

server.
    WithHealthCheck("/health").
    WithReadiness("/ready", () => dbPool.IsConnected())

TCP — protocols that are not HTTP

A Server is protocol-agnostic — it owns identity, port and lifecycle. Hand it an HTTP config and it speaks HTTP; call ServeTCP and it speaks whatever you write. Redis's RESP, memcached, SMTP and line-oriented control ports all need a byte stream rather than a router.

The protocol is the only thing that changes. Port, name, banner, warmup and graceful shutdown are configured the same way either way.

import . "github.com/martianoff/gala-server"

// Handlers are top-level functions taking the Conn, one per connection.
func handle(c Conn) {
    var running = true
    for running {
        c.ReadLine(8192) match {
            case None()       => { running = false }          // peer closed
            case Some(line)   => {
                if line == "QUIT" {
                    c.Write("BYE\r\n")
                    running = false
                } else {
                    c.Write(line + "\r\n")
                }
            }
        }
        // Flush only when nothing further is buffered — this is what makes
        // pipelined requests one syscall instead of one per reply.
        if !running || c.Buffered() == 0 {
            c.Flush()
        }
    }
    c.Close()
}

func main() {
    NewServer().
        WithName("echo").
        WithPort(7070).
        WithShutdownTimeout(10 * time.Second).
        ServeTCP(handle)
}

No Go type appears in the API: no []byte, no net.Conn, no (value, error) pairs — just string, int, Option and Try. ReadExactly(n, skip) reads a length-prefixed binary payload safely (bounded, so a lying length prefix cannot exhaust memory, and delimiter-free, so a payload containing \r\n survives).

On SIGINT/SIGTERM it stops accepting, lets in-flight connections finish, and force-closes whatever remains once ShutdownTimeout expires.

HTTP and TCP coexist in one binary as two ports — ServeHTTP for health and metrics, ServeTCP for the protocol itself.

See docs/tcp.md for the full API, the concurrency story, and why payloads are string rather than []byte. A runnable line protocol lives in examples/tcp-echo.

Prerequisites

  • GALA compiler (v0.25.3+)
  • Go SDK 1.25+ on PATH
  • Bazel (via Bazelisk)
  • GALA toolchain cloned as a sibling directory: git clone https://github.com/martianoff/gala.git ../gala_simple

Build & Test

# Build library
bazel build //:gala-server

# Run all tests (7 test targets, 330+ test functions)
bazel test //:server_test //:filter_test //:integration_test //:extractor_test //:sse_test //:metrics_test //:session_test //:readme_test

# Run a specific test target
bazel test //:server_test

# Build everything including examples
bazel build //...

Note: MODULE.bazel uses local_path_override pointing to ../gala_simple. Clone the GALA repo there, or update the path.

Running the Example

The examples/hello/ directory contains a full-featured demo with 20 routes, 8 filters, route groups, auth, SSE, content negotiation, health checks, and more.

# Build and run
bazel run //examples/hello

The server starts on http://localhost:8080 with base path /api. Test with:

# Basic routes
curl http://localhost:8080/api/
curl http://localhost:8080/api/health
curl http://localhost:8080/api/ready

# Search with query params
curl "http://localhost:8080/api/public/search?q=gala&page=2"

# Server-Sent Events
curl http://localhost:8080/api/public/sse

# Content negotiation
curl -H "Accept: application/json" http://localhost:8080/api/public/negotiate
curl -H "Accept: text/html" http://localhost:8080/api/public/negotiate

# Auth required (401 without header)
curl http://localhost:8080/api/v1/users
curl -H "Authorization: Bearer any-token" http://localhost:8080/api/v1/users
curl -H "Authorization: Bearer any-token" http://localhost:8080/api/v1/users/42

# Basic auth
curl -u admin:password http://localhost:8080/api/basic

# API key auth
curl -H "X-API-Key: my-secret-key" http://localhost:8080/api/api-key

# Admin (bearer token)
curl -H "Authorization: Bearer admin-secret" http://localhost:8080/api/admin/stats

See examples/hello/README.md for the full list of test commands.

Live TUI Dashboard

The tui/ subpackage wraps a server with a request-capturing filter and renders a live Elm-style dashboard. Header shows live req/s, p95 latency, total request count, and uptime. Two sparklines track throughput (req/s) and p95 latency (ms) over the last 60 seconds. A sortable DataTable shows per-route stats — top 10 routes by hits with average latency and error percentage. A 2xx/3xx/4xx/5xx BarChart histogram tracks status-code distribution. The log panel tails every request's method, path, status, and latency. Press q or Ctrl+C to quit.

Prerequisite: clone the gala-tui repository as a sibling of gala-server:

# Both repos must be siblings — gala-server's MODULE.bazel uses local_path_override
git clone https://github.com/martianoff/gala-tui.git ../gala-tui

Run the dashboard:

bazel run //examples/dashboard

The example wires four routes (/, /hello/{name}, /slow, /error) to a server on :8080 and hands it to tui.RunWithDashboard(server).

Drive traffic with the bundled load generator in another terminal:

bazel run //examples/loadgen
# Or with custom pacing / duration:
bazel run //examples/loadgen -- -url http://localhost:8080 -min-ms 20 -max-ms 200 -duration 30s

loadgen picks routes weighted-random from the dashboard example's set, so the sparkline and log tail light up immediately. Flags: -url, -min-ms / -max-ms (random delay range), -duration (0 = until interrupted), -quiet, -seed. Ctrl+C stops it and prints a summary of 2xx/3xx/4xx/5xx counts.

Use it in your own server:

import (
    . "github.com/martianoff/gala-server"
    . "martianoff/gala/time_utils"
    "github.com/martianoff/gala-server/tui"
)

func main() {
    val app = NewHTTP().
        GET("/", (req) => Ok("hi")).
        WithFilter(Recovery())

    tui.RunWithDashboard(NewServer().WithPort(8080), app)
}

Project Structure

gala-server/
  types.gala          # Core types: Method, StatusCode, HTTPError, ErrorMapper, etc.
  request.gala        # Request API (params, query, headers, body, context, content negotiation)
  response.gala       # Response constructors and builders (including Negotiate)
  filter.gala         # All built-in filters (35+) including resilience and filter algebra
  metrics.gala        # Prometheus metrics collector and MetricsFilter
  session.gala        # Cookie-based session management (SessionFilter, SessionGet/Set/Delete)
  extractor.gala      # Type-safe extractors (PathParam, QueryRequired, BodyAs, etc.)
  sse.gala            # Server-Sent Events (SSEEvent, SSE, SSEStream)
  group.gala          # Route groups with scoped filters
  server.gala         # Server builder, TLS, health/readiness, warmup, error mapper
  router.gala         # Route type and registration
  httpcore/            # Go bridge (~500 lines) — the only Go code
    httpcore.go        # BridgeRequest, BridgeResponse, ServerBuilder, CircuitBreaker, Semaphore
    testing.go         # TestRequestBuilder for unit tests
  tui/                 # Optional live TUI dashboard powered by gala_tui
    bus.go             # Thread-safe event bus from server goroutines to TUI (Go)
    dashboard.gala     # Elm-style model/update/view + RunWithDashboard entry
  server_test.gala     # Server, response, request unit tests
  filter_test.gala     # Filter behavior tests
  integration_test.gala # HTTP-level integration tests
  examples/
    hello/main.gala    # Full-feature demo (20 routes, 8 filters, auth, SSE, ...)
    dashboard/main.gala # Live TUI dashboard demo (4 routes + tui.RunWithDashboard)
    loadgen/main.go    # Random-frequency request generator for driving the dashboard
  docs/                # Documentation

Architecture

+---------------------------------------------+
|              GALA Server Layer               |
|  types, request, response, filter, group,   |
|  server, router, extractor, sse  (all .gala) |
+---------------------------------------------+
|           httpcore Bridge (Go)               |
|  BridgeRequest, BridgeResponse,             |
|  ServerBuilder, CircuitBreaker, Semaphore,   |
|  TokenBucket, HMACSigner, ETag              |
+---------------------------------------------+
|              Go net/http                     |
+---------------------------------------------+

The GALA layer is purely functional and immutable. The httpcore bridge is a thin Go adapter that translates between net/http and GALA's type system. All HTTP types (Request, Response, Method, StatusCode) are GALA-native sealed types.

Documentation

  • Getting Started -- installation, core concepts, first server
  • API Reference -- complete reference for all types, responses, request API, filters
  • Filters -- filter system: three scopes, execution order, built-in filters, filter algebra, resilience
  • Route Groups -- grouping routes with shared prefixes and scoped filters
  • Transpiler Notes -- known transpiler issues and workarounds for gala-server development

Test Coverage

330+ tests across 7 test targets — all passing via bazel test:

  • Server builder: NewServer, WithPort, WithName, WithDebug, WithBanner, immutability, Any method
  • Response constructors: all 16 status codes, content types (JSON, HTML, XML, Text), JSONP, blobs, redirects, streams
  • Response builder: WithHeader, WithBody, WithStatus, cookies (set, secure, delete), immutability
  • Sealed types: Method (7 variants), StatusCode (13 variants)
  • Request API: params, query params, headers, body, form, cookies, context, host, scheme, TLS, RealIP
  • Filters: Logger, Recovery, CORS, Auth, AuthBearer, BasicAuth, KeyAuth, RateLimit, RateLimitPerIP, Timeout, Gzip, RequestId, Secure, BodyLimit, CSRF, HTTPSRedirect, WWWRedirect, NonWWWRedirect, TrailingSlash, RemoveTrailingSlash, MethodOverride, BodyDump, Decompress, Rewrite, JWTAuth, Proxy
  • Groups: nesting, composition, filter scoping, filter isolation
  • HTTPError: structured errors, JSON responses
  • Route naming: URL generation with parameter substitution
  • Metrics: MetricsFilter recording, Prometheus exposition endpoint, concurrent counter safety
  • Sessions: SessionFilter lifecycle, SessionGet/Set/Delete, cleanup, cookie configuration
  • Sliding window: RateLimitSlidingWindow and per-IP variant, window expiry, burst accuracy
  • JWT: token creation, validation, expiry, claims, signature verification
  • Integration: full HTTP request/response cycle with real server

About

Immutable HTTP server library for GALA — builder pattern, 35+ composable filters (auth, CORS, rate limiting, circuit breaker), sealed type enums, zero-reflection JSON, SSE, graceful shutdown. Inspired by Finagle + Echo.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages