oef/main.go

85 lines
1.6 KiB
Go

package main
import (
"flag"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/handlers"
"git.andreafazzi.eu/andrea/oef/config"
oef_handlers "git.andreafazzi.eu/andrea/oef/handlers"
"git.andreafazzi.eu/andrea/oef/orm"
)
const (
MaxNumRetries = 20
RetryTimeInterval = 10
)
var (
db *orm.Database
err error
models = []interface{}{
&orm.Question{},
&orm.Answer{},
&orm.Contest{},
&orm.Participant{},
&orm.School{},
&orm.Response{},
&orm.User{},
&orm.Category{},
&orm.Region{},
}
)
func main() {
configFile := flag.String("config", "config/config.yaml", "Load the given config file")
flag.Parse()
log.Println("Loading config file...")
err = config.ReadFile(*configFile, config.Config)
if err != nil {
panic(err)
}
log.Println("Starting OEF and waiting for the DB...")
count := MaxNumRetries
wait := true
for wait && count > 0 {
db, err = orm.NewDatabase(config.Config, models)
if err != nil {
count--
log.Println(err)
log.Printf("Remaining retries: %d", count)
time.Sleep(time.Second * RetryTimeInterval)
continue
}
wait = false
}
// REMOVE
// orm.Use(db)
if config.Config.Orm.AutoMigrate {
log.Print("Automigrating...")
db.AutoMigrate()
}
log.Println("Eventually write categories on DB...")
orm.CreateCategories(db)
log.Println("Eventually write regions on DB...")
orm.CreateRegions(db)
log.Println("OEF is listening to port 3000...")
if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, oef_handlers.NewHandlers(config.Config, db, models).Router)); err != nil {
panic(err)
}
}