Documentation
¶
Index ¶
- Constants
- Variables
- func AESDecrypt(ciphertext []byte, key []byte) ([]byte, error)
- func AESEncrypt(plaintext []byte, key []byte) ([]byte, error)
- func ArrayContains(arr interface{}, val interface{}) bool
- func ConfigPath() string
- func GenerateRandomBytes(length int) []byte
- func IsCamelCase(str string) bool
- func IsChainCase(str string) bool
- func IsFlatCase(str string) bool
- func IsPascalCase(str string) bool
- func IsSnakeCase(str string) bool
- func LoadDotenvMap(embedFS embed.FS, logger *Logger) (map[string]string, error)
- func LoadEnv(embedFS embed.FS, logger *Logger) (map[string]string, error)
- func LoadEnvToConfig(config interface{}, embedFS embed.FS, logger *Logger) error
- func MasterKey(embedFS embed.FS) ([]byte, error)
- func MasterKeyPath() string
- func ModuleName() string
- func Plural(str string) string
- func Singular(str string) string
- func ToCamelCase(str string) string
- func ToChainCase(str string) string
- func ToFlatCase(str string) string
- func ToPascalCase(str string) string
- func ToSnakeCase(str string) string
- func UnloadDotenv(embedFS embed.FS, logger *Logger)
- type Config
- type Logger
Constants ¶
const ( // ConfigEncSuffix indicates the suffix that is appended to the encrypted // value in the config file. ConfigEncSuffix = " #encrypted" )
const (
// VERSION indicates the current semantic version.
VERSION = "0.1.0"
)
Variables ¶
var ( // ErrMissingMasterKey indicates the master key is not provided. ErrMissingMasterKey = errors.New("master key is missing") // ErrReadMasterKeyFile indicates there is a problem reading master key file. ErrReadMasterKeyFile = errors.New("failed to read master key file in config path") )
var ( // Build is the current build type for the application, can be "debug" or // "release". Please take note that this value will be updated to "release" // when running "go run . build" command. Build = "debug" )
Functions ¶
func AESDecrypt ¶
AESDecrypt decrypts a cipher text into a plain text using the key with AES-256 algorithm.
func AESEncrypt ¶
AESEncrypt encrypts a plaintext into a cipher text using the key with AES-256 algorithm.
func ArrayContains ¶
func ArrayContains(arr interface{}, val interface{}) bool
ArrayContains checks if a value is in a slice of the same type.
func GenerateRandomBytes ¶
GenerateRandomBytes generates random bytes of the specific length.
func IsChainCase ¶
IsChainCase checks if a string is a chain-case.
func IsPascalCase ¶
IsPascalCase checks if a string is a PascalCase.
func IsSnakeCase ¶
IsSnakeCase checks if a string is a snake_case.
func LoadDotenvMap ¶
LoadDotenvMap loads the dotenv config into a map.
func LoadEnv ¶
LoadEnv loads the dotenv config into environment variables and return them in a map. If the dotenv config variable exists in the environment variable, the environment variable will take precedence over it.
func LoadEnvToConfig ¶
LoadEnvToConfig loads the dotenv file with environment variables taking precedence into the interface.
func MasterKeyPath ¶
func MasterKeyPath() string
MasterKeyPath returns the current master key path for config values decryption.
func ToCamelCase ¶
ToCamelCase converts a string to camelCase style.
func ToChainCase ¶
ToChainCase converts a string to chain-case style.
func ToFlatCase ¶
ToFlatCase converts a string to flatcase style.
func ToPascalCase ¶
ToPascalCase converts a string to PascalCase style.
func ToSnakeCase ¶
ToSnakeCase converts a string to snake_case style.
func UnloadDotenv ¶
UnloadDotenv unset the dotenv config from the environment variables.
Types ¶
type Config ¶
type Config struct {
// AppEnv indicates the environment that the codebase is running on and it
// determines which config to use. By default, it is is "development" and
// its corresponding config is "configs/.env.development".
//
// Note: APP_ENV=test is used for unit tests.
AppEnv string `env:"APP_ENV" envDefault:"development"`
// AppName indicates the application name.
AppName string `env:"APP_NAME" envDefault:"appkit"`
// HTTPCSRFCookieDomain indicates which domain the CSRF cookie can be sent
// to. By default, it is "localhost".
HTTPCSRFCookieDomain string `env:"HTTP_CSRF_COOKIE_DOMAIN" envDefault:"localhost"`
// HTTPCSRFCookieName indicates the cookie name to use to store the CSRF
// token. By default, it is "_csrf_token".
HTTPCSRFCookieName string `env:"HTTP_CSRF_COOKIE_NAME" envDefault:"_csrf_token"`
// HTTPCSRFCookieHTTPOnly indicates if the CSRF cookie is only accessible via
// HTTP request and not Javascript `Document.cookie` API. By default, it is true.
HTTPCSRFCookieHTTPOnly bool `env:"HTTP_CSRF_COOKIE_HTTP_ONLY" envDefault:"true"`
// HTTPCSRFCookieMaxAge indicates how long till the CSRF cookie should expire.
// By default, it is 0 which gets deleted if the browser is closed.
HTTPCSRFCookieMaxAge int `env:"HTTP_CSRF_COOKIE_MAX_AGE" envDefault:"0"`
// HTTPCSRFCookiePath indicates which URL path the CSRF cookie can be sent to.
// By default, it is "/".
HTTPCSRFCookiePath string `env:"HTTP_CSRF_COOKIE_PATH" envDefault:"/"`
// HTTPCSRFCookieSameSite indicates if the CSRF cookie can be sent with
// cross-site requests. By default, it is 1.
//
// Available options:
// - SameSiteDefaultMode = 1
// - SameSiteLaxMode = 2
// - SameSiteStrictMode = 3
// - SameSiteNoneMode = 4
HTTPCSRFCookieSameSite http.SameSite `env:"HTTP_CSRF_COOKIE_SAME_SITE" envDefault:"1"`
// HTTPCSRFCookieSecure indicates if the session cookie can only be sent
// with HTTPS request. By default, it is false.
HTTPCSRFCookieSecure bool `env:"HTTP_CSRF_COOKIE_SECURE" envDefault:"false"`
// HTTPCSRFAuthenticityFieldName indicates the POST form field name that
// contains the authenticity token for CSRF check. By default, it is
// "authenticity_token".
HTTPCSRFAuthenticityFieldName string `env:"HTTP_CSRF_AUTHENTICITY_FIELD_NAME" envDefault:"authenticity_token"`
// HTTPCSRFRequestHeader indicates the HTTP header that contains the
// authenticity token for CSRF check. By default, it is "X-CSRF-Token".
HTTPCSRFRequestHeader string `env:"HTTP_CSRF_REQUEST_HEADER" envDefault:"X-CSRF-Token"`
// HTTPCSRFSecret indicates the secret to encrypt the CSRF cookie. By
// default, it is "".
HTTPCSRFSecret []byte `env:"HTTP_CSRF_SECRET" envDefault:""`
// HTTPCSRFTrustedOrigins indicates a set of origins (Referers) that are
// considered as trusted. This will allow cross-domain CSRF use-cases, e.g.
// where the front-end is served from a different domain than the API server
// to correctly pass a CSRF check.
//
// You should only provide origins you own or have full control over.
HTTPCSRFTrustedOrigins []string `env:"HTTP_CSRF_TRUSTED_ORIGINS" envDefault:"localhost"`
// HTTPHost indicates the host that the HTTP server is listening on.
HTTPHost string `env:"HTTP_HOST" envDefault:"localhost"`
// HTTPPort indicates the port that the HTTP server is listening on.
HTTPPort string `env:"HTTP_PORT" envDefault:"3000"`
// HTTPGracefulShutdownTimeout indicates how long to wait for the HTTP server
// to shut down so that any active connection is not interrupted by
// SIGTERM/SIGINT. By default, it is "30s".
HTTPGracefulShutdownTimeout time.Duration `env:"HTTP_GRACEFUL_SHUTDOWN_TIMEOUT" envDefault:"30s"`
// HTTPGzipCompressLevel indicates the compression level used to compress the
// HTTP response. By default, it is -1.
//
// Available options:
// - Default Compression = -1
// - No Compression = 0
// - Fastest Compression = 1
// - Best Compression = 9
HTTPGzipCompressLevel int `env:"HTTP_GZIP_COMPRESS_LEVEL" envDefault:"-1"`
// HTTPGzipContentTypes indicates which content types to compress. By
// default, it is "text/html,text/css,text/plain,text/javascript,application/javascript,application/javascript,application/x-javascript,application/json,application/atom+xml,application/rss+xml,image/svg+xml".
HTTPGzipContentTypes []string `` /* 231-byte string literal not displayed */
// HTTPLogFilterParameters indicates which query parameters in the URL to
// filter so that the sensitive information like password are masked in the
// HTTP request log. By default, it is "password".
HTTPLogFilterParameters []string `env:"HTTP_LOG_FILTER_PARAMETERS" envDefault:"password"`
// HTTPHealthCheckPath indicates the path to check if the HTTP server is healthy.
// This endpoint is a middleware that is designed to avoid redundant computing
// resource usage. By default, it is "/health_check".
//
// In general, if your server is running behind a load balancer, this endpoint
// will be served to inform the load balancer that the server is healthy and
// ready to receive HTTP requests.
HTTPHealthCheckPath string `env:"HTTP_HEALTH_CHECK_PATH" envDefault:"/health_check"`
// HTTPIdleTimeout is the maximum amount of time to wait for the next request
// when keep-alives are enabled. If HTTPIdleTimeout is zero, the value of
// HTTPReadTimeout is used. If both are zero, there is no timeout. By default,
// it is "75s".
HTTPIdleTimeout time.Duration `env:"HTTP_IDLE_TIMEOUT" envDefault:"75s"`
// HTTPMaxHeaderBytes controls 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,
// http.DefaultMaxHeaderBytes (1 << 20 which is 1 MB) is used.
HTTPMaxHeaderBytes int `env:"HTTP_MAX_HEADER_BYTES" envDefault:"0"`
// HTTPReadTimeout is the maximum duration for reading the entire request,
// including the body. Because HTTPReadTimeout does not let Handlers make
// per-request decisions on each request body's acceptable deadline or upload
// rate, most users will prefer to use HTTPReadHeaderTimeout. It is valid
// to use them both. By default, it is "60s".
HTTPReadTimeout time.Duration `env:"HTTP_READ_TIMEOUT" envDefault:"60s"`
// HTTPReadHeaderTimeout is 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
// HTTPReadHeaderTimeout is zero, the value of HTTPReadTimeout is used. If
// both are zero, there is no timeout. By default, it is "60s".
HTTPReadHeaderTimeout time.Duration `env:"HTTP_READ_HEADER_TIMEOUT" envDefault:"60s"`
// HTTPWriteTimeout is the maximum duration before timing out writes of the
// response. It is reset whenever a new request's header is read. Like
// HTTPReadTimeout, it does not let Handlers make decisions on a per-request
// basis. By default, it is "60s".
HTTPWriteTimeout time.Duration `env:"HTTP_WRITE_TIMEOUT" envDefault:"60s"`
// WorkerRedisSentinelAddrs indicates the Redis sentinel hosts to connect to.
// By default, it is "".
//
// Note: If this is configured to non-empty string, both WorkerRedisAddr or
// WorkerRedisURL will be ignored.
WorkerRedisSentinelAddrs []string `env:"WORKER_REDIS_SENTINEL_ADDRS" envDefault:""`
// WorkerRedisSentinelDB indicates the Redis DB to connect in the sentinel hosts.
// By default, it is 0.
WorkerRedisSentinelDB int `env:"WORKER_REDIS_SENTINEL_DB" envDefault:"0"`
// WorkerRedisSentinelMasterName indicates the Redis sentinel master name to
// connect to. By default, it is "".
WorkerRedisSentinelMasterName string `env:"WORKER_REDIS_SENTINEL_MASTER_NAME" envDefault:""`
// WorkerRedisSentinelPassword indicates the password used to connect to the
// sentinel hosts. By default, it is "".
WorkerRedisSentinelPassword string `env:"WORKER_REDIS_SENTINEL_PASSWORD" envDefault:""`
// WorkerRedisSentinelPoolSize indicates the connection pool size for the
// sentinel hosts. By default, it is 25.
WorkerRedisSentinelPoolSize int `env:"WORKER_REDIS_SENTINEL_POOL_SIZE" envDefault:"25"`
// WorkerRedisAddr indicates the Redis hostname to connect. By default, it is
// "localhost:6379".
WorkerRedisAddr string `env:"WORKER_REDIS_ADDR" envDefault:"localhost:6379"`
// WorkerRedisDB indicates the Redis DB to connect. By default, it is 0.
WorkerRedisDB int `env:"WORKER_REDIS_DB" envDefault:"0"`
// WorkerRedisPassword indicates the password used to connect to the Redis
// server. By default, it is "".
WorkerRedisPassword string `env:"WORKER_REDIS_PASSWORD" envDefault:""`
// WorkerRedisPoolSize indicates the connection pool size for the Redis
// server. By default, it is 25.
WorkerRedisPoolSize int `env:"WORKER_REDIS_POOL_SIZE" envDefault:"25"`
// WorkerRedisURL indicates the Redis URL to connect to. By default, it is "".
WorkerRedisURL string `env:"WORKER_REDIS_URL" envDefault:""`
// WorkerConcurrency indicates how many background jobs should be processed
// at one time. By default, it is 25.
WorkerConcurrency int `env:"WORKER_CONCURRENCY" envDefault:"25"`
// WorkerQueues indicates how many queues to process and the number followed
// is the priority. By default, it is "default:10".
//
// If the value is "critical:6,default:3,low:1", this will allow the worker
// to process 3 queues as below:
// - tasks in critical queue will be processed 60% of the time
// - tasks in default queue will be processed 30% of the time
// - tasks in low queue will be processed 10% of the time
WorkerQueues map[string]int `env:"WORKER_QUEUES" envDefault:"default:10"`
// WorkerStrictPriority indicates if the worker should strictly follow the
// priority to process the background jobs. By default, it is false.
//
// If the value is true, the queues with higher priority is always processed
// first, and queues with lower priority is processed only if all the other
// queues with higher priorities are empty.
WorkerStrictPriority bool `env:"WORKER_STRICT_PRIORITY" envDefault:"false"`
// WorkerGracefulShutdownTimeout indicates how long to wait for the worker
// to shut down so that any active job processing is not interrupted by
// SIGTERM/SIGINT. By default, it is "30s".
WorkerGracefulShutdownTimeout time.Duration `env:"WORKER_GRACEFUL_SHUTDOWN_TIMEOUT" envDefault:"30s"`
}
Config provides the configuration loaded via the dotenv file with decryption supported.