Documentation
¶
Overview ¶
Package server provides an abstraction layer for HTTP servers. It wraps popular frameworks like Gin to provide a consistent API.
Package server provides an abstraction layer for HTTP servers. It wraps popular frameworks like Gin to provide a consistent API.
Package server provides version information for the go-http-server.
Index ¶
- Constants
- Variables
- func NewServer(frameworkType core.FrameworkType, port string, showFrameworkLogs bool) (core.Server, error)
- type APIKeyConfig
- type AuthConfig
- type AuthType
- type BadRequestHttpError
- type BasicAuthUserLookup
- type CORSConfig
- type Context
- type DuplicateRequestConfig
- type ErrorDetail
- type ErrorHandlerConfig
- type ErrorResponse
- type ForbiddenHttpError
- type FrameworkType
- type GinServer
- type HandlerFunc
- type HttpMethod
- type InternalServerHttpError
- type JWTUserLookup
- type LoggingConfig
- type MapClaims
- type MethodNotAllowedHttpError
- type NotFoundHttpError
- type RequestIDGenerator
- type RequestIDStorage
- type RouterGroup
- type Server
- type ServerBuilder
- func (b *ServerBuilder) AddController(controller core.Controller) *ServerBuilder
- func (b *ServerBuilder) AddControllers(controllers ...core.Controller) *ServerBuilder
- func (b *ServerBuilder) AddMiddleware(middleware core.HandlerFunc) *ServerBuilder
- func (b *ServerBuilder) AddMiddlewares(middleware ...core.HandlerFunc) *ServerBuilder
- func (b *ServerBuilder) Build() (core.Server, error)
- func (b *ServerBuilder) WithCORS(cors CORSConfig) *ServerBuilder
- func (b *ServerBuilder) WithDefaultCORS() *ServerBuilder
- func (b *ServerBuilder) WithDefaultErrorHandling() *ServerBuilder
- func (b *ServerBuilder) WithDefaultLogging(console ...bool) *ServerBuilder
- func (b *ServerBuilder) WithDefaultPort() *ServerBuilder
- func (b *ServerBuilder) WithDefaultRandomPort() *ServerBuilder
- func (b *ServerBuilder) WithDefaultTimeout() *ServerBuilder
- func (b *ServerBuilder) WithErrorHandler(errorConfig core.ErrorHandlerConfig) *ServerBuilder
- func (b *ServerBuilder) WithFrameworkLogs(enabled bool) *ServerBuilder
- func (b *ServerBuilder) WithLogging(customFields map[string]string) *ServerBuilder
- func (b *ServerBuilder) WithNoMethod(handlers ...core.HandlerFunc) *ServerBuilder
- func (b *ServerBuilder) WithNoRoute(handlers ...core.HandlerFunc) *ServerBuilder
- func (b *ServerBuilder) WithRemoteLogging(remoteURL string, customFields map[string]string) *ServerBuilder
- func (b *ServerBuilder) WithTimeout(timeout TimeoutConfig) *ServerBuilder
- type ServiceUnavailableHttpError
- type StdServer
- type TimeoutConfig
- type UnauthorizedHttpError
Constants ¶
const ( // FrameworkGin represents the Gin framework. FrameworkGin = core.FrameworkGin // FrameworkStdHTTP represents the standard net/http package. FrameworkStdHTTP = core.FrameworkStdHTTP // HTTP methods // GET represents the HTTP GET method. GET = core.GET // POST represents the HTTP POST method. POST = core.POST // PUT represents the HTTP PUT method. PUT = core.PUT // DELETE represents the HTTP DELETE method. DELETE = core.DELETE // PATCH represents the HTTP PATCH method. PATCH = core.PATCH )
Re-export constants from core package
const ( // AuthTypeBasic represents HTTP Basic authentication. AuthTypeBasic = middleware.AuthTypeBasic // AuthTypeJWT represents JWT Bearer token authentication. AuthTypeJWT = middleware.AuthTypeJWT )
Re-export constants from middleware package
const Version = "0.1.3"
Version is the current version of the go-http-server.
Variables ¶
var ( // TimeoutMiddleware returns a middleware function that times out requests after a specified duration. TimeoutMiddleware = middleware.TimeoutMiddleware // AuthMiddleware returns a middleware function that checks authorization. AuthMiddleware = middleware.AuthMiddleware // APIKeyMiddleware returns a middleware function that checks for a valid API key. APIKeyMiddleware = middleware.APIKeyMiddleware // CORSMiddleware returns a middleware function that handles CORS (Cross-Origin Resource Sharing). CORSMiddleware = middleware.CORSMiddleware // DuplicateRequestMiddleware returns a middleware function that prevents duplicate requests. DuplicateRequestMiddleware = middleware.DuplicateRequestMiddleware // GetUserFromContext retrieves the authenticated user from the context. GetUserFromContext = middleware.GetUserFromContext // NewDefaultAPIKeyMiddleware returns a middleware function with default configuration and the specified API key. NewDefaultAPIKeyMiddleware = middleware.NewDefaultAPIKeyMiddleware // NewDefaultJWTAuthMiddleware returns a middleware function with default JWT authentication configuration. NewDefaultJWTAuthMiddleware = middleware.NewDefaultJWTAuthMiddleware // NewDefaultBasicAuthMiddleware returns a middleware function with default Basic authentication configuration. NewDefaultBasicAuthMiddleware = middleware.NewDefaultBasicAuthMiddleware // NewDefaultCORSMiddleware returns a middleware function with default configuration. NewDefaultCORSMiddleware = middleware.NewDefaultCORSMiddleware // NewDefaultDuplicateRequestMiddleware returns a middleware function with default configuration. NewDefaultDuplicateRequestMiddleware = middleware.NewDefaultDuplicateRequestMiddleware // NewDefaultConsoleLogging returns a logging configuration for console-only logging with the specified ignore path list and custom fields. NewDefaultConsoleLogging = middleware.NewDefaultConsoleLogging // NewDefaultTimeoutMiddleware returns a middleware function with default configuration. NewDefaultTimeoutMiddleware = middleware.NewDefaultTimeoutMiddleware )
Re-export functions from middleware package
var ( // NewErrorResponse creates a new ErrorResponse with the given status code and message. NewErrorResponse = errors.NewErrorResponse // NewBadRequestResponse creates a new ErrorResponse for a 400 Bad Request error. NewBadRequestResponse = errors.NewBadRequestResponse NewUnauthorizedResponse = errors.NewUnauthorizedResponse // NewForbiddenResponse creates a new ErrorResponse for a 403 Forbidden error. NewForbiddenResponse = errors.NewForbiddenResponse // NewNotFoundResponse creates a new ErrorResponse for a 404 Not Found error. NewNotFoundResponse = errors.NewNotFoundResponse // NewConflictResponse creates a new ErrorResponse for a 409 Conflict error. NewConflictResponse = errors.NewConflictResponse // NewInternalServerErrorResponse creates a new ErrorResponse for a 500 Internal Server Error. NewInternalServerErrorResponse = errors.NewInternalServerErrorResponse NewServiceUnavailableResponse = errors.NewServiceUnavailableResponse // Constructor functions for the error structs // NewBadRequestHttpError creates a new BadRequestHttpError. NewBadRequestHttpError = errors.NewBadRequestHttpError NewUnauthorizedHttpError = errors.NewUnauthorizedHttpError // NewForbiddenHttpError creates a new ForbiddenHttpError. NewForbiddenHttpError = errors.NewForbiddenHttpError // NewNotFoundHttpError creates a new NotFoundHttpError. NewNotFoundHttpError = errors.NewNotFoundHttpError // NewMethodNotAllowedHttpError creates a new MethodNotAllowedHttpError. NewMethodNotAllowedHttpError = errors.NewMethodNotAllowedHttpError // NewInternalServerHttpError creates a new InternalServerHttpError. NewInternalServerHttpError = errors.NewInternalServerHttpError NewServiceUnavailableHttpError = errors.NewServiceUnavailableHttpError )
Re-export functions from middleware/errors package
Functions ¶
func NewServer ¶
func NewServer(frameworkType core.FrameworkType, port string, showFrameworkLogs bool) (core.Server, error)
NewServer creates a new Server instance. By default, it uses the Gin framework if no framework type is specified. If port is not provided, it defaults to "8080". If showFrameworkLogs is true, logs about the framework, middleware, and routes will be printed to the console. If showFrameworkLogs is false, these logs will be suppressed.
Types ¶
type APIKeyConfig ¶
type APIKeyConfig = middleware.APIKeyConfig
APIKeyConfig holds configuration for the API key middleware.
type AuthConfig ¶
type AuthConfig = middleware.AuthConfig
AuthConfig holds configuration for the authorization middleware.
type AuthType ¶
type AuthType = middleware.AuthType
AuthType represents the type of authentication to use.
type BadRequestHttpError ¶
type BadRequestHttpError = errors.BadRequestHttpError
Error structs that embed the error interface BadRequestHttpError represents a 400 Bad Request error.
type BasicAuthUserLookup ¶
type BasicAuthUserLookup = middleware.BasicAuthUserLookup
BasicAuthUserLookup defines the interface for looking up users based on Basic Auth credentials.
type CORSConfig ¶
type CORSConfig = middleware.CORSConfig
CORSConfig holds configuration for the CORS middleware.
type DuplicateRequestConfig ¶
type DuplicateRequestConfig = middleware.DuplicateRequestConfig
DuplicateRequestConfig holds configuration for the duplicate request prevention middleware.
type ErrorDetail ¶
type ErrorDetail = errors.ErrorDetail
ErrorDetail represents the structure of an error detail in the response.
type ErrorHandlerConfig ¶
type ErrorHandlerConfig = core.ErrorHandlerConfig
ErrorHandlerConfig holds configuration for the error handler middleware.
type ErrorResponse ¶
type ErrorResponse = errors.ErrorResponse
ErrorResponse represents the structure of an error response.
type ForbiddenHttpError ¶
type ForbiddenHttpError = errors.ForbiddenHttpError
ForbiddenHttpError represents a 403 Forbidden error.
type FrameworkType ¶
type FrameworkType = core.FrameworkType
FrameworkType represents the type of HTTP framework to use.
type HandlerFunc ¶
type HandlerFunc = core.HandlerFunc
HandlerFunc is a function that handles an HTTP request.
type InternalServerHttpError ¶
type InternalServerHttpError = errors.InternalServerHttpError
InternalServerHttpError represents a 500 Internal Server Error.
type JWTUserLookup ¶
type JWTUserLookup = middleware.JWTUserLookup
JWTUserLookup defines the interface for looking up users based on JWT claims.
type LoggingConfig ¶
type LoggingConfig = core.LoggingConfig
LoggingConfig holds configuration for the logging middleware.
type MethodNotAllowedHttpError ¶
type MethodNotAllowedHttpError = errors.MethodNotAllowedHttpError
MethodNotAllowedHttpError represents a 405 Method Not Allowed error.
type NotFoundHttpError ¶
type NotFoundHttpError = errors.NotFoundHttpError
NotFoundHttpError represents a 404 Not Found error.
type RequestIDGenerator ¶
type RequestIDGenerator = middleware.RequestIDGenerator
RequestIDGenerator defines the interface for generating request IDs.
type RequestIDStorage ¶
type RequestIDStorage = middleware.RequestIDStorage
RequestIDStorage defines the interface for checking and storing request IDs.
type ServerBuilder ¶
type ServerBuilder struct {
// contains filtered or unexported fields
}
ServerBuilder is a builder for creating a server with controllers and middleware.
func NewGinServerBuilder ¶
func NewGinServerBuilder() *ServerBuilder
NewGinServerBuilder creates a new ServerBuilder with the Gin framework. This is a convenience function that doesn't require any arguments. The port must be set using WithDefaultPort before calling Build.
func NewServerBuilder ¶
func NewServerBuilder(frameworkType core.FrameworkType, port ...string) *ServerBuilder
NewServerBuilder creates a new ServerBuilder with the specified framework type and optional port. If port is provided, it will be used; otherwise, you must call WithDefaultPort before Build.
func (*ServerBuilder) AddController ¶
func (b *ServerBuilder) AddController(controller core.Controller) *ServerBuilder
AddController adds a controller to the builder.
func (*ServerBuilder) AddControllers ¶
func (b *ServerBuilder) AddControllers(controllers ...core.Controller) *ServerBuilder
AddControllers adds multiple controllers to the builder.
func (*ServerBuilder) AddMiddleware ¶
func (b *ServerBuilder) AddMiddleware(middleware core.HandlerFunc) *ServerBuilder
AddMiddleware adds a middleware to the builder.
func (*ServerBuilder) AddMiddlewares ¶
func (b *ServerBuilder) AddMiddlewares(middleware ...core.HandlerFunc) *ServerBuilder
AddMiddlewares adds multiple middleware to the builder.
func (*ServerBuilder) Build ¶
func (b *ServerBuilder) Build() (core.Server, error)
Build creates a server with the configured controllers and middleware.
func (*ServerBuilder) WithCORS ¶
func (b *ServerBuilder) WithCORS(cors CORSConfig) *ServerBuilder
WithCORS configures the CORS middleware with the specified configuration.
func (*ServerBuilder) WithDefaultCORS ¶
func (b *ServerBuilder) WithDefaultCORS() *ServerBuilder
WithDefaultCORS enables the default CORS middleware.
func (*ServerBuilder) WithDefaultErrorHandling ¶
func (b *ServerBuilder) WithDefaultErrorHandling() *ServerBuilder
WithDefaultErrorHandling enables the default error handler middleware.
func (*ServerBuilder) WithDefaultLogging ¶
func (b *ServerBuilder) WithDefaultLogging(console ...bool) *ServerBuilder
WithDefaultLogging enables the default logging middleware. If console is not provided or is true, logs will be written to the console. If console is provided and is false, logs will not be written to the console. This function accepts an optional boolean parameter for backward compatibility.
func (*ServerBuilder) WithDefaultPort ¶ added in v0.0.6
func (b *ServerBuilder) WithDefaultPort() *ServerBuilder
WithDefaultPort sets the default port (8080) for the server. This method must be called if no port was provided in NewServerBuilder.
func (*ServerBuilder) WithDefaultRandomPort ¶ added in v0.1.3
func (b *ServerBuilder) WithDefaultRandomPort() *ServerBuilder
WithDefaultRandomPort sets a random available port for the server. This method automatically finds an available port between 8000 and 9000. This method must be called if no port was provided in NewServerBuilder.
func (*ServerBuilder) WithDefaultTimeout ¶
func (b *ServerBuilder) WithDefaultTimeout() *ServerBuilder
WithDefaultTimeout enables the default timeout middleware.
func (*ServerBuilder) WithErrorHandler ¶
func (b *ServerBuilder) WithErrorHandler(errorConfig core.ErrorHandlerConfig) *ServerBuilder
WithErrorHandler configures the error handler middleware with the specified configuration.
func (*ServerBuilder) WithFrameworkLogs ¶ added in v0.1.3
func (b *ServerBuilder) WithFrameworkLogs(enabled bool) *ServerBuilder
WithFrameworkLogs controls whether framework logs are shown. If enabled is true, logs about the framework, middleware, and routes will be printed to the console. If enabled is false, these logs will be suppressed. By default, framework logs are enabled.
func (*ServerBuilder) WithLogging ¶
func (b *ServerBuilder) WithLogging(customFields map[string]string) *ServerBuilder
WithLogging configures the logging middleware with the specified custom fields.
func (*ServerBuilder) WithNoMethod ¶
func (b *ServerBuilder) WithNoMethod(handlers ...core.HandlerFunc) *ServerBuilder
WithNoMethod configures custom handlers for 405 Method Not Allowed errors.
func (*ServerBuilder) WithNoRoute ¶
func (b *ServerBuilder) WithNoRoute(handlers ...core.HandlerFunc) *ServerBuilder
WithNoRoute configures custom handlers for 404 Not Found errors.
func (*ServerBuilder) WithRemoteLogging ¶
func (b *ServerBuilder) WithRemoteLogging(remoteURL string, customFields map[string]string) *ServerBuilder
WithRemoteLogging configures the logging middleware with the specified remote URL and custom fields.
func (*ServerBuilder) WithTimeout ¶
func (b *ServerBuilder) WithTimeout(timeout TimeoutConfig) *ServerBuilder
WithTimeout configures the timeout middleware with the specified timeout.
type ServiceUnavailableHttpError ¶
type ServiceUnavailableHttpError = errors.ServiceUnavailableHttpError
ServiceUnavailableHttpError represents a 503 Service Unavailable error.
type TimeoutConfig ¶
type TimeoutConfig = middleware.TimeoutConfig
TimeoutConfig holds configuration for the timeout middleware.
type UnauthorizedHttpError ¶
type UnauthorizedHttpError = errors.UnauthorizedHttpError
UnauthorizedHttpError represents a 401 Unauthorized error.
Directories
¶
Path | Synopsis |
---|---|
Package core provides the core interfaces and types for the HTTP server abstraction.
|
Package core provides the core interfaces and types for the HTTP server abstraction. |
gin
Package gin provides a Gin implementation of the HTTP server abstraction.
|
Package gin provides a Gin implementation of the HTTP server abstraction. |
middleware
Package middleware provides common middleware functionality for HTTP servers.
|
Package middleware provides common middleware functionality for HTTP servers. |
middleware/errors
Package errors provides error classes for HTTP status codes.
|
Package errors provides error classes for HTTP status codes. |
std
Package std provides a standard HTTP implementation of the HTTP server abstraction.
|
Package std provides a standard HTTP implementation of the HTTP server abstraction. |
examples
|
|
apikey
command
This example demonstrates how to use the API Key middleware
|
This example demonstrates how to use the API Key middleware |
auth
command
|
|
builder
command
This example demonstrates how to use the ServerBuilder to create a server with controllers and middleware.
|
This example demonstrates how to use the ServerBuilder to create a server with controllers and middleware. |
builder_default
command
This example demonstrates how to use the ServerBuilder with default middleware.
|
This example demonstrates how to use the ServerBuilder with default middleware. |
builder_noroute
command
This example demonstrates how to use the ServerBuilder with NoRoute and NoMethod handlers.
|
This example demonstrates how to use the ServerBuilder with NoRoute and NoMethod handlers. |
cors
command
This example demonstrates how to use the CORS middleware
|
This example demonstrates how to use the CORS middleware |
duprequest
command
This example demonstrates how to use the duplicate request prevention middleware
|
This example demonstrates how to use the duplicate request prevention middleware |
errors
command
This example demonstrates how to use the standardized error response structure
|
This example demonstrates how to use the standardized error response structure |
logging
command
This example demonstrates how to use the logging middleware
|
This example demonstrates how to use the logging middleware |
middleware
command
|
|
noroute
command
This example demonstrates how to use NoRoute and NoMethod handlers
|
This example demonstrates how to use NoRoute and NoMethod handlers |
noroute_custom
command
This example demonstrates how custom NoRoute and NoMethod handlers work
|
This example demonstrates how custom NoRoute and NoMethod handlers work |
noroute_default
command
This example demonstrates how the default NoRoute and NoMethod handlers are applied automatically
|
This example demonstrates how the default NoRoute and NoMethod handlers are applied automatically |
simple
command
This example demonstrates how to use the tenqube-go-http-server library to create a simple HTTP server.
|
This example demonstrates how to use the tenqube-go-http-server library to create a simple HTTP server. |
test_changes
command
This example demonstrates the use of the Stop method and WithDefaultLogging with console parameter
|
This example demonstrates the use of the Stop method and WithDefaultLogging with console parameter |
test_default_ports
command
This example demonstrates the use of WithDefaultPort and WithDefaultRandomPort
|
This example demonstrates the use of WithDefaultPort and WithDefaultRandomPort |
test_get_port
command
This example demonstrates the use of the GetPort method
|
This example demonstrates the use of the GetPort method |
test_port_config
command
This example demonstrates the use of the port configuration options in ServerBuilder
|
This example demonstrates the use of the port configuration options in ServerBuilder |
test_random_port
command
This example tests the modified findAvailablePort function
|
This example tests the modified findAvailablePort function |
test_withdefaultlogging
command
This example demonstrates the use of WithDefaultLogging with and without parameters
|
This example demonstrates the use of WithDefaultLogging with and without parameters |
test_withdefaultport
command
This example demonstrates the use of WithDefaultPort to automatically find an available port
|
This example demonstrates the use of WithDefaultPort to automatically find an available port |
timeout
command
This example demonstrates how to use the timeout middleware
|
This example demonstrates how to use the timeout middleware |