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.
68 lines
1.9 KiB
Markdown
68 lines
1.9 KiB
Markdown
# 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 |
|