server

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSAllowAll

func CORSAllowAll(c *gin.Context)

CORSAllowAll sets up the CORS headers to allow all origins, methods and headers.

func CloudflareIPCountry added in v1.0.1

func CloudflareIPCountry(c *gin.Context) (string, bool)

CloudflareIPCountry gets the country code from the Cloudflare header "CF-IPCountry", or returns "", false if the header is not set. The country code is a two-letter ISO 3166-1 alpha-2 code.

func CloudflareOriginIP added in v1.0.1

func CloudflareOriginIP(c *gin.Context) string

CloudflareOriginIP gets the origin IP from the Cloudflare header, or falls back to the client IP if the header is not set.

func GetInt64Param

func GetInt64Param(c *gin.Context, key string, maxVal, defaultVal int64) (int64, error)

GetInt64Param gets the int64 parameter from the gin context, and returns the value or the default value if the parameter is not set.

func GetInt64Query

func GetInt64Query(c *gin.Context, key string, maxVal, defaultVal int64) (int64, error)

GetInt64Query gets the int64 query parameter from the gin context, and returns the value or the default value if the parameter is not set.

func GetPagenation

func GetPagenation(c *gin.Context, maxPageSize, defaultPageSize int64) (int64, int64, error)

GetPagenation takes the query parameters "page" and "pageSize" from the gin context, and returns the values or an error if the parameters are invalid. Defaults page to 0, and pageSize to defaultPageSize if not set. Returns page, pageSize, error

func GetUUIDParam

func GetUUIDParam(c *gin.Context, key string) (uuid.UUID, error)

GetUUIDParam gets the uuid parameter from the gin context Param call, and parses then returns the value or an error if the parameter is not set or invalid.

func ListenWithGracefulShutdown

func ListenWithGracefulShutdown(ctx context.Context, log logrus.Ext1FieldLogger, router *gin.Engine, conf Config) error

ListenWithGracefulShutdown listens on the given address with the given gin router, and will shutdown gracefully when a SIGINT or SIGTERM signal is received. Also setups up a health check endpoint on the given path, which will be unhealthy when the server is shutting down. This allows for graceful shutdown of the server in a Kubernetes environment, where the health check is used to determine if the server is ready to accept traffic.

Types

type Config

type Config struct {
	ReadTimeout     time.Duration `env:"READ_TIMEOUT" env-default:"5s"`
	ShutdownTimeout time.Duration `env:"SHUTDOWN_TIMEOUT" env-default:"15s"`
	Listen          string        `env:"LISTEN" env-default:":8080"`
	HealthCheckPath string        `env:"HEALTH_CHECK_PATH" env-default:"/health"`
}

func LoadServerConfigFromEnv

func LoadServerConfigFromEnv() (Config, error)

type HealthCheck

type HealthCheck struct {
	IsUnhealthy *atomic.Bool
}

func NewHealthCheck

func NewHealthCheck() *HealthCheck

func (*HealthCheck) Health

func (h *HealthCheck) Health(c *gin.Context)

func (*HealthCheck) SetUnhealthy

func (h *HealthCheck) SetUnhealthy()

Jump to

Keyboard shortcuts

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