2023-05-03 02:18:29 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/config"
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
2023-05-09 17:35:00 +02:00
|
|
|
"golang.org/x/exp/slog"
|
2023-05-03 02:18:29 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Settings struct {
|
|
|
|
host string
|
|
|
|
port int
|
|
|
|
appName string
|
|
|
|
isLive bool
|
|
|
|
isDevel bool
|
|
|
|
sessionCookieName string
|
|
|
|
sessionCookieSecret string
|
|
|
|
assetsPath string
|
|
|
|
templatesPath string
|
|
|
|
version string
|
2023-05-05 23:10:56 +02:00
|
|
|
dbConnstring string
|
|
|
|
dbType string
|
2023-05-05 22:58:13 +02:00
|
|
|
dbIsSetUp bool
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new instance of the settings struct.
|
|
|
|
func New() *Settings {
|
|
|
|
return &Settings{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consolidate reconciles whatever values are set in config and via flags and
|
|
|
|
// sets it to one place that should be regarded as a single source of truth -
|
|
|
|
// the settings struct. Order of preference for values is (from higher to
|
|
|
|
// lower) as follows: flag -> Env var -> configuration file.
|
|
|
|
func (s *Settings) Consolidate(conf *config.Config, host *string, port *int, devel *bool, version string) {
|
2023-05-11 17:06:20 +02:00
|
|
|
log := *slogging.Logger() // have a local copy.
|
2023-05-09 17:35:00 +02:00
|
|
|
log.Logger = log.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "app/settings")),
|
|
|
|
)
|
2023-05-03 02:18:29 +02:00
|
|
|
|
|
|
|
log.Debug("starting to consolidate settings")
|
2023-05-09 17:35:00 +02:00
|
|
|
log.Debug("parsing config values")
|
2023-05-03 02:18:29 +02:00
|
|
|
|
|
|
|
s.SetHost(conf.Host)
|
|
|
|
s.SetPort(conf.Port)
|
|
|
|
s.SetAppName(conf.AppName)
|
|
|
|
s.SetIsLive(conf.LiveMode)
|
|
|
|
s.SetIsDevel(conf.DevelMode)
|
2023-05-05 19:31:43 +02:00
|
|
|
s.SetSessionCookieName(conf.Session.CookieName)
|
|
|
|
s.SetSessionCookieSecret(conf.Session.CookieSecret)
|
2023-05-03 02:18:29 +02:00
|
|
|
|
|
|
|
log.Debug("checking flag overrides")
|
|
|
|
|
|
|
|
overrideMsg := "overriding '%s' based on a flag: %+v"
|
|
|
|
|
2023-05-04 21:28:30 +02:00
|
|
|
if isFlagPassed("host") {
|
|
|
|
if h := *host; h != "unset" && h != conf.Host {
|
|
|
|
log.Debugf(overrideMsg, "host", h)
|
|
|
|
s.SetHost(h)
|
|
|
|
}
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 21:28:30 +02:00
|
|
|
if isFlagPassed("port") {
|
|
|
|
if p := *port; p > 0 && p < 65536 && p != conf.Port {
|
|
|
|
log.Debugf(overrideMsg, "port", p)
|
|
|
|
s.SetPort(p)
|
|
|
|
}
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-04 21:28:30 +02:00
|
|
|
if isFlagPassed("devel") {
|
|
|
|
if d := *devel; d != conf.DevelMode {
|
|
|
|
log.Debugf(overrideMsg, "develMode", d)
|
|
|
|
s.SetIsDevel(d)
|
|
|
|
}
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s.SetVersion(version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host returns the host.
|
|
|
|
func (s *Settings) Host() string {
|
|
|
|
return s.host
|
|
|
|
}
|
|
|
|
|
|
|
|
// Port returns the port.
|
|
|
|
func (s *Settings) Port() int {
|
|
|
|
return s.port
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppName returns the appName.
|
|
|
|
func (s *Settings) AppName() string {
|
|
|
|
return s.appName
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLive returns the value of isLive of the receiver.
|
|
|
|
func (s *Settings) IsLive() bool {
|
|
|
|
return s.isLive
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDevel returns the value of isDevel of the receiver.
|
|
|
|
func (s *Settings) IsDevel() bool {
|
|
|
|
return s.isDevel
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionCookieName returns the sessionCookieName.
|
|
|
|
func (s *Settings) SessionCookieName() string {
|
|
|
|
return s.sessionCookieName
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionCookieSecret returns the sessionCookieSecret.
|
|
|
|
func (s *Settings) SessionCookieSecret() string {
|
|
|
|
return s.sessionCookieSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssetsPath returns the assetsPath.
|
|
|
|
func (s *Settings) AssetsPath() string {
|
|
|
|
return s.assetsPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemplatesPath returns the templatesPath.
|
|
|
|
func (s *Settings) TemplatesPath() string {
|
|
|
|
return s.templatesPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the version.
|
|
|
|
func (s *Settings) Version() string {
|
|
|
|
return s.version
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:58:13 +02:00
|
|
|
// DbIsSetUp returns the dbIsSetUp.
|
|
|
|
func (s *Settings) DbIsSetUp() bool {
|
|
|
|
return s.dbIsSetUp
|
|
|
|
}
|
|
|
|
|
2023-05-05 23:10:56 +02:00
|
|
|
// DbConnstring returns the dbConnString.
|
|
|
|
func (s *Settings) DbConnstring() string {
|
|
|
|
return s.dbConnstring
|
|
|
|
}
|
|
|
|
|
|
|
|
// DbType returns the dbType.
|
|
|
|
func (s *Settings) DbType() string {
|
|
|
|
return s.dbType
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
// SetHost sets the host.
|
|
|
|
func (s *Settings) SetHost(host string) {
|
|
|
|
s.host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPort sets the port.
|
|
|
|
func (s *Settings) SetPort(port int) {
|
|
|
|
s.port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAppName sets the appName.
|
|
|
|
func (s *Settings) SetAppName(appName string) {
|
|
|
|
s.appName = appName
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIsLive sets the value of isLive of the receiver.
|
|
|
|
func (s *Settings) SetIsLive(live bool) {
|
|
|
|
s.isLive = live
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIsDevel sets the value of isDevel of the receiver.
|
|
|
|
func (s *Settings) SetIsDevel(devel bool) {
|
|
|
|
s.isDevel = devel
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionCookieName sets the sessionCookieName.
|
|
|
|
func (s *Settings) SetSessionCookieName(sessionCookieName string) {
|
|
|
|
s.sessionCookieName = sessionCookieName
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionCookieSecret sets the sessionCookieSecret.
|
|
|
|
func (s *Settings) SetSessionCookieSecret(sessionCookieSecret string) {
|
|
|
|
s.sessionCookieSecret = sessionCookieSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAssetsPath sets the assetsPath.
|
|
|
|
func (s *Settings) SetAssetsPath(assetsPath string) {
|
|
|
|
s.assetsPath = assetsPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTemplatesPath sets the templatesPath.
|
|
|
|
func (s *Settings) SetTemplatesPath(templatesPath string) {
|
|
|
|
s.templatesPath = templatesPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetVersion sets the version.
|
|
|
|
func (s *Settings) SetVersion(version string) {
|
|
|
|
s.version = version
|
|
|
|
}
|
2023-05-05 22:58:13 +02:00
|
|
|
|
2023-05-05 23:10:56 +02:00
|
|
|
// SetDbConnstring sets the dbConnString.
|
|
|
|
func (s *Settings) SetDbConnstring(connstring string) {
|
|
|
|
s.dbConnstring = connstring
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDbType sets the dbType.
|
|
|
|
func (s *Settings) SetDbType(dbType string) {
|
|
|
|
s.dbType = dbType
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:58:13 +02:00
|
|
|
// SetDbIsSetUp sets the dbIsSetUp.
|
|
|
|
func (s *Settings) SetDbIsSetUp(is bool) {
|
|
|
|
s.dbIsSetUp = is
|
|
|
|
}
|