gohttps

package module
v0.0.0-...-1251270 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 15 Imported by: 2 modules, 8 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gohttps

gohttps includes reusable functions to create HTTP servers in Go. The library aims to simplify HTTP server creation in Go by providing common patterns and middleware out of the box, with strong observability and configuration management features.

Key Features

  • Router creation with sensible defaults using Chi router.
  • Built-in middlewares: compression, decompression, CORS, request timeout, max body size.
  • Observability support: OpenTelemetry integration, Prometheus metrics
  • TLS support for HTTPS servers
  • Configuration-driven setup via YAML/JSON with JSON schema validation.

Documentation

Overview

Package gohttps includes reusable functions to create HTTP servers in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreatePrometheusServer

func CreatePrometheusServer(router *chi.Mux, currentPort int) (*http.Server, error)

CreatePrometheusServer creates a Prometheus HTTP server from config.

func ListenAndServe

func ListenAndServe(ctx context.Context, router *chi.Mux, config *ServerConfig) error

ListenAndServe listens and serves the HTTP server.

func NewRouter

func NewRouter(config *ServerConfig, logger *slog.Logger) *chi.Mux

NewRouter creates a new router with default middlewares.

Types

type CORSConfig

type CORSConfig struct {
	// AllowedOrigins is a list of origins a cross-domain request can be executed from.
	// If the special "*" value is present in the list, all origins will be allowed.
	// An origin may contain a wildcard (*) to replace 0 or more characters
	// (i.e.: http://*.domain.com). Usage of wildcards implies a small performance penalty.
	// Only one wildcard can be used per origin.
	// CORS is disabled if empty.
	AllowedOrigins []string `env:"SERVER_CORS_ALLOWED_ORIGINS" json:"allowedOrigins,omitempty" yaml:"allowedOrigins,omitempty"`
	// AllowedMethods is a list of methods the client is allowed to use with cross-domain requests.
	// Default value is simple methods (HEAD, GET and POST).
	AllowedMethods []string `env:"SERVER_CORS_ALLOWED_METHODS" json:"allowedMethods,omitempty" yaml:"allowedMethods,omitempty"`
	// AllowedHeaders is list of non simple headers the client is allowed to use with cross-domain requests.
	// If the special "*" value is present in the list, all headers will be allowed.
	// Default value is [] but "Origin" is always appended to the list.
	AllowedHeaders []string `env:"SERVER_CORS_ALLOWED_HEADERS" json:"allowedHeaders,omitempty" yaml:"allowedHeaders,omitempty"`
	// ExposedHeaders indicates which headers are safe to expose to the API of a CORS API specification
	ExposedHeaders []string `env:"SERVER_CORS_EXPOSED_HEADERS" json:"exposedHeaders,omitempty" yaml:"exposedHeaders,omitempty"`
	// MaxAge indicates how long (in seconds) the results of a preflight request can be cached
	MaxAge int `env:"SERVER_CORS_MAX_AGE" json:"maxAge,omitempty" yaml:"maxAge,omitempty" jsonschema:"minimum=0"`
	// AllowCredentials indicates whether the request can include user credentials like cookies,
	// HTTP authentication or client side SSL certificates.
	AllowCredentials bool `env:"SERVER_CORS_ALLOW_CREDENTIALS" json:"allowCredentials,omitempty" yaml:"allowCredentials"`
	// OptionsPassthrough instructs preflight to let other potential next handlers to process the OPTIONS method.
	// Turn this on if your application handles OPTIONS.
	OptionsPassthrough bool `env:"SERVER_CORS_OPTIONS_PASSTHROUGH" json:"optionsPassthrough,omitempty" yaml:"optionsPassthrough,omitempty"`
}

CORSConfig represents configurations of CORS.

type ServerConfig

