From 18d0685ef1947dea66a95a9d5e023ca5b4a39762 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Wed, 3 Mar 2021 21:15:20 +0100 Subject: [PATCH] further fun --- cmd/gotosocial/main.go | 11 ++++++--- example/config.yaml | 37 ++++++++++++++++++++++++++++++ go.mod | 4 ++-- go.sum | 2 -- internal/config/config.go | 18 ++++++++------- internal/consts/consts.go | 30 ++++++++---------------- internal/db/service.go | 15 ++++++------ {cmd => internal}/server/server.go | 5 ++-- web/template/index.tmpl | 5 ---- 9 files changed, 77 insertions(+), 50 deletions(-) create mode 100644 example/config.yaml rename {cmd => internal}/server/server.go (92%) delete mode 100644 web/template/index.tmpl diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go index ffb7b9b8d..26b9d49b6 100644 --- a/cmd/gotosocial/main.go +++ b/cmd/gotosocial/main.go @@ -21,7 +21,7 @@ import ( "os" - "github.com/gotosocial/gotosocial/cmd/server" + "github.com/gotosocial/gotosocial/internal/server" "github.com/gotosocial/gotosocial/internal/consts" "github.com/sirupsen/logrus" @@ -32,6 +32,7 @@ func main() { flagNames := consts.GetFlagNames() envNames := consts.GetEnvNames() app := &cli.App{ + Usage: "a fediverse social media server", Flags: []cli.Flag{ // GENERAL FLAGS &cli.StringFlag{ @@ -47,6 +48,12 @@ func main() { EnvVars: []string{envNames.ApplicationName}, Hidden: true, }, + &cli.StringFlag{ + Name: flagNames.ConfigPath, + Usage: "Path to a yaml file containing gotosocial configuration. Values set in this file will be overwritten by values set as env vars or arguments", + Value: "", + EnvVars: []string{envNames.ConfigPath}, + }, // DATABASE FLAGS &cli.StringFlag{ @@ -76,9 +83,7 @@ func main() { &cli.StringFlag{ Name: flagNames.DbPassword, Usage: "Database password", - Value: "postgres", EnvVars: []string{envNames.DbPassword}, - FilePath: "./dbpass", }, &cli.StringFlag{ Name: flagNames.DbDatabase, diff --git a/example/config.yaml b/example/config.yaml new file mode 100644 index 000000000..bc87ecdf4 --- /dev/null +++ b/example/config.yaml @@ -0,0 +1,37 @@ +# String. Log level to use throughout the application. Must be lower-case. +# Options: ["debug","info","warn","error","fatal"] +# Default: "info" +logLevel: "info" + +# String. Application name to use internally. +# Examples: ["My Application","gotosocial"] +# Default: "gotosocial" +applicationName: "gotosocial" + +# Config pertaining to the Gotosocial database connection +db: + # String. Database type. + # Options: ["postgres"] + # Default: "postgres" + type: "postgres" + + # String. Database address. Can be either an ipv4 address or a hostname. + # Examples: ["localhost","my.db.host","127.0.0.1","192.111.39.110"] + # Default: "localhost" + address: "127.0.0.1" + + # Int. Port for database connection. + # Examples: [5432, 1234, 6969] + # Default: 5432 + port: 5432 + + # String. Username for the database connection. + # Examples: ["mydbuser","postgres","gotosocial"] + # Default: "postgres" + user: "postgres" + + # REQUIRED + # String. Password to use for the database connection + # Examples: ["password123","verysafepassword","postgres"] + # Default: "" + password: "" diff --git a/go.mod b/go.mod index edf69c9f6..fbc657cd8 100644 --- a/go.mod +++ b/go.mod @@ -9,16 +9,16 @@ require ( github.com/golang/protobuf v1.4.3 // indirect github.com/google/go-cmp v0.5.4 // indirect github.com/jinzhu/inflection v1.0.0 // indirect - github.com/namsral/flag v1.7.4-pre // indirect github.com/onsi/ginkgo v1.15.0 // indirect github.com/onsi/gomega v1.10.5 // indirect github.com/sirupsen/logrus v1.8.0 github.com/stretchr/testify v1.7.0 // indirect - github.com/urfave/cli/v2 v2.3.0 // indirect + github.com/urfave/cli/v2 v2.3.0 golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b // indirect google.golang.org/protobuf v1.25.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v2 v2.3.0 mellium.im/sasl v0.2.1 // indirect ) diff --git a/go.sum b/go.sum index 0365c4fe0..b3350dc78 100644 --- a/go.sum +++ b/go.sum @@ -77,8 +77,6 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs= -github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/internal/config/config.go b/internal/config/config.go index 9b6935a7c..1f31eefd6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -19,22 +19,24 @@ package config import ( - "encoding/json" "fmt" "os" "github.com/gotosocial/gotosocial/internal/db" + "gopkg.in/yaml.v2" ) // Config contains all the configuration needed to run gotosocial type Config struct { - DBConfig *db.Config `json:"db,omitempty"` + LogLevel string `yaml:"logLevel"` + ApplicationName string `yaml:"applicationName,omitempty"` + DBConfig *db.Config `yaml:"db,omitempty"` } // New returns a new config, or an error if something goes amiss. // The path parameter is optional, for loading a configuration json from the given path. func New(path string) (*Config, error) { - var config *Config + config := &Config{} if path != "" { var err error if config, err = loadFromFile(path); err != nil { @@ -45,7 +47,7 @@ func New(path string) (*Config, error) { return config, nil } -// loadFromFile takes a path to a .json file and attempts to load a Config object from it +// loadFromFile takes a path to a yaml file and attempts to load a Config object from it func loadFromFile(path string) (*Config, error) { bytes, err := os.ReadFile(path) if err != nil { @@ -53,16 +55,16 @@ func loadFromFile(path string) (*Config, error) { } config := &Config{} - if err := json.Unmarshal(bytes, config); err != nil { + if err := yaml.Unmarshal(bytes, config); err != nil { return nil, fmt.Errorf("could not unmarshal file at path %s: %s", path, err) } return config, nil } -// WithFlags returns a copy of this config object with flags set using the provided flags object -func (c *Config) WithFlags(f Flags) *Config { - return c +// ParseFlags sets flags on the config using the provided Flags object +func (c *Config) ParseFlags(f Flags) { + } // Flags is a wrapper for any type that can store keyed flags and give them back diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 92c574a41..8a22e5825 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -20,11 +20,12 @@ // Don't judge me. package consts -// FlagNames is used for storing the names of the various flags used for +// Flags is used for storing the names of the various flags used for // initializing and storing urfavecli flag variables. -type FlagNames struct { +type Flags struct { LogLevel string ApplicationName string + ConfigPath string DbType string DbAddress string DbPort string @@ -35,38 +36,27 @@ type FlagNames struct { // GetFlagNames returns a struct containing the names of the various flags used for // initializing and storing urfavecli flag variables. -func GetFlagNames() FlagNames { - return FlagNames{ +func GetFlagNames() Flags { + return Flags{ LogLevel: "log-level", ApplicationName: "application-name", + ConfigPath: "config-path", DbType: "db-type", DbAddress: "db-address", DbPort: "db-port", - DbUser: "db-users", + DbUser: "db-user", DbPassword: "db-password", DbDatabase: "db-database", } } -// EnvNames is used for storing the environment variable keys used for -// initializing and storing urfavecli flag variables. -type EnvNames struct { - LogLevel string - ApplicationName string - DbType string - DbAddress string - DbPort string - DbUser string - DbPassword string - DbDatabase string -} - // GetEnvNames returns a struct containing the names of the environment variable keys used for // initializing and storing urfavecli flag variables. -func GetEnvNames() FlagNames { - return FlagNames{ +func GetEnvNames() Flags { + return Flags{ LogLevel: "GTS_LOG_LEVEL", ApplicationName: "GTS_APPLICATION_NAME", + ConfigPath: "GTS_CONFIG_PATH", DbType: "GTS_DB_TYPE", DbAddress: "GTS_DB_ADDRESS", DbPort: "GTS_DB_PORT", diff --git a/internal/db/service.go b/internal/db/service.go index 6c738606e..4449a984f 100644 --- a/internal/db/service.go +++ b/internal/db/service.go @@ -46,14 +46,13 @@ type Service interface { // Config provides configuration options for the database connection type Config struct { - Type string `json:"type,omitempty"` - Address string `json:"address,omitempty"` - Port int `json:"port,omitempty"` - User string `json:"user,omitempty"` - Password string `json:"password,omitempty"` - PasswordFile string `json:"passwordFile,omitempty"` - Database string `json:"database,omitempty"` - ApplicationName string `json:"applicationName,omitempty"` + Type string `yaml:"type,omitempty"` + Address string `yaml:"address,omitempty"` + Port int `yaml:"port,omitempty"` + User string `yaml:"user,omitempty"` + Password string `yaml:"password,omitempty"` + Database string `yaml:"database,omitempty"` + ApplicationName string `yaml:"applicationName,omitempty"` } // NewService returns a new database service that satisfies the Service interface and, by extension, diff --git a/cmd/server/server.go b/internal/server/server.go similarity index 92% rename from cmd/server/server.go rename to internal/server/server.go index bd16e1f84..6457f55dd 100644 --- a/cmd/server/server.go +++ b/internal/server/server.go @@ -26,6 +26,7 @@ "syscall" "github.com/gotosocial/gotosocial/internal/config" + "github.com/gotosocial/gotosocial/internal/consts" "github.com/gotosocial/gotosocial/internal/db" "github.com/gotosocial/gotosocial/internal/log" "github.com/urfave/cli/v2" @@ -38,8 +39,8 @@ func Run(c *cli.Context) error { return fmt.Errorf("error creating logger: %s", err) } - var gtsConfig *config.Config - if gtsConfig, err = config.New(c.String("config")); err != nil { + gtsConfig, err := config.New(c.String(consts.GetFlagNames().ConfigPath)) + if err != nil { return fmt.Errorf("error creating config: %s", err) } diff --git a/web/template/index.tmpl b/web/template/index.tmpl deleted file mode 100644 index a1656bf72..000000000 --- a/web/template/index.tmpl +++ /dev/null @@ -1,5 +0,0 @@ - -

- {{ .title }} -

-