docs(httpserver): correct tier from 4 to 3

httpserver depends on launcher (Tier 2), placing it at Tier 3.
With launcher corrected from Tier 5 to Tier 2, httpserver's tier
drops accordingly.
This commit is contained in:
2026-03-19 13:39:19 +00:00
commit 1ec0780f72
15 changed files with 750 additions and 0 deletions

67
README.md Normal file
View File

@@ -0,0 +1,67 @@
# httpserver
Lifecycle-managed HTTP server built on [chi](https://github.com/go-chi/chi) that integrates with the launcher component model.
## Features
- Implements `launcher.Component` (`OnInit` / `OnStart` / `OnStop`)
- Embeds `chi.Router` — full routing API exposed directly on the component
- Graceful shutdown with configurable timeouts
- No middleware installed by default — compose your stack with `WithMiddleware`
## Installation
```
require code.nochebuena.dev/go/httpserver v0.1.0
```
## Usage
```go
srv := httpserver.New(logger, cfg,
httpserver.WithMiddleware(
httpmw.Recover(),
httpmw.CORS([]string{"*"}),
httpmw.RequestID(uuid.NewString),
httpmw.RequestLogger(logger),
),
)
// Register routes directly on srv — it is a chi.Router.
srv.Get("/health", health.NewHandler(logger, db, cache))
srv.Route("/api/v1", func(r chi.Router) {
r.Use(httpauth.AuthMiddleware(firebaseClient, nil))
r.Get("/orders/{id}", httputil.Handle(validator, svc.GetOrder))
})
lc := launcher.New(logger)
lc.Append(db, cache, srv)
if err := lc.Run(); err != nil {
log.Fatal(err)
}
```
## Config
| Field | Env var | Default | Description |
|---|---|---|---|
| `Host` | `SERVER_HOST` | `0.0.0.0` | Bind address |
| `Port` | `SERVER_PORT` | `8080` | Listen port |
| `ReadTimeout` | `SERVER_READ_TIMEOUT` | `5s` | Max time to read request |
| `WriteTimeout` | `SERVER_WRITE_TIMEOUT` | `10s` | Max time to write response |
| `IdleTimeout` | `SERVER_IDLE_TIMEOUT` | `120s` | Keep-alive idle timeout |
## Options
| Option | Description |
|---|---|
| `WithMiddleware(mw ...func(http.Handler) http.Handler)` | Applies middleware to the root router during `OnInit` |
## Lifecycle
| Phase | Action |
|---|---|
| `OnInit` | Applies registered middleware to the router |
| `OnStart` | Starts `net/http.Server` in a background goroutine |
| `OnStop` | Graceful shutdown — waits up to 10s for in-flight requests |