[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
parent ae5402ada6
commit 43ac0cdb9c
90 changed files with 2450 additions and 1125 deletions

View File

@@ -27,7 +27,6 @@ import (
"codeberg.org/gruf/go-debug"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"golang.org/x/crypto/acme/autocert"
@@ -70,16 +69,12 @@ func (r *router) AttachStaticFS(relativePath string, fs http.FileSystem) {
// Start starts the router nicely. It will serve two handlers if letsencrypt is enabled, and only the web/API handler if letsencrypt is not enabled.
func (r *router) Start() {
var (
keys = config.Keys
// listen is the server start function, by
// default pointing to regular HTTP listener,
// but updated to TLS if LetsEncrypt is enabled.
listen := r.srv.ListenAndServe
// listen is the server start function, by
// default pointing to regular HTTP listener,
// but updated to TLS if LetsEncrypt is enabled.
listen = r.srv.ListenAndServe
)
if viper.GetBool(keys.LetsEncryptEnabled) {
if config.GetLetsEncryptEnabled() {
// LetsEncrypt support is enabled
// Prepare an HTTPS-redirect handler for LetsEncrypt fallback
@@ -97,8 +92,8 @@ func (r *router) Start() {
srv := (*r.srv) //nolint
srv.Handler = r.certManager.HTTPHandler(redirect)
srv.Addr = fmt.Sprintf("%s:%d",
viper.GetString(keys.BindAddress),
viper.GetInt(keys.LetsEncryptPort),
config.GetBindAddress(),
config.GetLetsEncryptPort(),
)
// Start the LetsEncrypt autocert manager HTTP server.
@@ -144,8 +139,6 @@ func (r *router) Stop(ctx context.Context) error {
// The given DB is only used in the New function for parsing config values, and is not otherwise
// pinned to the router.
func New(ctx context.Context, db db.DB) (Router, error) {
keys := config.Keys
gin.SetMode(gin.ReleaseMode)
// create the actual engine here -- this is the core request routing handler for gts
@@ -158,7 +151,7 @@ func New(ctx context.Context, db db.DB) (Router, error) {
engine.MaxMultipartMemory = 8 << 20
// set up IP forwarding via x-forward-* headers.
trustedProxies := viper.GetStringSlice(keys.TrustedProxies)
trustedProxies := config.GetTrustedProxies()
if err := engine.SetTrustedProxies(trustedProxies); err != nil {
return nil, err
}
@@ -187,8 +180,8 @@ func New(ctx context.Context, db db.DB) (Router, error) {
}
// create the http server here, passing the gin engine as handler
bindAddress := viper.GetString(keys.BindAddress)
port := viper.GetInt(keys.Port)
bindAddress := config.GetBindAddress()
port := config.GetPort()
listen := fmt.Sprintf("%s:%d", bindAddress, port)
s := &http.Server{
Addr: listen,
@@ -201,14 +194,14 @@ func New(ctx context.Context, db db.DB) (Router, error) {
// We need to spawn the underlying server slightly differently depending on whether lets encrypt is enabled or not.
// In either case, the gin engine will still be used for routing requests.
leEnabled := viper.GetBool(keys.LetsEncryptEnabled)
leEnabled := config.GetLetsEncryptEnabled()
var m *autocert.Manager
if leEnabled {
// le IS enabled, so roll up an autocert manager for handling letsencrypt requests
host := viper.GetString(keys.Host)
leCertDir := viper.GetString(keys.LetsEncryptCertDir)
leEmailAddress := viper.GetString(keys.LetsEncryptEmailAddress)
host := config.GetHost()
leCertDir := config.GetLetsEncryptCertDir()
leEmailAddress := config.GetLetsEncryptEmailAddress()
m = &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(host),