2020-04-10 08:27:43 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-04-11 08:45:06 +02:00
|
|
|
"path"
|
2020-04-10 08:27:43 +02:00
|
|
|
"time"
|
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
"github.com/jackc/pgx/v4/stdlib"
|
|
|
|
//_ "github.com/jackc/pgx/v4/stdlib"
|
2020-04-10 08:27:43 +02:00
|
|
|
)
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
func initConf() (*Config, error) {
|
|
|
|
c, err := ReadInConfig(path.Join(confPath, GetConfigName()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.LogLevel {
|
|
|
|
case "debug":
|
|
|
|
logLevel = LogLevelDebug
|
|
|
|
case "error":
|
|
|
|
logLevel = LogLevelError
|
|
|
|
case "warn":
|
|
|
|
logLevel = LogLevelWarn
|
|
|
|
case "info":
|
|
|
|
logLevel = LogLevelInfo
|
|
|
|
default:
|
|
|
|
logLevel = LogLevelNone
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auths: validate and sanitize
|
|
|
|
am := make(map[string]struct{})
|
|
|
|
|
|
|
|
for i := 0; i < len(c.Auths); i++ {
|
|
|
|
a := &c.Auths[i]
|
|
|
|
a.Name = sanitize(a.Name)
|
|
|
|
|
|
|
|
if _, ok := am[a.Name]; ok {
|
|
|
|
c.Auths = append(c.Auths[:i], c.Auths[i+1:]...)
|
|
|
|
log.Printf("WRN duplicate auth found: %s", a.Name)
|
|
|
|
}
|
|
|
|
am[a.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions: validate and sanitize
|
|
|
|
axm := make(map[string]struct{})
|
|
|
|
|
|
|
|
for i := 0; i < len(c.Actions); i++ {
|
|
|
|
a := &c.Actions[i]
|
|
|
|
a.Name = sanitize(a.Name)
|
|
|
|
a.AuthName = sanitize(a.AuthName)
|
|
|
|
|
|
|
|
if _, ok := axm[a.Name]; ok {
|
|
|
|
c.Actions = append(c.Actions[:i], c.Actions[i+1:]...)
|
|
|
|
log.Printf("WRN duplicate action found: %s", a.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := am[a.AuthName]; !ok {
|
|
|
|
c.Actions = append(c.Actions[:i], c.Actions[i+1:]...)
|
|
|
|
log.Printf("WRN invalid auth_name '%s' for auth: %s", a.AuthName, a.Name)
|
|
|
|
}
|
|
|
|
axm[a.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var anonFound bool
|
|
|
|
|
|
|
|
for _, r := range c.Roles {
|
|
|
|
if sanitize(r.Name) == "anon" {
|
|
|
|
anonFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !anonFound {
|
|
|
|
log.Printf("WRN unauthenticated requests will be blocked. no role 'anon' defined")
|
|
|
|
c.AuthFailBlock = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 06:43:18 +02:00
|
|
|
func initDB(c *Config, useDB bool) (*sql.DB, error) {
|
2020-04-10 08:27:43 +02:00
|
|
|
var db *sql.DB
|
|
|
|
var err error
|
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// cs := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s",
|
|
|
|
// c.DB.Host, c.DB.Port,
|
|
|
|
// c.DB.User, c.DB.Password,
|
|
|
|
// c.DB.DBName)
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// fmt.Println(">>", cs)
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// for i := 1; i < 10; i++ {
|
|
|
|
// db, err = sql.Open("pgx", cs)
|
|
|
|
// if err == nil {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// time.Sleep(time.Duration(i*100) * time.Millisecond)
|
|
|
|
// }
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
2020-04-10 08:27:43 +02:00
|
|
|
// }
|
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// return db, nil
|
|
|
|
|
|
|
|
config, _ := pgx.ParseConfig("")
|
|
|
|
config.Host = c.DB.Host
|
|
|
|
config.Port = c.DB.Port
|
|
|
|
config.User = c.DB.User
|
|
|
|
config.Password = c.DB.Password
|
|
|
|
config.RuntimeParams = map[string]string{
|
|
|
|
"application_name": c.AppName,
|
|
|
|
"search_path": c.DB.Schema,
|
|
|
|
}
|
|
|
|
|
2020-04-13 06:43:18 +02:00
|
|
|
if useDB {
|
|
|
|
config.Database = c.DB.DBName
|
|
|
|
}
|
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
// switch c.LogLevel {
|
|
|
|
// case "debug":
|
2020-04-12 16:09:37 +02:00
|
|
|
// config.LogLevel = pgx.LogLevelDebug
|
2020-04-10 08:27:43 +02:00
|
|
|
// case "info":
|
2020-04-12 16:09:37 +02:00
|
|
|
// config.LogLevel = pgx.LogLevelInfo
|
2020-04-10 08:27:43 +02:00
|
|
|
// case "warn":
|
2020-04-12 16:09:37 +02:00
|
|
|
// config.LogLevel = pgx.LogLevelWarn
|
2020-04-10 08:27:43 +02:00
|
|
|
// case "error":
|
2020-04-12 16:09:37 +02:00
|
|
|
// config.LogLevel = pgx.LogLevelError
|
2020-04-10 08:27:43 +02:00
|
|
|
// default:
|
2020-04-12 16:09:37 +02:00
|
|
|
// config.LogLevel = pgx.LogLevelNone
|
2020-04-10 08:27:43 +02:00
|
|
|
// }
|
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
//config.Logger = NewSQLLogger(logger)
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
// if c.DB.MaxRetries != 0 {
|
|
|
|
// opt.MaxRetries = c.DB.MaxRetries
|
|
|
|
// }
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
// if c.DB.PoolSize != 0 {
|
|
|
|
// config.MaxConns = conf.DB.PoolSize
|
|
|
|
// }
|
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
db = stdlib.OpenDB(*config)
|
|
|
|
if db == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(i*100) * time.Millisecond)
|
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
|
2020-04-12 16:09:37 +02:00
|
|
|
return db, nil
|
2020-04-10 08:27:43 +02:00
|
|
|
}
|