package main import ( "html/template" "log" "net/http" "time" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/pkg/errors" "forge.cadoles.com/foodoles/auth" "forge.cadoles.com/foodoles/bdd" "forge.cadoles.com/foodoles/config" "forge.cadoles.com/foodoles/foodlist" "forge.cadoles.com/foodoles/vote" ) // User is a user type User struct { Name string Password string } const configFile string = "server.conf" var secret string func main() { var conf *config.Config var conferr error conf, conferr = config.NewFromFile(configFile) if conferr != nil { conf = config.NewDefault() } secret = conf.HTTP.Secret bdd.InitDB() vote.GetVotesOfTheDay() r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) r.Use(middleware.Timeout(60 * time.Second)) r.Handle("/", &User{}) r.Handle("/results", &User{}) log.Printf("listening on '%s'", conf.HTTP.Address) if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil { panic(errors.Wrapf(err, "error while listening on '%s'", conf.HTTP.Address)) } } // ServerHTTP is the entry point to all requests func (u *User) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { log.Printf("ParseForm() err: %v", err) return } u.Auth(r.FormValue("user"), r.FormValue("password")) if r.Method == http.MethodPost && r.Form.Get("option") != "" && r.Form.Get("key") == secret { VoteEndPoint(w, r) return } if u.Name == "" { LogInPage(w, r) return } if r.URL.Path == "/" { HomePage(w, r) return } if r.URL.Path == "/results" { ResultsPage(w, r) return } } // Auth is the login page for user func (u *User) Auth(user string, pass string) bool { auth, _ := auth.LogIn(user, pass) if auth { u.Name = user u.Password = pass } else { u.Name = "" u.Password = "" } return auth } // LogInPage is the log in page of the app func LogInPage(w http.ResponseWriter, r *http.Request) { paths := []string{ "./templates/login.tmpl", } t := template.Must(template.New("login.tmpl").ParseFiles(paths...)) if err := t.Execute(w, ""); err != nil { log.Printf("\nExecute error: %v", err) return } } // HomePage is the homepage of the app func HomePage(w http.ResponseWriter, r *http.Request) { type HomeData struct { Key string Foods foodlist.FoodOfTheDay Votes vote.VotesOfTheDay } datas := HomeData{secret, foodlist.GetFoodOfTheDay(), vote.GetVotesOfTheDay()} paths := []string{ "./templates/index.tmpl", } t := template.Must(template.New("index.tmpl").ParseFiles(paths...)) if err := t.Execute(w, datas); err != nil { log.Printf("\nExecute error: %v", err) return } } // ResultsPage is the page displaiyng the votes results func ResultsPage(w http.ResponseWriter, r *http.Request) { paths := []string{ "./templates/results.tmpl", } t := template.Must(template.New("results.tmpl").ParseFiles(paths...)) if err := t.Execute(w, vote.GetVotesOfTheDay()); err != nil { log.Printf("\nExecute error: %v", err) return } } // VoteEndPoint is the endpoint to add votes func VoteEndPoint(w http.ResponseWriter, r *http.Request) { r.ParseForm() option := r.Form.Get("option") log.Print("vote for : ", option) vote.ForFood(option) w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") w.Write([]byte("Success vote for: " + option)) }