Documentation
¶
Index ¶
- Constants
- func GetConstraintName(err error) string
- func IsForeignKeyViolation(err error) bool
- func IsUniqueViolation(err error) bool
- func ParseUniqueViolationField(constraintName string) string
- type ApiError
- type Config
- type ErrorResponse
- type ErrorType
- type GRPCServer
- type HTTPServer
- type HTTPServerOption
- type Handler
- type InternalError
- type Middleware
- func (m *Middleware) Apply(router *gin.Engine)
- func (m *Middleware) CORS() gin.HandlerFunc
- func (m *Middleware) ErrorHandler() gin.HandlerFunc
- func (m *Middleware) Logger() gin.HandlerFunc
- func (m *Middleware) MaxBodySize(limit int64) gin.HandlerFunc
- func (m *Middleware) PrettyLog() gin.HandlerFunc
- func (m *Middleware) Profiling() gin.HandlerFunc
- func (m *Middleware) Recovery() gin.HandlerFunc
- type Response
- type ResponseWriter
- func (rw *ResponseWriter) BadRequest(c *gin.Context, msg string)
- func (rw *ResponseWriter) Created(c *gin.Context, data interface{})
- func (rw *ResponseWriter) Error(c *gin.Context, err error)
- func (rw *ResponseWriter) InternalServerError(c *gin.Context, err error)
- func (rw *ResponseWriter) NoContent(c *gin.Context)
- func (rw *ResponseWriter) NotFound(c *gin.Context)
- func (rw *ResponseWriter) Success(c *gin.Context, data interface{})
- func (rw *ResponseWriter) Unauthorized(c *gin.Context)
- type Server
- type ServerType
Constants ¶
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
GetConstraintName extracts the constraint name from a PostgreSQL error
func IsForeignKeyViolation ¶ added in v0.0.11
IsForeignKeyViolation checks if the error is a PostgreSQL foreign key violation
func IsUniqueViolation ¶ added in v0.0.11
IsUniqueViolation checks if the error is a PostgreSQL unique constraint violation
func ParseUniqueViolationField ¶ added in v0.0.11
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 ¶
ToApiError converts error to API-safe structure
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 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) 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 InternalError ¶
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 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) 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" )