cfg

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2024 License: AGPL-3.0 Imports: 15 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// nolint:gochecknoglobals
	// DefaultConfig is the default config for the application.
	// It is used as a fallback if loading the proper config fails.
	// Loading it should always be accompanied by a warning.
	DefaultConfig = Config{
		DBConfig: DBConfig{
			Host:           "localhost",
			Port:           uint16(5432),
			Database:       "librate",
			User:           "postgres",
			Password:       "postgres",
			SSL:            "unknown",
			MigrationsPath: "/app/data/migrations",
		},
		Fiber: FiberConfig{
			Host: "localhost",
			Port: 3000,
			Perf: PerformanceConfig{
				Prefork: false,
			},
		},
		Secret:     uuid.Must(uuid.NewV7()).String(),
		LibrateEnv: "production",
	}
	// nolint:gochecknoglobals
	// TestConfig is a convenience config for testing, so that the test functions are terser, avoiding unnecessary repetition.
	TestConfig = Config{
		DBConfig: DBConfig{
			Host:               "0.0.0.0",
			Port:               uint16(5432),
			Database:           "librate_test",
			User:               "postgres",
			Password:           "postgres",
			SSL:                "disable",
			ExitAfterMigration: false,
			MigrationsPath:     "./migrations",
		},
		Logging: logging.Config{
			Level:  "debug",
			Target: "stdout",
			Format: "json",
			Caller: true,
			Timestamp: logging.TimestampConfig{
				Enabled: false,
				Format:  "2006-01-0215:04:05.000Z07:00",
			},
		},
		Redis: RedisConfig{
			Host:     "localhost",
			Port:     6379,
			Username: "",
			Password: "",
			CacheDB:  8,
			PowDB:    9,
			CsrfDB:   11,
		},
		Search: SearchConfig{
			Meili: MeiliConfig{
				Host:      "127.0.0.1",
				Port:      uint32(7700),
				MasterKey: "",
			},
			CouchDB: CouchDBConfig{
				Host:     "0.0.0.0",
				Port:     5984,
				User:     "librate",
				Password: "librate",
			},
		},
		Fiber: FiberConfig{
			Host: "0.0.0.0",
			Port: 3001,
			Perf: PerformanceConfig{
				Prefork:        false,
				ReduceMemUsage: false,
			},
			StaticDir: "./static",
			Security: WebSecurity{
				PowInterval:   1,
				PowDifficulty: 1,
			},
		},
		Secret:     "secret",
		LibrateEnv: "test",
	}
)

Functions

func FileExists added in v0.6.6

func FileExists(path string) bool

FileExists checks whether the config file exists. It is useful for the fallback mechanism of using default config

func LoadConfig

func LoadConfig() mo.Result[*Config]

LoadConfig loads the config from the config file, or falls back to defaults. It is used only when no --config flag is passed.

Types

type Config

type Config struct {
	DBConfig `json:"database,omitempty" yaml:"database" mapstructure:"database"`
	Fiber    FiberConfig `json:"fiber,omitempty" yaml:"fiber" mapstructure:"fiber"`
	// used to encrypt sessions database
	Secret string `json:"secret,omitempty" yaml:"secret" mapstructure:"secret" env:"LIBRATE_SECRET"`
	// default to production for security reasons
	// nolint: revive
	LibrateEnv string         `` /* 161-byte string literal not displayed */
	Redis      RedisConfig    `json:"redis,omitempty" yaml:"redis" mapstructure:"redis"`
	Logging    logging.Config `json:"logging,omitempty" yaml:"logging" mapstructure:"logging"`
	Keys       KeysConfig     `json:"keys,omitempty" yaml:"keys" mapstructure:"keys"`
	JWTSecret  string         `json:"jwtSecret,omitempty" yaml:"jwtSecret" mapstructure:"jwtSecret" env:"LIBRATE_JWT_SECRET"`
	GRPC       GrpcConfig     `json:"grpc,omitempty" yaml:"grpc" mapstructure:"grpc"`
	External   External       `json:"external,omitempty" yaml:"external" mapstructure:"external"`
	Search     SearchConfig   `json:"search,omitempty" yaml:"search" mapstructure:"search"`
}

Config is the struct that holds all the configuration for the application unfortunately, camel case must be used, instead the yaml parser will not work

func LoadFromFile added in v0.6.2

func LoadFromFile(path string) (conf *Config, err error)

