http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest          = NewError(400)
	ErrUnauthorized        = NewError(401)
	ErrForbidden           = NewError(403)
	ErrNotFound            = NewError(404)
	ErrMethodNotAllowed    = NewError(405)
	ErrInternalServerError = NewError(500)
	ErrBadGateway          = NewError(502)
	ErrServiceUnavailable  = NewError(503)
)

Predefined errors

Functions

func DefaultErrorHandler

func DefaultErrorHandler(c *Context, err error)

DefaultErrorHandler handles errors with JSON response

Types

type Config

type Config struct {
	ErrorHandler ErrorHandler
}

type Context

type Context struct {
	Request *http.Request
	Writer  http.ResponseWriter
	Params  map[string]string

	// Trace IDs (extracted from headers, not auto-generated)
	TraceID   string
	RequestID string

	// Error tracking (generated on error)
	ErrorID string
	// contains filtered or unexported fields
}

func (*Context) BadRequest

func (ctx *Context) BadRequest(message string) error

Error response helpers

func (*Context) BindJSON

func (ctx *Context) BindJSON(v interface{}) error

Request binding methods

func (*Context) Body

func (ctx *Context) Body() ([]byte, error)

func (*Context) Context

func (ctx *Context) Context() context.Context

func (*Context) Forbidden

func (ctx *Context) Forbidden(message string) error

func (*Context) Get

func (ctx *Context) Get(key string) string

Header helper methods

func (*Context) GetLocal

func (ctx *Context) GetLocal(key string) (any, bool)

GetLocal retrieves a value from request-scoped storage

func (*Context) HTML

func (ctx *Context) HTML(code int, html string) error

func (*Context) InternalServerError

func (ctx *Context) InternalServerError(message string) error

func (*Context) JSON

func (ctx *Context) JSON(code int, data interface{}) error

func (*Context) Locals

func (ctx *Context) Locals(key string) any

Locals retrieves a value (returns nil if not found)

func (*Context) NotFound

func (ctx *Context) NotFound(message string) error

func (*Context) Param

func (ctx *Context) Param(key string) string

func (*Context) ParamInt

func (ctx *Context) ParamInt(key string) (int, error)

Type-safe parameter extraction

func (*Context) PostForm

func (ctx *Context) PostForm(key string) string

func (*Context) Query

func (ctx *Context) Query(key string) string

func (*Context) QueryBool

func (ctx *Context) QueryBool(key string) bool

func (*Context) QueryInt

func (ctx *Context) QueryInt(key string, defaultValue ...int) int

func (*Context) Redirect

func (ctx *Context) Redirect(code int, location string) error

func (*Context) Set

func (ctx *Context) Set(key, value string)

func (*Context) SetContext

func (ctx *Context) SetContext(c context.Context)

func (*Context) SetLocal

func (ctx *Context) SetLocal(key string, value any)

SetLocal stores a value in request-scoped storage

func (*Context) Status

func (ctx *Context) Status(code int) *Context

func (*Context) String

func (ctx *Context) String(code int, format string, values ...interface{}) error

func (*Context) Unauthorized

func (ctx *Context) Unauthorized(message string) error

type Error

type Error struct {
	Code    int
	Message string
}

Error represents an HTTP error with status code

func NewError

func NewError(code int, message ...string) *Error

NewError creates a new HTTP error

func (*Error) Error

func (e *Error) Error() string

type ErrorHandler

type ErrorHandler func(*Context, error)

type HandlerFunc

type HandlerFunc func(*Context) error

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

type Route

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

func (*Route) Middleware

func (route *Route) Middleware(middleware ...MiddlewareFunc) *Route

func (*Route) Name

func (route *Route) Name(name string) *Route

type RouteGroup

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

func (*RouteGroup) DELETE

func (g *RouteGroup) DELETE(pattern string, handler HandlerFunc) *Route

func (*RouteGroup) GET

func (g *RouteGroup) GET(pattern string, handler HandlerFunc) *Route

func (*RouteGroup) Group

func (g *RouteGroup) Group(prefix string) *RouteGroup

func (*RouteGroup) Middleware

func (g *RouteGroup) Middleware(middleware ...MiddlewareFunc) *RouteGroup

func (*RouteGroup) PATCH

func (g *RouteGroup) PATCH(pattern string, handler HandlerFunc) *Route

func (*RouteGroup) POST

func (g *RouteGroup) POST(pattern string, handler HandlerFunc) *Route

func (*RouteGroup) PUT

func (g *RouteGroup) PUT(pattern string, handler HandlerFunc) *Route

type Router

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

func NewRouter

func NewRouter(config ...Config) *Router

func (*Router) DELETE

func (r *Router) DELETE(pattern string, handler HandlerFunc) *Route

func (*Router) GET

func (r *Router) GET(pattern string, handler HandlerFunc) *Route

func (*Router) HEAD

func (r *Router) HEAD(pattern string, handler HandlerFunc) *Route

func (*Router) Middleware

func (r *Router) Middleware(middleware ...MiddlewareFunc)

func (*Router) OPTIONS

func (r *Router) OPTIONS(pattern string, handler HandlerFunc) *Route

func (*Router) PATCH

func (r *Router) PATCH(pattern string, handler HandlerFunc) *Route

func (*Router) POST

func (r *Router) POST(pattern string, handler HandlerFunc) *Route

func (*Router) PUT

func (r *Router) PUT(pattern string, handler HandlerFunc) *Route

func (*Router) Prefix

func (r *Router) Prefix(prefix string) *RouteGroup

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) URL

func (r *Router) URL(name string, params map[string]string) (string, error)

Jump to

Keyboard shortcuts

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