Documentation
¶
Index ¶
- Variables
- func ConflictErrorResponse(ctx *gin.Context, message string)
- func ErrorBadRequest(ctx *gin.Context, message interface{})
- func ErrorsBadRequest(ctx *gin.Context, message []string)
- func InternalErrorResponse(ctx *gin.Context, message string)
- func NewEngine(config *APIConfig) *gin.Engine
- func NewServer(router *gin.Engine, config *APIConfig) *http.Server
- func SuccessResponse(ctx *gin.Context, body interface{})
- func UnauthorizedErrorResponse(ctx *gin.Context, message string)
- type APIConfig
- type ErrorResponseMessage
Constants ¶
This section is empty.
Variables ¶
var APIFlags = []cli.Flag{ &cli.StringFlag{ Name: apiHost, Value: "0.0.0.0", Usage: "API server host address", EnvVars: []string{"API_HOST"}, }, &cli.IntFlag{ Name: apiPort, Value: 8000, Usage: "API server port", EnvVars: []string{"API_PORT"}, }, &cli.IntFlag{ Name: apiMaxHeaderSize, Value: 262144, Usage: "Maximum size of request headers in bytes", EnvVars: []string{"API_MAX_HEADER_SIZE"}, }, &cli.IntFlag{ Name: apiRequestTimeout, Value: 5, Usage: "Maximum duration for reading the entire request in seconds", EnvVars: []string{"API_REQUEST_TIMEOUT"}, }, &cli.IntFlag{ Name: apiResponseTimeout, Value: 5, Usage: "Maximum duration before timing out writes of the response in seconds", EnvVars: []string{"API_RESPONSE_TIMEOUT"}, }, &cli.BoolFlag{ Name: logRequest, Value: true, Usage: "Enable or disable request logging", EnvVars: []string{"LOG_REQUEST"}, }, &cli.StringFlag{ Name: jwtSecret, Value: "qi87x8Sd9KpQUuiOMP7gFMid3gRTQFjr", Usage: "JWT secret used for signing authentication tokens", EnvVars: []string{"JWT_SECRET"}, }, &cli.IntFlag{ Name: jwtSecretLifeTime, Value: 60, Usage: "JWT token lifetime in minutes", EnvVars: []string{"JWT_SECRET_LIFE_TIME"}, }, }
APIFlags defines a slice of CLI flags for configuring API server settings. These flags allow customization of server parameters through command-line arguments or environment variables.
var Module = fx.Module("server", fx.Provide(GetAPIConfig), fx.Provide(NewEngine), fx.Provide(NewServer), )
Module is an Fx module that provides dependencies for the server setup, including API configuration, the HTTP engine, and the server instance. These components are initialized using dependency injection and are required for running the API server.
Functions ¶
func ConflictErrorResponse ¶
ConflictErrorResponse logs the error message and sends a conflict response with status 409. The function also aborts the current context.
func ErrorBadRequest ¶
ErrorBadRequest logs a single error message and sends a bad request response with status 400. The message can be of any type, and the context is aborted.
func ErrorsBadRequest ¶
ErrorsBadRequest logs the list of error messages and sends a bad request response with status 400. It uses a list of error messages and aborts the current context.
func InternalErrorResponse ¶
InternalErrorResponse logs the error message and sends an internal server error response with status 500. The function also aborts the current context.
func NewEngine ¶
NewEngine creates and configures a new Gin engine instance. It applies middleware, including request logging (if enabled), request recovery, and CORS settings.
func NewServer ¶
NewServer creates and configures a new HTTP server with a specified Gin router and API configuration. The server includes settings for address, timeouts, and max header bytes, with a timeout handler for request limits.
func SuccessResponse ¶
SuccessResponse sends a successful HTTP response with status 200 and a response body.
func UnauthorizedErrorResponse ¶
UnauthorizedErrorResponse logs the error message and sends an unauthorized response with status 401. The function also aborts the current context.
Types ¶
type APIConfig ¶
type APIConfig struct { APIHost string // Server host address APIPort string // Server port number RequestTimeout int // Maximum request read duration in seconds ResponseTimeout int // Maximum response write duration in seconds MaxHeaderBytes int // Maximum size of request headers in bytes JWTSecret string // JWT secret for signing tokens JWTSecretLifeTime int // JWT token lifetime in minutes LogRequest bool // Enable request logging }
APIConfig holds configuration settings for the API server.
func GetAPIConfig ¶
func GetAPIConfig(c *cli.Context) *APIConfig
GetAPIConfig retrieves API server configuration from CLI flags or environment variables and initializes an APIConfig instance with these settings.
Parameters:
- c: The CLI context containing parsed command-line arguments and environment variables.
Returns:
A pointer to an ApiConfig instance populated with the specified configuration.
type ErrorResponseMessage ¶
type ErrorResponseMessage struct {
Errors []string `json:"errors"`
}
ErrorResponseMessage represents the structure of an error response with a list of error messages.
func NewErrorMessage ¶
func NewErrorMessage(err interface{}) *ErrorResponseMessage
NewErrorMessage creates a new ErrorResponseMessage with a single error message. It accepts either an error object or a string and returns a pointer to ErrorResponseMessage.
func NewErrorMessages ¶
func NewErrorMessages(errors []string) *ErrorResponseMessage
NewErrorMessages creates an ErrorResponseMessage with multiple error messages.