Quick Reference
Minimal config to get started on Linux amd64:
Zero values are not always the runtime values you will see in logs. For example, WorkerCount and Listeners both default to 0 in config and are derived later by the Linux epoll runtime.
s := core.New(core.Config{Addr: ":8080", PlainHTTP: true}) s := core.New(core.Config{ Addr: ":443", TLSCertFile: "cert.pem", TLSKeyFile: "key.pem", }) s := core.New(core.Config{ Addr: ":443", HTTPAddr: ":80", ACME: &core.ACMEConfig{ Email: "you@example.com", Domains: []string{"example.com"}, }, })
Network
| Field | Type | Default | Description |
|---|---|---|---|
| Addr | string | ":8443" | Listen address (e.g. :443, 0.0.0.0:8080) |
| HTTPAddr | string | "" (auto :80 when Addr is :443) | HTTP listen address for ACME challenges & HTTPS redirect |
| ServerName | string | "ALOS" | Value for the Server response header |
| ProxyMode | bool | false | Enable when running behind a reverse proxy / load balancer. The server trusts X-Forwarded-For / X-Real-IP from all peers and rewrites the client IP for every request (rate limiting, logging, and handlers all see the real client). Leave it off when internet-facing, or clients can spoof their IP. Overrides TrustedProxies while on. |
| TrustedProxies | []string | nil | IP ranges/addresses whose X-Forwarded-For / X-Real-IP headers are trusted, e.g. "10.0.0.0/8". Use this instead of ProxyMode when only specific upstream proxies should be trusted. Ignored when ProxyMode is on. |
TLS & Certificates
| Field | Type | Default | Description |
|---|---|---|---|
| TLSCertFile | string | "" | Path to PEM certificate file |
| TLSKeyFile | string | "" | Path to PEM private key file |
| Certs | []CertConfig | nil | Multi-domain certs (Domain, CertFile, KeyFile per entry) |
| DefaultDomain | string | "" | Fallback cert domain for unknown SNI |
| ACME | *ACMEConfig | nil | ACME / Let's Encrypt auto-certificate config |
CertConfig
| Field | Type | Description |
|---|---|---|
| Domain | string | SNI hostname to match |
| CertFile | string | Path to PEM certificate |
| KeyFile | string | Path to PEM private key |
ACMEConfig
| Field | Type | Default | Description |
|---|---|---|---|
| string | "" | ACME account email | |
| Domains | []string | nil | Domains to obtain certificates for |
| CacheDir | string | "/etc/letsencrypt" | Directory to cache certificates |
| ACMENode | string | "" | Proxy ACME challenges to this address (e.g. "10.0.0.5:80") |
Timeouts
| Field | Type | Default | Description |
|---|---|---|---|
| ReadTimeout | time.Duration | 0 (none) | Max time to read entire request (0 = no timeout) |
| WriteTimeout | time.Duration | 0 (none) | Max time to write entire response (0 = no timeout) |
| IdleTimeout | time.Duration | 120s | Keep-alive idle connection timeout |
| HandshakeTimeout | time.Duration | 30s | Max time for TLS handshake |
| ShutdownTimeout | time.Duration | 30s | Graceful shutdown deadline |
Size Limits
| Field | Type | Default | Description |
|---|---|---|---|
| MaxBodySize | int64 | 0 (unlimited) | Maximum request body size (0 = unlimited) |
| MaxReadSize | int64 | 0 | Max read size per request (0 = unlimited) |
| MaxWriteSize | int64 | 0 | Max write size per response (0 = unlimited) |
| MaxHeaderSize | int | 8 KB | Maximum total header size |
Bandwidth
Bandwidth limits use the BandwidthConfig struct with separate upload/download rates and burst size:
| Field | Type | Default | Description |
|---|---|---|---|
| ConnBandwidth | BandwidthConfig | {} | Per-connection bandwidth limits |
| GlobalBandwidth | BandwidthConfig | {} | Server-wide bandwidth limits |
BandwidthConfig
| Field | Type | Description |
|---|---|---|
| MaxUploadRate | int64 | Max upload speed in Mbps (0 = unlimited) |
| MaxDownloadRate | int64 | Max download speed in Mbps (0 = unlimited) |
| BurstSize | int64 | Burst allowance in Mbps before throttling |
s := core.New(core.Config{ Addr: ":443", ConnBandwidth: core.BandwidthConfig{ MaxDownloadRate: 40, BurstSize: 16, }, GlobalBandwidth: core.BandwidthConfig{ MaxDownloadRate: 4000, BurstSize: 800, }, })
Compression
| Field | Type | Default | Description |
|---|---|---|---|
| EnableCompress | bool | false | Enable gzip/deflate response compression |
| CompressLevel | int | 6 | Compression level (1 to 9, higher = smaller) |
| CompressMinSize | int | 256 | Minimum response body size to compress |
You can also enable compression via the Compress() middleware instead of config, which gives more control over the minimum size threshold.
Connection Limits
| Field | Type | Default | Description |
|---|---|---|---|
| MaxRequestsPerIP | int64 | 0 (unlimited) | Max concurrent requests from a single IP |
| MaxConnsPerIP | int64 | 20 | Max concurrent connections per client IP. Under ProxyMode / TrustedProxies the key is the real client IP from X-Forwarded-For / X-Real-IP after the proxy rewrite; otherwise the socket peer IP at accept. 0 applies the default of 20; a negative value disables it. Over-limit connections get 429 and are closed. |
| MaxConcurrentReqs | int64 | 0 (unlimited) | Max concurrent requests server-wide |
s := core.New(core.Config{ Addr: ":443", MaxRequestsPerIP: 100, MaxConnsPerIP: 20, MaxConcurrentReqs: 10000, })
Workers & Listeners
| Field | Type | Default | Description |
|---|---|---|---|
| WorkerCount | int | auto (hardware threads, capped 128) | Linux amd64 epoll TLS worker count. When left 0 it auto-sizes to GOMAXPROCS (hardware threads), clamped to 128. Set it explicitly to override; an explicit value is honored uncapped (e.g. 300 on a 300-thread box). |
| Listeners | int | auto (hardware threads, capped 128) | Number of SO_REUSEPORT listeners, shared across the :80 redirect, 443 TCP, and 443 UDP (QUIC) sockets. When left 0 it auto-sizes to hardware threads (capped 128) on Linux amd64, so every protocol scales to the box. A UDP/HTTP3 flood or a plain :80 flood can each use all cores, while idle pools park at ~0% CPU. 1 on other platforms. Set explicitly to override. |
Miscellaneous
| Field | Type | Default | Description |
|---|---|---|---|
| PlainHTTP | bool | false | Run without TLS (plain HTTP mode) |
| DisableHTTP2 | bool | false | When true the server never negotiates HTTP/2 and only serves HTTP/1.1 |
| Debug | bool | false | Enable debug logging (connections, TLS, errors) |
| LogRequests | bool | true | Log every request (method, path, status, duration) |
Server Hooks
Hooks let you intercept the request lifecycle without writing middleware. They run in registration order, and you can register multiple hooks of each type.
| Method | Signature | Description |
|---|---|---|
| OnRequest | func(*Request, *Response) bool | Called before route handlers. Return false to reject the request; the handler will not run. |
| OnResponse | func(*Request, *Response) | Called after every response, before compression. Use for logging, metrics, or header injection. |
s.OnRequest(func(req *core.Request, resp *core.Response) bool { if req.Header("X-Block") == "true" { resp.Status(403).String("Blocked") return false } return true }) s.OnResponse(func(req *core.Request, resp *core.Response) { log.Printf("%s %s -> %d", req.Method, req.Path, resp.StatusCode) })
Static File Serving
Serve files from a directory with automatic MIME type detection, path traversal protection, and optional directory listing.
| Function | Signature | Description |
|---|---|---|
| Static | Static(urlPrefix string, cfg StaticConfig) HandlerFunc | Serve a directory with full configuration |
| StaticDir | StaticDir(urlPrefix, root string) HandlerFunc | Convenience shorthand with default settings |
StaticConfig
| Field | Type | Default | Description |
|---|---|---|---|
| Root | string | "" | Root directory to serve files from |
| Index | string | "index.html" | Default file to serve for directory requests |
| Browse | bool | false | Enable directory listing when no index file exists |
Path traversal protection is built in: requests containing .., null bytes, or backslashes are rejected, and the resolved path is validated to stay within the root directory. MIME types are detected from file extensions (~40 types supported).
s.Router.GET("/static/*filepath", core.Static("/static", core.StaticConfig{ Root: "./public", Index: "index.html", Browse: false, })) s.Router.GET("/files/*filepath", core.StaticDir("/files", "./uploads"))
Graceful Shutdown
Use s.Shutdown to gracefully drain active connections before stopping the server. Pass a context with a deadline to bound the drain time. Shutdown stops ACME renewal, rate-limit cleanup, and closes all listeners.
| Method | Signature | Description |
|---|---|---|
| Shutdown | Shutdown(ctx context.Context) error | Gracefully drains connections within the context deadline |
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() if err := s.Shutdown(ctx); err != nil { log.Printf("shutdown: %v", err) }
Full Example
A production-ready configuration using most available options:
s := core.New(core.Config{ Addr: ":443", HTTPAddr: ":80", ServerName: "my-app", ACME: &core.ACMEConfig{ Email: "admin@example.com", Domains: []string{"example.com", "api.example.com"}, CacheDir: "/etc/alos/certs", }, ReadTimeout: 15 * time.Second, WriteTimeout: 30 * time.Second, IdleTimeout: 120 * time.Second, HandshakeTimeout: 5 * time.Second, ShutdownTimeout: 30 * time.Second, MaxBodySize: 10 * 1024 * 1024, MaxHeaderSize: 16 * 1024, MaxRequestsPerIP: 100, MaxConcurrentReqs: 10000, ConnBandwidth: core.BandwidthConfig{ MaxDownloadRate: 40, BurstSize: 16, }, GlobalBandwidth: core.BandwidthConfig{ MaxDownloadRate: 4000, BurstSize: 800, }, EnableCompress: true, CompressLevel: 6, CompressMinSize: 1024, WorkerCount: 8, TrustedProxies: []string{"10.0.0.0/8", "172.16.0.0/12"}, Debug: false, LogRequests: true, })