LoadFromFile loads the config from the config file, or tries to call LoadConfig.

type CouchDBConfig added in v0.9.18

type CouchDBConfig struct {
	Host     string `yaml:"host,omitempty" default:"librate-search" env:"LIBRATE_SEARCH_HOST"`
	Port     int    `yaml:"port,omitempty" default:"5984" env:"LIBRATE_SEARCH_PORT"`
	User     string `yaml:"user,omitempty" default:"admin" env:"LIBRATE_SEARCH_USER"`
	Password string `yaml:"password,omitempty" default:"admin" env:"LIBRATE_SEARCH_PASSWORD"`
}

type DBConfig

type DBConfig struct {
	Host               string `yaml:"host" default:"localhost" env:"LIBRATE_DB_HOST"`
	Port               uint16 `yaml:"port" default:"5432" env:"LIBRATE_DB_PORT"`
	Database           string `yaml:"database" default:"librate" env:"LIBRATE_DB_NAME"`
	User               string `yaml:"user" default:"postgres" env:"LIBRATE_DB_USER"`
	Password           string `yaml:"password,omitempty" default:"postgres" env:"LIBRATE_DB_PASSWORD"`
	SSL                string `yaml:"SSL" default:"unknown" env:"LIBRATE_DB_SSL"`
	ExitAfterMigration bool   `yaml:"exitAfterMigration,omitempty" default:"false" env:"LIBRATE_EXIT_AFTER_MIGRATION"`
	RetryAttempts      int32  `yaml:"retryAttempts,omitempty" default:"10" env:"LIBRATE_DB_RETRY_ATTEMPTS"`
	MigrationsPath     string `yaml:"migrationsPath,omitempty" default:"/app/data/migrations" env:"LIBRATE_MIGRATIONS"`
}

nolint: musttag,revive // tagged in the struct above, can't break tags into multiline

type External added in v0.8.18

type External struct {
	// currently supported: json, id3, spotify (requires client ID and secret)
	ImportSources       []string `yaml:"import_sources,omitempty" default:"json,id3" env:"LIBRATE_IMPORT_SOURCES"`
	SpotifyClientID     string   `yaml:"spotify_client_id,omitempty" env:"SPOTIFY_CLIENT_ID"`
	SpotifyClientSecret string   `yaml:"spotify_client_secret,omitempty" env:"SPOTIFY_CLIENT_SECRET"`
}

type FiberConfig

type FiberConfig struct {
	DefaultLanguage string            `yaml:"defaultLanguage" default:"en-US" env:"LIBRATE_DEFAULT_LANGUAGE"`
	Host            string            `yaml:"host" default:"localhost" env:"LIBRATE_HOST"`
	Domain          string            `yaml:"domain" default:"lr.localhost" env:"DOMAIN"`
	Port            int               `yaml:"port" default:"3000" env:"LIBRATE_PORT"`
	StaticDir       string            `yaml:"staticDir" default:"./static" env:"LIBRATE_ASSETS"`
	FrontendDir     string            `yaml:"frontendDir" default:"./fe/build" env:"LIBRATE_FRONTEND"`
	Thumbnailing    ThumbnailConfig   `yaml:"thumbnailing" default:"{namespaces: [{name: album_cover, size: {Width: 500, Height: 500}}]"`
	Security        WebSecurity       `yaml:"security"`
	Perf            PerformanceConfig `yaml:"performance" default:"{reduceMemUsage: false, prefork: false}"`
}

refer to https://docs.gofiber.io/api/fiber#config

type GrpcConfig added in v0.8.2

type GrpcConfig struct {
	Host            string `yaml:"host" default:"localhost" env:"LIBRATE_GRPC_HOST"`
	Port            int    `yaml:"port" default:"3030" env:"LIBRATE_GRPC_PORT"`
	ShutdownTimeout int    `yaml:"shutdownTimeout" default:"10" env:"LIBRATE_SHUTDOWN_TIMEOUT"`
}

type KeysConfig added in v0.7.0

type KeysConfig struct {
	Private string `yaml:"private" default:"./keys/private.pem" env:"LIBRATE_PRIVATE_KEY"`
	Public  string `yaml:"public" default:"./keys/public.pem" env:"LIBRATE_PUBLIC_KEY"`
}

KeysConfig defines the location of keys used for TLS

type MeiliConfig added in v0.9.18

