[chore] Global server configuration overhaul (#575)

* move config flag names and usage to config package, rewrite config package to use global Configuration{} struct

Signed-off-by: kim <grufwub@gmail.com>

* improved code comment

Signed-off-by: kim <grufwub@gmail.com>

* linter

Signed-off-by: kim <grufwub@gmail.com>

* fix unmarshaling

Signed-off-by: kim <grufwub@gmail.com>

* remove kim's custom go compiler changes

Signed-off-by: kim <grufwub@gmail.com>

* generate setter and flag-name functions, implement these in codebase

Signed-off-by: kim <grufwub@gmail.com>

* update deps

Signed-off-by: kim <grufwub@gmail.com>

* small change

Signed-off-by: kim <grufwub@gmail.com>

* appease the linter...

Signed-off-by: kim <grufwub@gmail.com>

* move configuration into ConfigState structure, ensure reloading to/from viper settings to keep in sync

Signed-off-by: kim <grufwub@gmail.com>

* lint

Signed-off-by: kim <grufwub@gmail.com>

* update code comments

Signed-off-by: kim <grufwub@gmail.com>

* fix merge issue

Signed-off-by: kim <grufwub@gmail.com>

* fix merge issue

Signed-off-by: kim <grufwub@gmail.com>

* improved version string (removes time + go version)

Signed-off-by: kim <grufwub@gmail.com>

* fix version string build to pass test script + consolidate logic in func

Signed-off-by: kim <grufwub@gmail.com>

* add license text, update config.Defaults comment

Signed-off-by: kim <grufwub@gmail.com>

* add license text to generated config helpers file

Signed-off-by: kim <grufwub@gmail.com>

* defer unlock on config.Set___(), to ensure unlocked on panic

Signed-off-by: kim <grufwub@gmail.com>

* make it more obvious which cmd flags are being attached

Signed-off-by: kim <grufwub@gmail.com>
This commit is contained in:
kim 2022-05-30 13:41:24 +01:00 committed by GitHub
commit 43ac0cdb9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
90 changed files with 2450 additions and 1125 deletions

View file

@ -19,45 +19,19 @@
package testrig
import (
"reflect"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/config"
)
// InitTestConfig resets + initializes the viper configuration with test defaults.
// InitTestConfig initializes viper configuration with test defaults.
func InitTestConfig() {
// reset viper to an empty state
viper.Reset()
// get the field names of config.Keys
keyFields := reflect.VisibleFields(reflect.TypeOf(config.Keys))
// get the field values of config.Keys
keyValues := reflect.ValueOf(config.Keys)
// get the field values of TestDefaults
testDefaults := reflect.ValueOf(TestDefaults)
// for each config field...
for _, field := range keyFields {
// the viper config key should be the value of the key
key, ok := keyValues.FieldByName(field.Name).Interface().(string)
if !ok {
panic("could not convert config.Keys value to string")
}
// the value should be the test default corresponding to the given fieldName
value := testDefaults.FieldByName(field.Name).Interface()
// actually set the value in viper -- this will override everything
viper.Set(key, value)
}
config.Config(func(cfg *config.Configuration) {
*cfg = TestDefaults
})
}
// TestDefaults returns a Values struct with values set that are suitable for local testing.
var TestDefaults = config.Values{
var TestDefaults = config.Configuration{
LogLevel: "trace",
LogDbQueries: true,
ApplicationName: "gotosocial",

View file

@ -24,7 +24,6 @@ import (
"strconv"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
@ -69,11 +68,15 @@ var testModels = []interface{}{
// value as the port instead.
func NewTestDB() db.DB {
if alternateAddress := os.Getenv("GTS_DB_ADDRESS"); alternateAddress != "" {
viper.Set(config.Keys.DbAddress, alternateAddress)
config.Config(func(cfg *config.Configuration) {
cfg.DbAddress = alternateAddress
})
}
if alternateDBType := os.Getenv("GTS_DB_TYPE"); alternateDBType != "" {
viper.Set(config.Keys.DbType, alternateDBType)
config.Config(func(cfg *config.Configuration) {
cfg.DbType = alternateDBType
})
}
if alternateDBPort := os.Getenv("GTS_DB_PORT"); alternateDBPort != "" {
@ -81,7 +84,9 @@ func NewTestDB() db.DB {
if err != nil {
panic(err)
}
viper.Set(config.Keys.DbPort, port)
config.Config(func(cfg *config.Configuration) {
cfg.DbPort = int(port)
})
}
testDB, err := bundb.NewBunDBService(context.Background())

View file

@ -19,7 +19,6 @@
package testrig
import (
"github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/email"
)
@ -30,7 +29,9 @@ import (
// the map, with email address of the recipient as the key, and the value as the
// parsed email message as it would have been sent.
func NewEmailSender(templateBaseDir string, sentEmails map[string]string) email.Sender {
viper.Set(config.Keys.WebTemplateBaseDir, templateBaseDir)
config.Config(func(cfg *config.Configuration) {
cfg.WebTemplateBaseDir = templateBaseDir
})
var sendCallback func(toAddress string, message string)

View file

@ -26,7 +26,6 @@ import (
"runtime"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/router"
@ -38,11 +37,13 @@ import (
// value as the template base directory instead.
func NewTestRouter(db db.DB) router.Router {
if alternativeTemplateBaseDir := os.Getenv("GTS_WEB_TEMPLATE_BASE_DIR"); alternativeTemplateBaseDir != "" {
viper.Set(config.Keys.WebTemplateBaseDir, alternativeTemplateBaseDir)
config.Config(func(cfg *config.Configuration) {
cfg.WebTemplateBaseDir = alternativeTemplateBaseDir
})
}
if alternativeBindAddress := os.Getenv("GTS_BIND_ADDRESS"); alternativeBindAddress != "" {
viper.Set(config.Keys.BindAddress, alternativeBindAddress)
config.SetBindAddress(alternativeBindAddress)
}
r, err := router.New(context.Background(), db)
@ -56,7 +57,7 @@ func NewTestRouter(db db.DB) router.Router {
func ConfigureTemplatesWithGin(engine *gin.Engine) {
router.LoadTemplateFunctions(engine)
templateBaseDir := viper.GetString(config.Keys.WebTemplateBaseDir)
templateBaseDir := config.GetWebTemplateBaseDir()
if !filepath.IsAbs(templateBaseDir) {
// https://stackoverflow.com/questions/31873396/is-it-possible-to-get-the-current-root-of-package-structure-as-a-string-in-golan