package main import ( "database/sql" "fmt" "log" "net/http" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/lists" "fagott.pw/grilist/modules/grils" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" ) var app *grilist.Grilist func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { _, err := app.Charakterin.GetUserFromRequest(r) if err != nil { http.Redirect(w, r, "/login", 302) return } http.Redirect(w, r, "/dashboard", 302) } func loadModule(mod grilist.Module) error { if _, ok := app.Modules[mod.Name()]; ok { return fmt.Errorf("module with name %s already exists", mod.Name()) } app.Modules[mod.Name()] = mod mod.Init(app) return nil } func main() { config := grilist.LoadConfig() db, err := sql.Open("postgres", config.DBConnectionString()) if err != nil { log.Fatal(err) } if err := db.Ping(); err != nil { log.Fatal(err) } log.Println("database connection established") renderer := frontend.New("views") login := charakterin.New(db) login.UseRenderer(renderer) router := httprouter.New() app = &grilist.Grilist{ config, renderer, login, db, make(map[string]grilist.Module), router, } router.HandlerFunc("GET", "/login", login.DisplayLogin) router.HandlerFunc("POST", "/login", login.Login) router.HandlerFunc("GET", "/logout", login.Logout) router.HandlerFunc("GET", "/register", login.DisplayRegistration) router.HandlerFunc("POST", "/register", login.Register) router.GET("/", index) router.GET("/dashboard", viewDashboard) fs := http.FileServer(http.Dir("assets")) http.Handle("/assets/", http.StripPrefix("/assets/", fs)) http.Handle("/", router) // Module laden loadModule(grils.New()) loadModule(lists.New()) log.Fatal(http.ListenAndServe(":8080", nil)) }