httpkit

package
v0.0.0-...-5d01d84 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 10 Imported by: 0

README

HTTPKit Module

HTTP server integration for Fx applications.

Features

  • Provides net.Listener bound to configured address.
  • Provides *http.ServeMux.
  • Opt-in /debug/pprof endpoints.
  • Supports grouped route registration (group:"http.handlers").
  • Graceful shutdown with Fx lifecycle.

Config

http:
  addr: ":8080"
  read_timeout_ms: 5000
  write_timeout_ms: 5000
  enable_pprof: false

httpkit.Config uses validate tags, so addr must be provided and timeout values must be non-negative. Invalid configs fail fast when the Fx app starts.

Usage

app := fx.New(
  httpkit.Module(),
  fx.Provide(func() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("pong"))
    })
  }),
  fx.Provide(func(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      h.ServeHTTP(w, r)
    })
  }),
)

Documentation

Overview

Package httpkit provides an HTTP server with Uber Fx integration. It includes configuration loading, optional pprof endpoints, structured logging, graceful shutdown, and a simple mechanism for services to register their own handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Module

func Module() fx.Option

Module provides HTTP server configuration and lifecycle management for Fx.

It wires:

  • Config from "http" subtree
  • net.Listener bound to Addr
  • *http.ServeMux with optional pprof + group handlers
  • Server lifecycle with graceful shutdown

To register routes from a service:

fx.Provide(func() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("hello"))
    })
})

Or directly provide a route:

fx.Provide(func() http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("pong"))
    })
})

func NewListener

func NewListener(cfg *Config) (net.Listener, error)

NewListener binds a TCP listener to the configured Addr.

func NewMux

func NewMux(p Params) *http.ServeMux

NewMux builds a ServeMux with optional pprof and all grouped handlers.

Types

type Config

type Config struct {
	// Addr is the listen address, e.g. ":8080".
	Addr string `yaml:"addr" validate:"required"`

	// ReadTimeoutMS sets the maximum duration for reading the request in ms.
	ReadTimeoutMS int `yaml:"read_timeout_ms" validate:"gte=0"`

	// WriteTimeoutMS sets the maximum duration for writing the response in ms.
	WriteTimeoutMS int `yaml:"write_timeout_ms" validate:"gte=0"`

	// EnablePprof enables /debug/pprof endpoints if true. Default false.
	EnablePprof bool `yaml:"enable_pprof"`
}

Config holds HTTP server configuration.

type Handler

type Handler struct {
	Pattern string
	Handler http.Handler
}

Handler allows services to register additional HTTP routes via Fx groups.

type Params

type Params struct {
	fx.In
	Cfg      *Config
	Handlers []Handler `group:"http.handlers"`
}

Params is used by NewMux to pull in grouped handlers.

Jump to

Keyboard shortcuts

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