server

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 17 Imported by: 0

README

HTTP Server Module Architecture

Documentation

Index

Constants

View Source
const (
	PgUniqueViolation     = "23505" // unique_violation
	PgForeignKeyViolation = "23503" // foreign_key_violation
)

PostgreSQL error codes

Variables

This section is empty.

Functions

func GetConstraintName added in v0.0.11

func GetConstraintName(err error) string

GetConstraintName extracts the constraint name from a PostgreSQL error

func IsForeignKeyViolation added in v0.0.11

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation checks if the error is a PostgreSQL foreign key violation

func IsUniqueViolation added in v0.0.11

func IsUniqueViolation(err error) bool

IsUniqueViolation checks if the error is a PostgreSQL unique constraint violation

func ParseUniqueViolationField added in v0.0.11

func ParseUniqueViolationField(constraintName string) string

ParseUniqueViolationField extracts a user-friendly field name from the constraint name

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
	ServerType      ServerType
}

Config defines runtime configuration

func DefaultConfig

func DefaultConfig(serverType ServerType) *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
	ErrorConflict     ErrorType = 409
	ErrorLargePayload ErrorType = 413
)

type GRPCServer added in v0.0.6

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

func NewGRPCServer added in v0.0.6

func NewGRPCServer(cfg *Config, opts ...HTTPServerOption) *GRPCServer

func (*GRPCServer) GetLogger added in v0.0.6

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

func (*GRPCServer) GetResponseWriter added in v0.0.6

func (s *GRPCServer) GetResponseWriter() *ResponseWriter

gRPC has no response writer, so so panic

func (*GRPCServer) Router added in v0.0.6

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

gRPC has no router, so panic

func (*GRPCServer) Shutdown added in v0.0.6

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

func (*GRPCServer) Start added in v0.0.6

func (s *GRPCServer) Start() error

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) ErrorHandler added in v0.0.6

func (m *Middleware) ErrorHandler() 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

type ServerType added in v0.0.6

type ServerType string
const (
	ServerHTTP ServerType = "http"
	ServerGRPC ServerType = "grpc"
)

Jump to

Keyboard shortcuts

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