chiserver

package
v1.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package chiserver provides a production-ready Chi HTTP server with:

  • Chi middleware: RequestID, RealIP, Recoverer
  • CORS headers
  • JSON error responses via RespondError
  • /health endpoint
  • BindAndValidate for JSON + validator tags

Usage:

srv := chiserver.New(chiserver.Config{Port: 8080})
r := srv.Router()
r.Mount("/api/v1", userHandler.Routes())
if err := srv.Run(); err != nil { ... }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindAndValidate

func BindAndValidate(w http.ResponseWriter, r *http.Request, dst any) error

BindAndValidate decodes JSON from the body and runs struct tag validation. On failure it writes 400 JSON and returns the error.

func RespondError

func RespondError(w http.ResponseWriter, err error)

RespondError writes a JSON error body with status from apperrors.HTTPStatus.

Types

type Config

type Config struct {
	Port int
}

Config holds server configuration.

type Server

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

Server wraps a Chi router with portsmith defaults.

func New

func New(cfg Config) *Server

New creates a new Server with default middleware and /health.

func (*Server) Router

func (s *Server) Router() chi.Router

Router returns the Chi router for mounting domain routes.

func (*Server) Run

func (s *Server) Run() error

Run listens and serves HTTP until the server stops.

Jump to

Keyboard shortcuts

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