foodoles/server.go

153 lines
3.2 KiB
Go

package main
import (
"cadoles/foodoles/auth"
"cadoles/foodoles/bdd"
"cadoles/foodoles/config"
"cadoles/foodoles/foodlist"
"cadoles/foodoles/vote"
"html/template"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pkg/errors"
)
// User is a user
type User struct {
Name string
Password string
}
var configFile = "server.conf"
func main() {
var conf *config.Config
var conferr error
conf, conferr = config.NewFromFile(configFile)
if conferr != nil {
conf = config.NewDefault()
}
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)
} else {
u.Auth(r.FormValue("user"), r.FormValue("password"))
}
if u.Name == "" {
LogInPage(w, r)
return
}
if r.Method == http.MethodPost && r.Form.Get("option") != "" {
VoteEndPoint(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
}
// auth := true
// u.Name = "mlamalle"
// u.Password = "pass"
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...))
err := t.Execute(w, "")
if 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 {
Foods foodlist.FoodOfTheDay
Votes vote.VotesOfTheDay
}
datas := HomeData{foodlist.GetFoodOfTheDay(), vote.GetVotesOfTheDay()}
paths := []string{
"./templates/index.tmpl",
}
t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
err := t.Execute(w, datas)
if 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...))
err := t.Execute(w, vote.GetVotesOfTheDay())
if 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))
}