Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SendResponse ¶
func SendResponse(w http.ResponseWriter, statusCode int, headers http.Header, body io.ReadCloser) error
SendResponse sends an HTTP response with the provided status code, headers, and body content to the client. It streams the body data in chunks, ensures headers are set correctly, and handles client disconnection or errors during streaming.
Types ¶
type Config ¶
type Config struct { Host string // Server host address Cors *Cors // CORS configuration NoRouteHandler *func(w http.ResponseWriter, r *http.Request) // Custom handler for undefined routes }
Config holds the server configuration options, including the host address, CORS settings, and a custom handler for undefined routes.
type Cors ¶
type Cors struct { // AllowOrigins is a list of origins that are allowed to access the server's resources. // You can use "*" as a wildcard to allow all origins. // Example: ["https://example.com", "https://another-site.com"] or ["*"] to allow all. AllowOrigins []string // AllowMethods is a list of HTTP methods that are allowed for cross-origin requests. // You can use "*" as a wildcard to allow all methods. // Example: ["GET", "POST", "PUT"] or ["*"] to allow all methods. AllowMethods []string // AllowHeaders is a list of headers that the server allows in cross-origin requests. // You can use "*" as a wildcard to allow all headers. // Example: ["Content-Type", "Authorization", "X-Custom-Header"] or ["*"] to allow all headers. AllowHeaders []string // ExposeHeaders is a list of headers that the browser is allowed to access in the response // from the server during a CORS request. You can use "*" as a wildcard to allow access to all headers. // Example: ["X-Response-Time", "Content-Length"] or ["*"] to expose all headers. ExposeHeaders []string // AllowCredentials indicates whether credentials (like cookies, HTTP authentication, etc.) // are allowed in cross-origin requests. Note: If AllowOrigins is set to "*", this must be false // as credentials cannot be used with a wildcard origin. // Example: true to allow credentials to be sent with the request, false otherwise. AllowCredentials bool // MaxAge is the duration for which the results of a preflight request (OPTIONS) can be cached // by the browser. // Example: time.Hour() MaxAge time.Duration }
Cors defines the structure for configuring Cross-Origin Resource Sharing (CORS) settings. These settings control how a server responds to requests from different origins, specifying what is allowed in terms of origins, methods, headers, etc. For fields like AllowOrigins, AllowMethods, and AllowHeaders, setting the value to "*" will allow any value (e.g., any origin, any method, or any header).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router wraps the HTTP server, managing routing and lifecycle (graceful shutdown).
func NewRouter ¶
NewRouter creates and configures a new Router with HTTP/2, CORS support, and a custom 404 handler. It validates the provided config and listens for a context cancellation to gracefully shut down.
func (*Router) ListenAndServe ¶
ListenAndServe starts the HTTP server in a separate goroutine and returns a channel that captures any errors.
func (*Router) RegisterHandler ¶
func (r *Router) RegisterHandler(method, relativePath string, handler func(w http.ResponseWriter, r *http.Request)) error
RegisterHandler registers a handler for the specified HTTP method and path.