kit

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package kit provides a high-level, zero-boilerplate API for rapid prototyping and production microservices.

Quickstart:

func main() {
    svc := kit.New(":8080")
    svc.Handle("/hello", kit.JSON[HelloReq](func(ctx context.Context, req HelloReq) (any, error) {
        return HelloResp{Message: "Hello, " + req.Name}, nil
    }))
    svc.Run()
}

With middleware:

svc := kit.New(":8080",
    kit.WithRateLimit(100),
    kit.WithCircuitBreaker(5),
    kit.WithTimeout(5*time.Second),
    kit.WithRequestID(),
    kit.WithLogging(logger),
    kit.WithMetrics(&metrics),
)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON[Req any](handler func(ctx context.Context, req Req) (any, error)) http.Handler

JSON creates a typed JSON http.Handler without needing a Service.

Types

type Option

type Option func(*Service)

Option configures a Service.

func WithCircuitBreaker

func WithCircuitBreaker(consecutiveFailures uint32) Option

WithCircuitBreaker adds a Gobreaker circuit breaker that opens after consecutiveFailures consecutive errors.

func WithGRPC

func WithGRPC(addr string, opts ...grpc.ServerOption) Option

WithGRPC enables a gRPC server on the given address (for example ":8081"). Call GRPCServer() to register proto services before calling Run or Start.

func WithLogging

func WithLogging(logger *kitlog.Logger) Option

WithLogging adds structured request logging.

func WithMetrics

func WithMetrics(m *endpoint.Metrics) Option

WithMetrics attaches a Metrics collector. The /health endpoint includes the request count when this option is set.

func WithRateLimit

func WithRateLimit(rps float64) Option

WithRateLimit adds a token-bucket rate limiter (rps = requests per second).

func WithRequestID

func WithRequestID() Option

WithRequestID injects a request ID into the context and response headers. The ID is taken from X-Request-ID if present, otherwise generated.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout adds a per-request context deadline.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service is a ready-to-run HTTP + gRPC microservice. Create one with New, register handlers with Handle/GRPC, then call Run.

func New

func New(addr string, opts ...Option) *Service

New creates a Service listening on addr (for example ":8080").

func (*Service) GRPCServer

func (s *Service) GRPCServer() *grpc.Server

GRPCServer returns the underlying *grpc.Server so callers can register proto services. It is created lazily on first call. Panics if WithGRPC was not set.

func (*Service) Handle

func (s *Service) Handle(pattern string, handler http.Handler)

Handle registers an http.Handler for the given pattern. Service-level middleware is applied by wrapping the handler as an endpoint.

func (*Service) HandleFunc

func (s *Service) HandleFunc(pattern string, fn http.HandlerFunc)

HandleFunc registers a plain http.HandlerFunc.

func (*Service) Run

func (s *Service) Run()

Run starts the HTTP server (and gRPC server if enabled) and blocks until SIGINT or SIGTERM. It performs a graceful shutdown with a 10-second deadline.

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler, allowing Service to be used directly with httptest.NewServer or http.ListenAndServe.

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context) error

Shutdown gracefully stops the HTTP server and gRPC server if running.

func (*Service) Start

func (s *Service) Start()

Start starts the HTTP server (and gRPC server if enabled) in the background.

Jump to

Keyboard shortcuts

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