127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"cadoles/foodoles/bdd"
|
|
"cadoles/foodoles/foodlist"
|
|
"cadoles/foodoles/vote"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
// User is a user
|
|
type User struct {
|
|
Name string
|
|
Password string
|
|
}
|
|
|
|
func main() {
|
|
|
|
bdd.InitDB()
|
|
|
|
vote.GetVotesOfTheDay()
|
|
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/", &User{})
|
|
s := &http.Server{Addr: "localhost:8080", Handler: mux}
|
|
|
|
log.Print("\nready: listening on localhost:8080\n")
|
|
|
|
if err := s.ListenAndServe(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
// }
|
|
u.Name = "mlamalle"
|
|
u.Password = "password"
|
|
var auth = true
|
|
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) {
|
|
foods := foodlist.GetFoodOfTheDay()
|
|
paths := []string{
|
|
"./templates/index.tmpl",
|
|
}
|
|
t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
|
|
err := t.Execute(w, foods)
|
|
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))
|
|
}
|