oef/main.go

123 lines
3.5 KiB
Go
Raw Normal View History

2019-11-04 15:00:46 +01:00
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"
2019-11-04 15:00:46 +01:00
)
const (
MaxNumRetries = 20
2019-12-04 10:11:18 +01:00
RetryTimeInterval = 10
2019-11-04 15:00:46 +01:00
)
var (
db *orm.Database
2019-11-04 15:00:46 +01:00
err error
)
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)
2019-11-04 15:00:46 +01:00
if err != nil {
panic(err)
}
log.Println("Starting OEF and waiting for the DB...")
count := MaxNumRetries
wait := true
for wait && count > 0 {
2020-01-15 16:42:35 +01:00
db, err = orm.NewDatabase(conf, orm.Models)
2019-11-04 15:00:46 +01:00
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 {
2019-11-04 15:00:46 +01:00
log.Print("Automigrating...")
db.AutoMigrate()
2019-11-04 15:00:46 +01:00
}
2019-12-09 08:27:46 +01:00
log.Println("Eventually write categories on DB...")
orm.CreateCategories(db)
2019-12-09 08:27:46 +01:00
2019-12-09 14:18:31 +01:00
log.Println("Eventually write regions on DB...")
orm.CreateRegions(db)
2019-11-04 15:00:46 +01:00
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,
}
2020-01-16 12:47:35 +01:00
permissions := map[string]map[string][]int{
"administrator": map[string][]int{
"Contest": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"Participant": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"School": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"Question": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"Answer": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"Response": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
},
"school": map[string][]int{
"Participant": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead, oef_handlers.PermissionReadAll, oef_handlers.PermissionUpdate, oef_handlers.PermissionDelete},
"School": []int{oef_handlers.PermissionRead, oef_handlers.PermissionUpdate},
},
"participant": map[string][]int{
"Participant": []int{oef_handlers.PermissionRead},
"Response": []int{oef_handlers.PermissionUpdate, oef_handlers.PermissionRead},
},
"subscriber": map[string][]int{
"School": []int{oef_handlers.PermissionCreate, oef_handlers.PermissionRead},
},
}
if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, oef_handlers.NewHandlers(
conf,
renderer,
db,
orm.Models,
permissions,
handlers.DefaultPathPatterns,
handlers.DefaultAPIPathPatterns,
).Router)); err != nil {
2019-11-04 15:00:46 +01:00
panic(err)
}
}