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" "fmt"
"io" "io"
"os" "os"
) "sync"
)
type Config struct { type Config struct {
Host string `json: "host"` Host string `json: "host"`
@ -20,7 +21,7 @@ type Config struct {
} }
var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"} var Defaults = Config{"localhost", 9999, "sqlite3", "./nyaa.db?cache_size=50"}
var PrintDefaults *bool
var allowedDatabaseTypes = map[string]bool{ var allowedDatabaseTypes = map[string]bool{
"sqlite3": true, "sqlite3": true,
"postgres": true, "postgres": true,
@ -28,39 +29,45 @@ var allowedDatabaseTypes = map[string]bool{
"mssql": true, "mssql": true,
} }
func NewConfig() *Config { var instance *Config
var config Config var once sync.Once
config.Host = Defaults.Host
config.Port = Defaults.Port func GetInstance() *Config {
config.DBType = Defaults.DBType once.Do(func() {
config.DBParams = Defaults.DBParams instance = &Config{}
return &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 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 // 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") conf_file := flag.String("conf", "", "path to the configuration file")
db_type := flag.String("dbtype", Defaults.DBType, "database backend") db_type := flag.String("dbtype", Defaults.DBType, "database backend")
host := flag.String("host", Defaults.Host, "binding address of the server") host := flag.String("host", Defaults.Host, "binding address of the server")
port := flag.Int("port", Defaults.Port, "port 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)") db_params := flag.String("dbparams", Defaults.DBParams, "parameters to open the database (see Gorm's doc)")
PrintDefaults = flag.Bool("print-defaults", false, "print the default configuration file on stdout")
return func() error { flag.Parse()
err := config.HandleConfFileFlag(*conf_file) err := config.HandleConfFileFlag(*conf_file)
if err != nil { 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 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 { func (config *Config) HandleConfFileFlag(path string) error {

Voir le fichier

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

Voir le fichier

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