oef/main.go

88 lines
1.6 KiB
Go
Raw Normal View History

2019-11-04 15:00:46 +01:00
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/handlers"
"github.com/jinzhu/gorm"
"git.andreafazzi.eu/andrea/oef/config"
oef_handlers "git.andreafazzi.eu/andrea/oef/handlers"
"git.andreafazzi.eu/andrea/oef/orm"
)
const (
MaxNumRetries = 20
2019-12-04 10:11:18 +01:00
RetryTimeInterval = 10
2019-11-04 15:00:46 +01:00
)
var (
db *gorm.DB
err error
models = []interface{}{
&orm.Question{},
&orm.Answer{},
2019-11-14 12:55:22 +01:00
&orm.Contest{},
2019-11-14 15:04:11 +01:00
&orm.Participant{},
2019-12-03 12:21:28 +01:00
&orm.School{},
2019-11-18 17:04:07 +01:00
&orm.Response{},
&orm.User{},
2019-12-09 08:27:46 +01:00
&orm.Category{},
2019-11-04 15:00:46 +01:00
}
)
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.New(fmt.Sprintf("%s?%s", config.Config.Orm.Connection, config.Config.Orm.Options))
if err != nil {
count--
log.Println(err)
log.Printf("Remaining retries: %d", count)
time.Sleep(time.Second * RetryTimeInterval)
continue
}
wait = false
}
orm.Use(db)
if config.Config.Orm.AutoMigrate {
log.Print("Automigrating...")
orm.AutoMigrate(models...)
}
2019-12-09 08:27:46 +01:00
log.Println("Eventually write categories on DB...")
orm.CreateCategories()
2019-11-04 15:00:46 +01:00
log.Println("Map models <-> handlers")
if err := orm.MapHandlers(models); err != nil {
panic(err)
}
log.Println("OEF is listening to port 3000...")
if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, oef_handlers.Handlers(models))); err != nil {
panic(err)
}
}