httpx

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2026 License: MIT Imports: 8 Imported by: 0

README

Modulex httpx HTTP Adapter

This package provides net/http glue for Modulex's health (liveness) and readiness checks, plus a managed *http.Server lifecycle. It depends only on net/http and the core modulex package — no third-party router — so the core modulex package can stay free of HTTP dependencies (the same pattern used by modulex/chi).

Usage

import (
    "net/http"
    "time"

    "github.com/mediusfy/modulex/httpx"
)

mux := http.NewServeMux()
mux.HandleFunc("/healthz", httpx.HealthHandler(manager))
mux.HandleFunc("/readyz", httpx.ReadinessHandler(manager))

server := &http.Server{Addr: ":8080", Handler: mux}
handle, err := httpx.Serve(ctx, manager, "http-server", server, 10*time.Second)
if err != nil {
    return err
}
// handle.Wait() blocks until the server has shut down cleanly or failed.

manager is anything implementing modulex.HealthCheckProvider / modulex.ReadinessProvider (a *modulex.Manager satisfies both) and modulex.TaskSpawner.

Behavior

  • HealthHandler(p modulex.HealthCheckProvider) runs every registered health (liveness) check concurrently, each bounded by the incoming request's deadline or a 5-second default when the request carries none. It responds 200 {"status":"ok","checks":{...}} if every check passes, or 503 {"status":"unhealthy","checks":{...}} if any fail. Every registered check appears in checks, with "ok" for passes and the check's error message for failures.
  • ReadinessHandler(p modulex.ReadinessProvider) behaves identically, sourced from ReadinessChecks() instead, using "ready" / "not-ready" in place of "ok" / "unhealthy".
  • Serve(ctx, spawner, name, server, shutdownTimeout) spawns server.ListenAndServe() as a supervised task via modulex.TaskSpawner.Go, and gracefully calls server.Shutdown with shutdownTimeout when either ctx or the manager's own shutdown fires first. http.ErrServerClosed is treated as a clean exit, not an error — it surfaces as a nil error from the returned *modulex.TaskHandle's Wait().

Why this exists

Every HTTP-serving consumer of Modulex ends up hand-writing the same boilerplate: run health/readiness checks and marshal them to JSON, and spawn ListenAndServe alongside a select on context cancellation that calls Shutdown with a timeout. httpx factors that out once so modules only need to register named check functions and call Serve.

Testing

go test ./httpx/...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthHandler

HealthHandler constructs an http.HandlerFunc for liveness checks.

func ReadinessHandler

func ReadinessHandler(p modulex.ReadinessProvider) http.HandlerFunc

ReadinessHandler constructs an http.HandlerFunc for readiness checks.

func Serve

func Serve(ctx context.Context, spawner modulex.TaskSpawner, name string, server *http.Server,
	shutdownTimeout time.Duration) (*modulex.TaskHandle, error)

Serve starts an HTTP server within a modulex.TaskSpawner and manages graceful shutdown.

Types

This section is empty.

Jump to

Keyboard shortcuts

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