config

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package config loads and validates environment configuration for gin-kit runtime applications and converts it into runtime options.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadDotenv

func LoadDotenv(path string) error

LoadDotenv loads KEY=VALUE pairs from path into the process environment. Variables already present in the environment are never overridden, so the real environment always wins over .env values. A missing file is not an error.

Supported syntax: blank lines, '#' comment lines, an optional "export " prefix, and single- or double-quoted values. There is no variable expansion, inline-comment stripping, or multiline support; applications needing full dotenv semantics can load a dedicated library before calling Load.

Types

type Config

type Config struct {
	Address               string        // PORT, "8080" or ":8080"
	Environment           string        // APP_ENV, defaults to development
	DatabaseURL           string        // DATABASE_URL
	JWTSecret             []byte        // JWT_SECRET, length is enforced by auth.New
	SessionSecret         []byte        // SESSION_SECRET, length is enforced by session.Middleware
	OAuth                 OAuthConfig   // OAUTH_* provider credentials and browser redirects
	TrustedProxyCIDRs     []string      // TRUSTED_PROXY_CIDRS, comma separated
	CORSAllowedOrigins    []string      // CORS_ALLOWED_ORIGINS, comma separated
	RateLimitEnabled      bool          // RATE_LIMIT_ENABLED, defaults to true
	RateLimitPerMinute    int           // RATE_LIMIT_PER_MINUTE, defaults to 60
	RateLimitBurst        int           // RATE_LIMIT_BURST, 0 lets the runtime derive it
	MaxBodyBytes          int64         // MAX_BODY_BYTES, defaults to 1 MiB
	CacheDriver           string        // CACHE_DRIVER, "memory" (default) or "redis"
	QueueDriver           string        // QUEUE_DRIVER, "sync" (default) or "redis"
	QueueConcurrency      int           // QUEUE_CONCURRENCY, defaults to 10
	RedisURL              string        // REDIS_URL, e.g. redis://localhost:6379/0
	MetricsEnabled        bool          // METRICS_ENABLED, defaults to false
	PProfEnabled          bool          // PPROF_ENABLED, defaults to false; never expose publicly
	DocsEnabled           bool          // DOCS_ENABLED, defaults to true only in development
	DocsPath              string        // DOCS_PATH, defaults to /docs
	DocsSpecPath          string        // DOCS_SPEC_PATH, defaults to /openapi.json
	DocsTitle             string        // DOCS_TITLE, empty lets the application inject its name
	DocsVersion           string        // DOCS_VERSION, defaults to 0.1.0
	DocsDescription       string        // DOCS_DESCRIPTION
	DocsServers           []string      // DOCS_SERVERS, comma separated
	DocsBasicAuthUser     string        // DOCS_BASIC_AUTH_USERNAME
	DocsBasicAuthPass     string        // DOCS_BASIC_AUTH_PASSWORD
	DevToolsEnabled       bool          // DEVTOOLS_ENABLED, defaults to true only in development; refused elsewhere
	DevToolsPath          string        // DEVTOOLS_PATH, defaults to /_ginkit
	MailDriver            string        // MAIL_DRIVER, "log" (default) or "smtp"
	MailHost              string        // MAIL_HOST
	MailPort              int           // MAIL_PORT, defaults per encryption
	MailUsername          string        // MAIL_USERNAME
	MailPassword          string        // MAIL_PASSWORD
	MailEncryption        string        // MAIL_ENCRYPTION, none|tls|starttls (default starttls)
	MailFromAddress       string        // MAIL_FROM_ADDRESS
	MailFromName          string        // MAIL_FROM_NAME
	WhatsAppDriver        string        // WHATSAPP_DRIVER, "log" (default) or "cloud"
	WhatsAppAccessToken   string        // WHATSAPP_ACCESS_TOKEN
	WhatsAppPhoneNumberID string        // WHATSAPP_PHONE_NUMBER_ID
	WhatsAppAPIVersion    string        // WHATSAPP_API_VERSION, for example v25.0
	WhatsAppTimeout       time.Duration // WHATSAPP_TIMEOUT, defaults to 15s
	StorageDriver         string        // STORAGE_DRIVER, "local" (default) or "s3"
	StorageLocalRoot      string        // STORAGE_LOCAL_ROOT, defaults to ./storage
	StorageLocalURL       string        // STORAGE_LOCAL_BASE_URL
	S3Endpoint            string        // S3_ENDPOINT, host[:port] without scheme
	S3Region              string        // S3_REGION
	S3Bucket              string        // S3_BUCKET
	S3AccessKey           string        // S3_ACCESS_KEY
	S3SecretKey           string        // S3_SECRET_KEY
	S3UseSSL              bool          // S3_USE_SSL, defaults to true
	S3UsePathStyle        bool          // S3_USE_PATH_STYLE, defaults to false (MinIO needs true)
	S3PresignTTL          time.Duration // S3_PRESIGN_TTL, defaults to 15m
	S3PublicBaseURL       string        // S3_PUBLIC_BASE_URL
	ReadTimeout           time.Duration // READ_TIMEOUT, Go duration syntax such as 10s
	WriteTimeout          time.Duration // WRITE_TIMEOUT
	IdleTimeout           time.Duration // IDLE_TIMEOUT
	ShutdownTimeout       time.Duration // SHUTDOWN_TIMEOUT
}

Config is the environment-shaped application configuration. Code-level choices such as UI mode, database dialect, and readiness checks belong on the runtime.Options value produced by Options.

func Load

func Load() (Config, error)

Load reads the process environment, failing fast on malformed values and on unsafe defaults outside development. Unset variables use safe defaults.

func (Config) IsDevelopment

func (c Config) IsDevelopment() bool

IsDevelopment performs this package operation.

func (Config) MailOptions

func (c Config) MailOptions() mail.Options

MailOptions converts the mail configuration for mail.New. The mailer is constructed by application code, not by runtime.New.

func (Config) Options

func (c Config) Options() runtime.Options

Options converts the configuration into runtime options. Set code-level fields (UI, Database, Validator, Readiness, ...) on the result before calling runtime.New.

func (Config) StorageOptions

func (c Config) StorageOptions() storage.Options

StorageOptions converts the storage configuration for storage.New. The disk is constructed by application code, not by runtime.New.

func (Config) WhatsAppOptions

func (c Config) WhatsAppOptions() whatsapp.Options

WhatsAppOptions converts WhatsApp environment configuration for whatsapp.New. The client is constructed by application code, not runtime.New.

type OAuthConfig

type OAuthConfig struct {
	// Google contains Google OIDC credentials and callback configuration.
	Google OAuthProviderConfig
	// GitHub contains GitHub OAuth credentials and callback configuration.
	GitHub OAuthProviderConfig
	// SuccessRedirect is the relative browser path used after a successful flow.
	SuccessRedirect string
	// FailureRedirect is the relative browser path used after a failed flow.
	FailureRedirect string
}

OAuthConfig holds the generated application's supported social providers.

func (OAuthConfig) Validate

func (c OAuthConfig) Validate() error

Validate rejects partial provider credentials and unsafe browser redirects.

type OAuthProviderConfig

type OAuthProviderConfig struct {
	// ClientID is the provider-issued OAuth client identifier.
	ClientID string
	// ClientSecret is the provider-issued credential and must remain secret.
	ClientSecret string
	// RedirectURL is the exact callback URL registered with the provider.
	RedirectURL string
}

OAuthProviderConfig holds one provider's client credentials and exact registered callback URL.

Jump to

Keyboard shortcuts

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