78 lines
1.4 KiB
Go
78 lines
1.4 KiB
Go
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
|
|
RetryTimeInterval = 5
|
|
)
|
|
|
|
var (
|
|
db *gorm.DB
|
|
err error
|
|
|
|
models = []interface{}{
|
|
&orm.Question{},
|
|
&orm.Answer{},
|
|
}
|
|
)
|
|
|
|
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...)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
}
|