Documentation
¶
Overview ¶
Package httpserver provides HTTP server functionality with middleware support.
Index ¶
- type FileServer
- type HTTPServer
- type HandlerGroup
- func (hg *HandlerGroup) Handle(pattern string, handler http.Handler)
- func (hg *HandlerGroup) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
- func (hg *HandlerGroup) HandleGroup(pattern string, handler http.Handler)
- func (hg *HandlerGroup) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (hg *HandlerGroup) Use(middlewares ...Middleware)
- func (hg *HandlerGroup) UseFunc(middlewareFuncs ...func(http.Handler) http.Handler)
- type Middleware
- type MiddlewareFunc
- type RecoverMiddleware
- type TraceIDMiddleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileServer ¶
type FileServer struct {
// contains filtered or unexported fields
}
FileServer represents an HTTP file server for serving static files.
func NewFileServer ¶
func NewFileServer(fs fs.FS, basePath, port string) *FileServer
NewFileServer creates a new FileServer instance with the given file system, base path, and port.
func (*FileServer) Run ¶
func (s *FileServer) Run(ctx context.Context) error
Run starts the file server and listens for incoming requests.
func (*FileServer) ServeHTTP ¶
func (s *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HTTPServer ¶
type HTTPServer struct {
// contains filtered or unexported fields
}
HTTPServer represents an HTTP server with middleware support and graceful shutdown.
func New ¶
func New(port string, shutdownTimeout time.Duration) *HTTPServer
New creates a new HTTPServer instance with the specified port and shutdown timeout.
func (*HTTPServer) Healthcheck ¶
func (s *HTTPServer) Healthcheck(_ context.Context) any
Healthcheck returns health check information for the HTTP server.
type HandlerGroup ¶
type HandlerGroup struct {
// contains filtered or unexported fields
}
HandlerGroup represents a group of HTTP handlers that share common middlewares.
func NewHandlerGroup ¶
func NewHandlerGroup() *HandlerGroup
NewHandlerGroup creates a new HandlerGroup with an initialized http.ServeMux.
func (*HandlerGroup) Handle ¶
func (hg *HandlerGroup) Handle(pattern string, handler http.Handler)
Handle registers an http.Handler for the given pattern
func (*HandlerGroup) HandleFunc ¶
func (hg *HandlerGroup) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
HandleFunc registers an http.HandlerFunc for the given pattern
func (*HandlerGroup) HandleGroup ¶
func (hg *HandlerGroup) HandleGroup(pattern string, handler http.Handler)
HandleGroup applies `http.StripPrefix` to http.Handler and registers it for the given pattern
func (*HandlerGroup) ServeHTTP ¶
func (hg *HandlerGroup) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface, allowing HandlerGroup to be used as an HTTP handler itself.
func (*HandlerGroup) Use ¶
func (hg *HandlerGroup) Use(middlewares ...Middleware)
Use adds a middleware to the HandlerGroup's middleware chain.
type Middleware ¶
type Middleware interface {
// Wrap wraps an http.Handler with middleware logic.
Wrap(http.Handler) http.Handler
}
Middleware defines an interface for HTTP middleware.
type MiddlewareFunc ¶
MiddlewareFunc is a convenience type that implements the Middleware interface.
type RecoverMiddleware ¶
type RecoverMiddleware struct{}
RecoverMiddleware is a middleware that recovers from panics in HTTP handlers. It catches panics, logs the error, and returns an HTTP 500 response to the client.
func NewRecoverMiddleware ¶
func NewRecoverMiddleware() *RecoverMiddleware
NewRecoverMiddleware creates a new instance of RecoverMiddleware.
type TraceIDMiddleware ¶
type TraceIDMiddleware struct {
// contains filtered or unexported fields
}
TraceIDMiddleware is a middleware that adds a trace ID to the request context and response headers.
func NewTraceIDMiddleware ¶
func NewTraceIDMiddleware(contextKey any, header string) *TraceIDMiddleware
NewTraceIDMiddleware returns a new TraceID middleware. If key is nil, log.TraceIdKey is used. If header is empty, "Platforma-Trace-Id" is used.