initial
This commit is contained in:
51
questions/config/config.go
Executable file
51
questions/config/config.go
Executable file
@@ -0,0 +1,51 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/args"
|
||||
"local/storage"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Addr string
|
||||
DB storage.DB
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func New() error {
|
||||
as := args.NewArgSet()
|
||||
as.Append(args.INT, "p", "port to listen on", 39981)
|
||||
as.Append(args.STRING, "db", "db type to use", "map")
|
||||
as.Append(args.STRING, "dbns", "db namespace", "")
|
||||
as.Append(args.STRING, "dbaddr", "db's address or path", "")
|
||||
as.Append(args.STRING, "dbuser", "db's username", "")
|
||||
as.Append(args.STRING, "dbpass", "db's password", "")
|
||||
err := as.Parse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := storage.New(
|
||||
storage.TypeFromString(as.Get("db").GetString()),
|
||||
as.Get("dbaddr").GetString(),
|
||||
as.Get("dbuser").GetString(),
|
||||
as.Get("dbpass").GetString(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
was := config.DB
|
||||
config = Config{
|
||||
Addr: fmt.Sprintf(":%d", as.Get("p").GetInt()),
|
||||
DB: db,
|
||||
}
|
||||
if was != nil {
|
||||
was.Close()
|
||||
}
|
||||
storage.DefaultNamespace = as.Get("dbns").GetString()
|
||||
return err
|
||||
}
|
||||
|
||||
func Get() Config {
|
||||
return config
|
||||
}
|
||||
Reference in New Issue
Block a user