package frontend import ( "html/template" "log" "net/http" "time" b "streifling.com/jason/cpolis/cmd/backend" ) func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { numRows, err := db.CountEntries("users") if err != nil { log.Fatalln(err) } data := new(struct { *UserHTMLData Version string }) data.UserHTMLData = &UserHTMLData{User: new(b.User)} data.Version = c.Version files := make([]string, 2) files[0] = c.WebDir + "/templates/index.html" if numRows == 0 { data.Role = b.NonExistent data.Title = "Erster Benutzer (Administrator)" data.ButtonText = "Anlegen" data.URL = "/user/add-first" files[1] = c.WebDir + "/templates/edit-user.html" tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } else { cookie, err := r.Cookie("cpolis_session") if err != nil { files[1] = c.WebDir + "/templates/login.html" tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } return } session, ok := s[cookie.Value] if !ok { cookie.Expires = time.Now() http.SetCookie(w, cookie) files[1] = c.WebDir + "/templates/login.html" tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } return } data.Role = session.User.Role files[1] = c.WebDir + "/templates/hub.html" tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } } } func ShowHub(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := ManageSession(w, r, c, s) if err != nil { http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized) return } data := new(struct{ Role int }) data.Role = session.User.Role tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } }