Albirew/nyaa-pantsu
Albirew
/
nyaa-pantsu
Archivé
1
0
Bifurcation 0

Fixed parameters through command line for DB

config is now a singleton
can use config.GetInstance() to know the context
Cette révision appartient à :
akuma06 2017-05-06 17:37:56 +02:00
Parent 428151e04a
révision a456a5f9f2
3 fichiers modifiés avec 35 ajouts et 30 suppressions

Voir le fichier

@ -8,7 +8,8 @@ import (
"fmt"
"io"
"os"
)
"sync"
)
type Config struct {
Host string `json: "host"`
@ -20,7 +21,7 @@ type Config struct {
}
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"}
var PrintDefaults *bool
var allowedDatabaseTypes = map[string]bool{
"sqlite3": true,
"postgres": true,
@ -28,39 +29,45 @@ var allowedDatabaseTypes = map[string]bool{
"mssql": true,
}
func NewConfig() *Config {
var config Config
config.Host = Defaults.Host
config.Port = Defaults.Port
config.DBType = Defaults.DBType
config.DBParams = Defaults.DBParams
return &config
var instance *Config
var once sync.Once
func GetInstance() *Config {
once.Do(func() {
instance = &Config{}
instance.Host = Defaults.Host
instance.Port = Defaults.Port
instance.DBType = Defaults.DBType
instance.DBParams = Defaults.DBParams
instance.BindFlags()
})
return instance
}
type processFlags func() error
func (config *Config) BindFlags() processFlags {
func (config *Config) BindFlags() error {
// This function returns a function which is to be used after
// flag.Parse to check and copy the flags' values to the Config instance.
// flag.Parse to check and copy the flags' values to the config instance.
conf_file := flag.String("conf", "", "path to the configuration file")
db_type := flag.String("dbtype", Defaults.DBType, "database backend")
host := flag.String("host", Defaults.Host, "binding address of the server")
port := flag.Int("port", Defaults.Port, "port of the server")
db_params := flag.String("dbparams", Defaults.DBParams, "parameters to open the database (see Gorm's doc)")
return func() error {
err := config.HandleConfFileFlag(*conf_file)
if err != nil {
return err
}
// You can override fields in the config file with flags.
config.Host = *host
config.Port = *port
config.DBParams = *db_params
err = config.SetDBType(*db_type)
PrintDefaults = flag.Bool("print-defaults", false, "print the default configuration file on stdout")
flag.Parse()
err := config.HandleConfFileFlag(*conf_file)
if err != nil {
return err
}
// You can override fields in the config file with flags.
config.Host = *host
config.Port = *port
config.DBParams = *db_params
err = config.SetDBType(*db_type)
return err
}
func (config *Config) HandleConfFileFlag(path string) error {

Voir le fichier

@ -13,7 +13,9 @@ var ORM, Errs = GormInit()
// GormInit init gorm ORM.
func GormInit() (*gorm.DB, error) {
conf := config.NewConfig()
conf := config.GetInstance()
db, err := gorm.Open(conf.DBType, conf.DBParams)
// db, err := gorm.Open("mysql", config.MysqlDSL())
//db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
@ -37,6 +39,7 @@ func GormInit() (*gorm.DB, error) {
}
log.CheckError(err)
log.Infof("lol", conf.DBParams)
// relation := gorm.Relationship{}
// relation.Kind = "many2many"

Voir le fichier

@ -2,7 +2,6 @@ package main
import (
"bufio"
"flag"
"fmt"
"github.com/ewhal/nyaa/config"
@ -29,17 +28,13 @@ func RunServer(conf *config.Config) {
}
func main() {
conf := config.NewConfig()
conf_bind := conf.BindFlags()
defaults := flag.Bool("print-defaults", false, "print the default configuration file on stdout")
flag.Parse()
if *defaults {
conf := config.GetInstance()
if *config.PrintDefaults {
stdout := bufio.NewWriter(os.Stdout)
conf.Pretty(stdout)
stdout.Flush()
os.Exit(0)
} else {
conf_bind()
RunServer(conf)
}
}