100 lines
1.8 KiB
Go
100 lines
1.8 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"
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
)
|
|
|
|
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...")
|
|
|
|
conf := new(config.ConfigT)
|
|
err = config.ReadFile(*configFile, conf)
|
|
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(conf, models)
|
|
if err != nil {
|
|
count--
|
|
log.Println(err)
|
|
log.Printf("Remaining retries: %d", count)
|
|
time.Sleep(time.Second * RetryTimeInterval)
|
|
continue
|
|
}
|
|
wait = false
|
|
}
|
|
|
|
if conf.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...")
|
|
|
|
htmlRenderer, err := renderer.NewHTMLRenderer("templates")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
jsonRenderer, err := renderer.NewJSONRenderer()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
renderer := map[string]renderer.Renderer{
|
|
"html": htmlRenderer,
|
|
"json": jsonRenderer,
|
|
}
|
|
if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, oef_handlers.NewHandlers(conf, renderer, db, models).Router)); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|