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 ¶
- func JSON[Req any](handler func(ctx context.Context, req Req) (any, error)) http.Handler
- type Option
- func WithCircuitBreaker(consecutiveFailures uint32) Option
- func WithGRPC(addr string, opts ...grpc.ServerOption) Option
- func WithLogging(logger *kitlog.Logger) Option
- func WithMetrics(m *endpoint.Metrics) Option
- func WithRateLimit(rps float64) Option
- func WithRequestID() Option
- func WithTimeout(d time.Duration) Option
- type Service
- func (s *Service) GRPCServer() *grpc.Server
- func (s *Service) Handle(pattern string, handler http.Handler)
- func (s *Service) HandleFunc(pattern string, fn http.HandlerFunc)
- func (s *Service) Run()
- func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Service) Shutdown(ctx context.Context) error
- func (s *Service) Start()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(*Service)
Option configures a Service.
func WithCircuitBreaker ¶
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 ¶
WithLogging adds structured request logging.
func WithMetrics ¶
WithMetrics attaches a Metrics collector. The /health endpoint includes the request count when this option is set.
func WithRateLimit ¶
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 ¶
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 (*Service) GRPCServer ¶
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 ¶
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.