type ServerConfig struct {
	// The port where the server is listening to.
	Port int `env:"PORT" json:"port,omitempty" yaml:"port,omitempty" jsonschema:"default=8080,minimum=1000,maximum=65535"`
	// Level of the logger.
	LogLevel string `` /* 135-byte string literal not displayed */
	// Default level which the server uses to compress response bodies.
	CompressionLevel *int `` /* 132-byte string literal not displayed */
	// The default timeout of every request. Return a 504 Gateway Timeout error to the client.
	RequestTimeout goutils.Duration `env:"SERVER_REQUEST_TIMEOUT" json:"requestTimeout,omitempty" yaml:"requestTimeout,omitempty"`
	// The maximum duration for reading the entire request, including the body.
	// A zero or negative value means there will be no timeout.
	ReadTimeout goutils.Duration `env:"SERVER_READ_TIMEOUT" json:"readTimeout,omitempty" yaml:"readTimeout,omitempty"`
	// The amount of time allowed to read request headers.
	// The connection's read deadline is reset after reading the headers and the Handler can decide what is considered too slow for the body.
	// If zero, the value of ReadTimeout is used. If negative, or if zero and ReadTimeout is zero or negative, there is no timeout.
	ReadHeaderTimeout goutils.Duration `env:"SERVER_READ_HEADER_TIMEOUT" json:"readHeaderTimeout,omitempty" yaml:"readHeaderTimeout,omitempty"`
	// The maximum duration before timing out writes of the response. It is reset whenever a new request's header is read.
	// Like ReadTimeout, it does not let Handlers make decisions on a per-request basis.
	// A zero or negative value means there will be no timeout.
	WriteTimeout goutils.Duration `env:"SERVER_WRITE_TIMEOUT" json:"writeTimeout,omitempty" yaml:"writeTimeout,omitempty"`
	// The maximum amount of time to wait for the next request when keep-alives are enabled.
	// If zero, the value of ReadTimeout is used.
	// If negative, or if zero and ReadTimeout is zero or negative, there is no timeout.
	IdleTimeout goutils.Duration `env:"SERVER_IDLE_TIMEOUT" json:"idleTimeout,omitempty" yaml:"idleTimeout,omitempty"`
	// The maximum number of bytes the server will read parsing the request header's keys and values, including the request line.
	// It does not limit the size of the request body. If zero, DefaultMaxHeaderBytes is used.
	MaxHeaderKilobytes int `env:"SERVER_MAX_HEADER_KILOBYTES" json:"maxHeaderKilobytes,omitempty" yaml:"maxHeaderKilobytes,omitempty"`
	// The maximum number of bytes the server will read parsing the request body.
	// A zero or negative value means there will be no limit.
	MaxBodyKilobytes int `env:"SERVER_MAX_BODY_KILOBYTES" json:"maxBodyKilobytes,omitempty" yaml:"maxBodyKilobytes,omitempty"`
	// The TLS certificate file to enable TLS connections.
	TLSCertFile string `env:"SERVER_TLS_CERT_FILE" json:"tlsCertFile,omitempty" yaml:"tlsCertFile,omitempty"`
	// The TLS key file to enable TLS connections.
	TLSKeyFile string `env:"SERVER_TLS_KEY_FILE" json:"tlsKeyFile,omitempty" yaml:"tlsKeyFile,omitempty"`
	// The configuration container to setup the CORS middleware.
	CORS *CORSConfig `json:"cors,omitempty" yaml:"cors,omitempty"`
	// The configuration container to setup the client IP middleware.
	ClientIP *middlewares.ClientIPConfig `json:"clientIp,omitempty" yaml:"clientIp,omitempty"`
}

ServerConfig holds information of required environment variables.

func (ServerConfig) GetLogLevel

func (sc ServerConfig) GetLogLevel() string

GetLogLevel returns the log level. Default is INFO.

func (ServerConfig) GetPort

func (sc ServerConfig) GetPort() int

GetPort returns the port of server. Default is 8080.

Directories

Path Synopsis
Package httputils defines common functions to manipulate the HTTP server.
Package httputils defines common functions to manipulate the HTTP server.
Package middlewares defines custom middlewares for the chi router.
Package middlewares defines custom middlewares for the chi router.

Jump to

Keyboard shortcuts

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