further yak shaving
这个提交包含在:
72
internal/config/config.go
普通文件
72
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
|
||||
}
|
77
internal/consts/consts.go
普通文件
77
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",
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
58
internal/log/log.go
普通文件
58
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
|
||||
}
|
在新工单中引用
屏蔽一个用户