super-graph/serv/serv.go

163 lines
3.7 KiB
Go
Raw Normal View History

2019-03-24 14:57:29 +01:00
package serv
import (
"errors"
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/dosco/super-graph/psql"
"github.com/dosco/super-graph/qcode"
"github.com/go-pg/pg"
2019-03-25 05:43:14 +01:00
"github.com/gobuffalo/flect"
2019-03-24 14:57:29 +01:00
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const (
authFailBlockAlways = iota + 1
authFailBlockPerQuery
authFailBlockNever
)
var (
logger *logrus.Logger
debug int
conf *viper.Viper
db *pg.DB
pcompile *psql.Compiler
qcompile *qcode.Compiler
authFailBlock int
)
func initLog() {
logger = logrus.New()
logger.Formatter = new(logrus.TextFormatter)
logger.Formatter.(*logrus.TextFormatter).DisableColors = false
logger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
logger.Level = logrus.TraceLevel
logger.Out = os.Stdout
}
func initConf() {
conf = viper.New()
cPath := flag.String("path", ".", "Path to folder that contains config files")
flag.Parse()
conf.AddConfigPath(*cPath)
switch os.Getenv("GO_ENV") {
case "production", "prod":
conf.SetConfigName("prod")
case "staging", "stage":
conf.SetConfigName("stage")
default:
conf.SetConfigName("dev")
}
err := conf.ReadInConfig()
if err != nil {
logger.Fatal(err)
}
debug = conf.GetInt("debug_level")
for k, v := range conf.GetStringMapString("inflections") {
2019-03-25 05:43:14 +01:00
flect.AddPlural(k, v)
2019-03-24 14:57:29 +01:00
}
conf.SetDefault("host_port", "0.0.0.0:8080")
conf.SetDefault("web_ui", false)
conf.SetDefault("debug_level", 0)
conf.SetDefault("database.type", "postgres")
conf.SetDefault("database.host", "localhost")
conf.SetDefault("database.port", 5432)
conf.SetDefault("database.user", "postgres")
conf.SetDefault("database.password", "")
conf.SetDefault("env", "development")
conf.BindEnv("env", "GO_ENV")
switch conf.GetString("auth_fail_block") {
case "always":
authFailBlock = authFailBlockAlways
case "per_query", "perquery", "query":
authFailBlock = authFailBlockPerQuery
case "never", "false":
authFailBlock = authFailBlockNever
default:
authFailBlock = authFailBlockAlways
}
}
func initDB() {
conf.BindEnv("database.host", "SG_DATABASE_HOST")
conf.BindEnv("database.port", "SG_DATABASE_PORT")
conf.BindEnv("database.user", "SG_DATABASE_USER")
conf.BindEnv("database.password", "SG_DATABASE_PASSWORD")
hostport := strings.Join([]string{
conf.GetString("database.host"), conf.GetString("database.port")}, ":")
opt := &pg.Options{
Addr: hostport,
User: conf.GetString("database.user"),
Password: conf.GetString("database.password"),
Database: conf.GetString("database.dbname"),
}
if conf.IsSet("database.pool_size") {
opt.PoolSize = conf.GetInt("database.pool_size")
}
if conf.IsSet("database.max_retries") {
opt.MaxRetries = conf.GetInt("database.max_retries")
}
if db = pg.Connect(opt); db == nil {
logger.Fatal(errors.New("failed to connect to postgres db"))
}
}
func initCompilers() {
2019-03-24 23:16:03 +01:00
filters := conf.GetStringMapString("database.filters")
blacklist := conf.GetStringSlice("database.blacklist")
2019-03-24 14:57:29 +01:00
2019-03-24 23:16:03 +01:00
fm := qcode.NewFilterMap(filters)
bl := qcode.NewBlacklist(blacklist)
2019-03-24 14:57:29 +01:00
qcompile = qcode.NewCompiler(fm, bl)
schema, err := psql.NewDBSchema(db)
if err != nil {
logger.Fatal(err)
}
2019-03-24 23:16:03 +01:00
varlist := conf.GetStringMapString("database.variables")
vars := psql.NewVariables(varlist)
2019-03-24 14:57:29 +01:00
pcompile = psql.NewCompiler(schema, vars)
}
func InitAndListen() {
initLog()
initConf()
initDB()
initCompilers()
http.HandleFunc("/api/v1/graphql", withAuth(apiv1Http))
if conf.GetBool("web_ui") {
fs := http.FileServer(http.Dir("web/build"))
http.Handle("/", fs)
}
hp := conf.GetString("host_port")
fmt.Printf("Super-Graph listening on %s (%s)\n", hp, conf.GetString("env"))
logger.Fatal(http.ListenAndServe(hp, nil))
}