gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 18d0685ef1947dea66a95a9d5e023ca5b4a39762
parent 54c4b8de203e023b63bffb65b40b9f15d1b4b102
Author: tsmethurst <tobi.smethurst@protonmail.com>
Date:   Wed,  3 Mar 2021 21:15:20 +0100

further fun

Diffstat:
Mcmd/gotosocial/main.go | 11++++++++---
Dcmd/server/server.go | 65-----------------------------------------------------------------
Aexample/config.yaml | 37+++++++++++++++++++++++++++++++++++++
Mgo.mod | 4++--
Mgo.sum | 2--
Minternal/config/config.go | 18++++++++++--------
Minternal/consts/consts.go | 30++++++++++--------------------
Minternal/db/service.go | 15+++++++--------
Ainternal/server/server.go | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dweb/template/index.tmpl | 5-----
10 files changed, 140 insertions(+), 113 deletions(-)

diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go @@ -21,7 +21,7 @@ package main 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/cmd/server/server.go b/cmd/server/server.go @@ -1,65 +0,0 @@ -/* - GoToSocial - Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. -*/ - -package server - -import ( - "context" - "fmt" - "os" - "os/signal" - "syscall" - - "github.com/gotosocial/gotosocial/internal/config" - "github.com/gotosocial/gotosocial/internal/db" - "github.com/gotosocial/gotosocial/internal/log" - "github.com/urfave/cli/v2" -) - -// Run starts the gotosocial server -func Run(c *cli.Context) error { - log, err := log.New(c.String("log-level")) - if err != nil { - return fmt.Errorf("error creating logger: %s", err) - } - - var gtsConfig *config.Config - if gtsConfig, err = config.New(c.String("config")); err != nil { - return fmt.Errorf("error creating config: %s", err) - } - - ctx := context.Background() - dbService, err := db.NewService(ctx, gtsConfig.DBConfig, log) - if err != nil { - return fmt.Errorf("error creating dbservice: %s", err) - } - - // catch shutdown signals from the operating system - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) - sig := <-sigs - log.Infof("received signal %s, shutting down", sig) - - // close down all running services in order - if err := dbService.Stop(ctx); err != nil { - return fmt.Errorf("error closing dbservice: %s", err) - } - - log.Info("done! exiting...") - return nil -} diff --git a/example/config.yaml 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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/internal/server/server.go b/internal/server/server.go @@ -0,0 +1,66 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package server + +import ( + "context" + "fmt" + "os" + "os/signal" + "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" +) + +// Run starts the gotosocial server +func Run(c *cli.Context) error { + log, err := log.New(c.String("log-level")) + if err != nil { + return fmt.Errorf("error creating logger: %s", err) + } + + gtsConfig, err := config.New(c.String(consts.GetFlagNames().ConfigPath)) + if err != nil { + return fmt.Errorf("error creating config: %s", err) + } + + ctx := context.Background() + dbService, err := db.NewService(ctx, gtsConfig.DBConfig, log) + if err != nil { + return fmt.Errorf("error creating dbservice: %s", err) + } + + // catch shutdown signals from the operating system + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) + sig := <-sigs + log.Infof("received signal %s, shutting down", sig) + + // close down all running services in order + if err := dbService.Stop(ctx); err != nil { + return fmt.Errorf("error closing dbservice: %s", err) + } + + log.Info("done! exiting...") + return nil +} diff --git a/web/template/index.tmpl b/web/template/index.tmpl @@ -1,5 +0,0 @@ -<html> - <h1> - {{ .title }} - </h1> -</html>