server

package module
v0.0.0-...-74974c2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiError

type ApiError struct {
	Code    int    `json:"code,omitempty"`
	Message string `json:"message"`
	TraceID string `json:"trace_id"`
}

func ToApiError

func ToApiError(c *gin.Context, err error) *ApiError

ToApiError converts error to API-safe structure

func (*ApiError) Error

func (e *ApiError) Error() string

type Config

type Config struct {
	Port            string
	Environment     string
	Version         string
	MaxRequestSize  int64
	EnableProfiling bool
	ShutdownTimeout time.Duration
}

Config defines runtime configuration

func DefaultConfig

func DefaultConfig() *Config

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

type ErrorType

type ErrorType int

ErrorType represents categorized error types

const (
	ErrorInternal     ErrorType = 500
	ErrorBadRequest   ErrorType = 400
	ErrorUnauthorized ErrorType = 401
	ErrorNotFound     ErrorType = 404
	ErrorLargePayload ErrorType = 413
)

type HTTPServer

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

HTTPServer is the main implementation

func NewHTTPServer

func NewHTTPServer(cfg *Config, opts ...HTTPServerOption) *HTTPServer

func (*HTTPServer) GetLogger

func (s *HTTPServer) GetLogger() api.Logger

func (*HTTPServer) GetResponseWriter

func (s *HTTPServer) GetResponseWriter() *ResponseWriter

func (*HTTPServer) Router

func (s *HTTPServer) Router() *gin.Engine

func (*HTTPServer) Shutdown

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

func (*HTTPServer) Start

func (s *HTTPServer) Start() error

type HTTPServerOption

type HTTPServerOption func(*HTTPServer)

func WithHandler

func WithHandler(handler Handler) HTTPServerOption

func WithLogger

func WithLogger(logger api.Logger) HTTPServerOption

func WithMiddleware

func WithMiddleware(m *Middleware) HTTPServerOption

func WithResponseWriter

func WithResponseWriter(w *ResponseWriter) HTTPServerOption

func WithShutdownFunc

func WithShutdownFunc(fn func()) HTTPServerOption

type Handler

type Handler interface {
	Setup(server Server) error
	Shutdown() error
}

Handler allows for modular startup and teardown

type InternalError

type InternalError struct {
	Type       ErrorType
	Message    string
	Original   error
	CallerInfo string
}

InternalError wraps errors with context

func NewError

func NewError(errType ErrorType, message string, err error) *InternalError

NewError creates a new structured internal error

func (*InternalError) Error

func (e *InternalError) Error() string

func (*InternalError) ToHttpMessage

func (e *InternalError) ToHttpMessage() string

func (*InternalError) ToHttpStatusCode

func (e *InternalError) ToHttpStatusCode() int

func (*InternalError) Unwrap

func (e *InternalError) Unwrap() error

type Middleware

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

func NewMiddleware

func NewMiddleware(logger api.Logger, config *Config) *Middleware

func (*Middleware) Apply

func (m *Middleware) Apply(router *gin.Engine)

func (*Middleware) CORS

func (m *Middleware) CORS() gin.HandlerFunc

func (*Middleware) Logger

func (m *Middleware) Logger() gin.HandlerFunc

func (*Middleware) MaxBodySize

func (m *Middleware) MaxBodySize(limit int64) gin.HandlerFunc

func (*Middleware) PrettyLog

func (m *Middleware) PrettyLog() gin.HandlerFunc

func (*Middleware) Profiling

func (m *Middleware) Profiling() gin.HandlerFunc

func (*Middleware) Recovery

func (m *Middleware) Recovery() gin.HandlerFunc

type Response

type Response struct {
	Data interface{} `json:"data"`
}

Response JSON structures

type ResponseWriter

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

func NewResponseWriter

func NewResponseWriter(logger api.Logger) *ResponseWriter

func (*ResponseWriter) BadRequest

func (rw *ResponseWriter) BadRequest(c *gin.Context, msg string)

Shorthand helpers

func (*ResponseWriter) Created

func (rw *ResponseWriter) Created(c *gin.Context, data interface{})

func (*ResponseWriter) Error

func (rw *ResponseWriter) Error(c *gin.Context, err error)

func (*ResponseWriter) InternalServerError

func (rw *ResponseWriter) InternalServerError(c *gin.Context, err error)

func (*ResponseWriter) NoContent

func (rw *ResponseWriter) NoContent(c *gin.Context)

func (*ResponseWriter) NotFound

func (rw *ResponseWriter) NotFound(c *gin.Context)

func (*ResponseWriter) Success

func (rw *ResponseWriter) Success(c *gin.Context, data interface{})

func (*ResponseWriter) Unauthorized

func (rw *ResponseWriter) Unauthorized(c *gin.Context)

type Server

type Server interface {
	Start() error
	Router() *gin.Engine
	Shutdown(ctx context.Context) error
	GetResponseWriter() *ResponseWriter
	GetLogger() api.Logger
}

Server defines the HTTP server contract

Jump to

Keyboard shortcuts

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