type MeiliConfig struct {
	Host string `yaml:"host,omitempty" default:"127.0.0.1" env:"MEILI_HOST"`
	// protobufs don't support smaller int sizes
	Port      uint32 `yaml:"port,omitempty" default:"7700" env:"MEILI_PORT"`
	MasterKey string `yaml:"masterKey,omitempty" env:"MEILI_MASTER_KEY"`
}

type PerformanceConfig added in v0.10.0

type PerformanceConfig struct {
	// whether to reduce memory usage at the cost of CPU usage
	ReduceMemUsage bool `yaml:"reduceMemUsage" default:"false" env:"LIBRATE_REDUCE_MEM"`
	// whether to use preforking (a little bit similar to BEAM processes)
	Prefork bool `yaml:"prefork" default:"false" env:"LIBRATE_PREFORK"`
}

type RedisConfig

type RedisConfig struct {
	Host string `yaml:"host,omitempty" default:"localhost" env:"LIBRATE_REDIS_HOST"`
	Port int    `yaml:"port,omitempty" default:"6379" env:"LIBRATE_REDIS_PORT"`
	// how many errors can occur during scan of SQL DB into cache before the process is stopped
	Username string `yaml:"username,omitempty" default:"" env:"LIBRATE_REDIS_USERNAME"`
	Password string `yaml:"password,omitempty" default:"" env:"LIBRATE_REDIS_PASSWORD"`
	CacheDB  int    `yaml:"cacheDb,omitempty" default:"0" env:"LIBRATE_CACHE_DB"`
	CsrfDB   int    `yaml:"csrfDb,omitempty" default:"2" env:"LIBRATE_CSRF_DB"`
	PowDB    int    `yaml:"powDb,omitempty" default:"3" env:"LIBRATE_POW_DB"`
	PagesDB  int    `yaml:"pagesDb,omitempty" default:"4" env:"LIBRATE_PAGES_DB"`
	SearchDB int    `yaml:"searchDb,omitempty" default:"5" env:"LIBRATE_SEARCH_CACHE"`
}

type SearchConfig added in v0.9.17

type SearchConfig struct {
	Meili MeiliConfig `yaml:"meili,omitempty"`
	// CouchDB config must be set
	CouchDB CouchDBConfig `yaml:"couchdb"`
}

SearchConfig holds the configuration for the search service, wrapping MeiliSearch and CouchDB

type ThumbnailConfig added in v0.8.19

type ThumbnailConfig struct {
	// Target namespaces can be grouped together or defined individually
	TargetNS []ThumbnailerNamespace `yaml:"namespaces" default:"[{names: {album_cover, film_poster} size: {Width: 500, Height: 500}}]"`
}

FIXME: currently this cannot be reliably configured via environment variables

type ThumbnailerNamespace added in v0.8.19

type ThumbnailerNamespace struct {
	Names   []string         `yaml:"names" validate:"required,oneof='album_cover' 'film_poster' 'profile' 'all'"`
	MaxSize thumbnailer.Dims `yaml:"size" default:"{Width: 500, Height: 500}"`
}

type ValidationError added in v0.8.5

type ValidationError struct {
	FailedField string
	Tag         string
	Value       interface{}
}

func Validate added in v0.8.5

func Validate(conf *Config, validate *validator.Validate) (errs []ValidationError)

type WebSecurity added in v0.10.0

type WebSecurity struct {
	// interval in seconds for the proof of work challenge
	// default is 5 minutes
	PowInterval int `yaml:"powInterval" default:"300" env:"POW_INTERVAL"`
	// difficulty of the proof of work challenge (number of leading zeros
	// in the SHA256 hash of the challenge)
	PowDifficulty int `yaml:"powDifficulty" default:"30000" env:"POW_DIFFICULTY"`
	// whether to use TLS (not needed if behind a reverse proxy)
	TLS bool `yaml:"tls" default:"false" env:"LIBRATE_TLS"`
	// maximum size of uploads in bytes (default is 4MB)
	MaxUploadSize int64 `yaml:"maxUploadSize" default:"4194304" env:"LIBRATE_MAX_SIZE"`
	// request timeout in seconds for all requests (default is 10)
	RequestTimeout int `yaml:"requestTimeout" default:"10" env:"LIBRATE_REQUEST_TIMEOUT"`
}

Jump to

Keyboard shortcuts

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