taskcafe/cmd/citadel/main.go

63 lines
1.3 KiB
Go
Raw Normal View History

2020-04-10 04:40:22 +02:00
package main
import (
"fmt"
_ "github.com/lib/pq"
"io/ioutil"
2020-04-10 04:40:22 +02:00
"net/http"
"time"
2020-04-10 04:40:22 +02:00
"github.com/BurntSushi/toml"
2020-04-10 04:40:22 +02:00
"github.com/jmoiron/sqlx"
"github.com/jordanknott/project-citadel/api/internal/route"
2020-04-10 04:40:22 +02:00
log "github.com/sirupsen/logrus"
)
type Database struct {
Host string
Name string
User string
Password string
}
type AppConfig struct {
Database Database
}
2020-04-10 04:40:22 +02:00
func main() {
dat, err := ioutil.ReadFile("conf/app.toml")
if err != nil {
panic(err)
}
var appConfig AppConfig
_, err = toml.Decode(string(dat), &appConfig)
if err != nil {
panic(err)
}
2020-04-10 04:40:22 +02:00
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
log.SetFormatter(Formatter)
log.SetLevel(log.InfoLevel)
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
appConfig.Database.User,
appConfig.Database.Password,
appConfig.Database.Host,
appConfig.Database.Name,
)
db, err := sqlx.Connect("postgres", connection)
2020-04-10 04:40:22 +02:00
if err != nil {
log.Panic(err)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5 * time.Minute)
2020-04-10 04:40:22 +02:00
defer db.Close()
fmt.Println("starting graphql server on http://localhost:3333")
fmt.Println("starting graphql playground on http://localhost:3333/__graphql")
r, _ := route.NewRouter(db)
2020-04-10 04:40:22 +02:00
http.ListenAndServe(":3333", r)
}