128 lines
3.7 KiB
Go
128 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gorilla/handlers"
|
|
"github.com/pkg/profile"
|
|
|
|
"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
|
|
)
|
|
|
|
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 (Dev Branch) and waiting for the DB...")
|
|
|
|
count := MaxNumRetries
|
|
wait := true
|
|
|
|
for wait && count > 0 {
|
|
db, err = orm.NewDatabase(conf, orm.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)
|
|
|
|
if conf.Profiling {
|
|
log.Println("Start profiling...")
|
|
defer profile.Start().Stop()
|
|
}
|
|
|
|
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,
|
|
}
|
|
|
|
conf.Handlers.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},
|
|
},
|
|
}
|
|
|
|
conf.Handlers.PathPatterns = oef_handlers.DefaultPathPatterns
|
|
conf.Handlers.APIPathPatterns = oef_handlers.DefaultAPIPathPatterns
|
|
|
|
if err := http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, oef_handlers.NewHandlers(
|
|
conf,
|
|
renderer,
|
|
db,
|
|
orm.Models,
|
|
).Router)); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
}
|