further fun
This commit is contained in:
@ -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
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
|
66
internal/server/server.go
Normal file
66
internal/server/server.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user