diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/config/config.go | 72 | ||||
| -rw-r--r-- | internal/consts/consts.go | 77 | ||||
| -rw-r--r-- | internal/db/postgres.go | 3 | ||||
| -rw-r--r-- | internal/db/service.go | 15 | ||||
| -rw-r--r-- | internal/log/log.go | 58 | 
5 files changed, 217 insertions, 8 deletions
| diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 000000000..9b6935a7c --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,72 @@ +/* +   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 config + +import ( +	"encoding/json" +	"fmt" +	"os" + +	"github.com/gotosocial/gotosocial/internal/db" +) + +// Config contains all the configuration needed to run gotosocial +type Config struct { +	DBConfig *db.Config `json:"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 +	if path != "" { +		var err error +		if config, err = loadFromFile(path); err != nil { +			return nil, fmt.Errorf("error creating config: %s", err) +		} +	} + +	return config, nil +} + +// loadFromFile takes a path to a .json file and attempts to load a Config object from it +func loadFromFile(path string) (*Config, error) { +	bytes, err := os.ReadFile(path) +	if err != nil { +		return nil, fmt.Errorf("could not read file at path %s: %s", path, err) +	} + +	config := &Config{} +	if err := json.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 +} + +// Flags is a wrapper for any type that can store keyed flags and give them back +type Flags interface { +	String(k string) string +	Int(k string) int +} diff --git a/internal/consts/consts.go b/internal/consts/consts.go new file mode 100644 index 000000000..92c574a41 --- /dev/null +++ b/internal/consts/consts.go @@ -0,0 +1,77 @@ +/* +   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 consts is where we shove any consts that don't really belong anywhere else in the code. +// Don't judge me. +package consts + +// FlagNames is used for storing the names of the various flags used for +// initializing and storing urfavecli flag variables. +type FlagNames struct { +	LogLevel        string +	ApplicationName string +	DbType          string +	DbAddress       string +	DbPort          string +	DbUser          string +	DbPassword      string +	DbDatabase      string +} + +// GetFlagNames returns a struct containing the names of the various flags used for +// initializing and storing urfavecli flag variables. +func GetFlagNames() FlagNames { +	return FlagNames{ +		LogLevel:        "log-level", +		ApplicationName: "application-name", +		DbType:          "db-type", +		DbAddress:       "db-address", +		DbPort:          "db-port", +		DbUser:          "db-users", +		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{ +		LogLevel:        "GTS_LOG_LEVEL", +		ApplicationName: "GTS_APPLICATION_NAME", +		DbType:          "GTS_DB_TYPE", +		DbAddress:       "GTS_DB_ADDRESS", +		DbPort:          "GTS_DB_PORT", +		DbUser:          "GTS_DB_USER", +		DbPassword:      "GTS_DB_PASSWORD", +		DbDatabase:      "GTS_DB_DATABASE", +	} +} diff --git a/internal/db/postgres.go b/internal/db/postgres.go index f4cf47406..14c8d3d7b 100644 --- a/internal/db/postgres.go +++ b/internal/db/postgres.go @@ -23,6 +23,7 @@ import (  	"errors"  	"fmt"  	"net/url" +	"strings"  	"time"  	"github.com/go-fed/activity/streams/vocab" @@ -95,7 +96,7 @@ func newPostgresService(ctx context.Context, config *Config, log *logrus.Entry)  // derivePGOptions takes an application config and returns either a ready-to-use *pg.Options  // with sensible defaults, or an error if it's not satisfied by the provided config.  func derivePGOptions(config *Config) (*pg.Options, error) { -	if config.Type != dbTypePostgres { +	if strings.ToUpper(config.Type) != dbTypePostgres {  		return nil, fmt.Errorf("expected db type of %s but got %s", dbTypePostgres, config.Type)  	} diff --git a/internal/db/service.go b/internal/db/service.go index 9a1d3ce2c..6c738606e 100644 --- a/internal/db/service.go +++ b/internal/db/service.go @@ -46,13 +46,14 @@ type Service interface {  // Config provides configuration options for the database connection  type Config struct { -	Type            string -	Address         string -	Port            int -	User            string -	Password        string -	Database        string -	ApplicationName string +	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"`  }  // NewService returns a new database service that satisfies the Service interface and, by extension, diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 000000000..65b820ce4 --- /dev/null +++ b/internal/log/log.go @@ -0,0 +1,58 @@ +/* +   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 log + +import ( +	"bytes" +	"os" + +	"github.com/sirupsen/logrus" +) + +// New returns a new logrus logger with the specified level, +// or an error if that level can't be parsed +func New(level string) (*logrus.Logger, error) { +	log := logrus.New() +	log.SetOutput(&outputSplitter{}) +	return setLogLevel(level, log) +} + +// outputSplitter implements the io.Writer interface for use with Logrus, and simply +// splits logs between stdout and stderr depending on their severity. +// See: https://github.com/sirupsen/logrus/issues/403#issuecomment-346437512 +type outputSplitter struct{} + +func (splitter *outputSplitter) Write(p []byte) (n int, err error) { +	if bytes.Contains(p, []byte("level=error")) { +		return os.Stderr.Write(p) +	} +	return os.Stdout.Write(p) +} + +// setLogLevel will try to set the logrus log level to the +// desired level specified by the user with the --log-level flag +func setLogLevel(level string, logger *logrus.Logger) (*logrus.Logger, error) { +	log := logrus.New() +	logLevel, err := logrus.ParseLevel(level) +	if err != nil { +		return nil, err +	} +	log.SetLevel(logLevel) +	return log, nil +} | 
