2019-11-20 17:11:29 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-21 13:54:40 +01:00
|
|
|
"cadoles/foodoles/foodlist"
|
2019-11-20 17:11:29 +01:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-21 13:54:40 +01:00
|
|
|
// Today is today
|
|
|
|
var Today = time.Now().Format("02/01/2006")
|
2019-11-20 17:11:29 +01:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
http.HandleFunc("/", base)
|
|
|
|
http.HandleFunc("/results", results)
|
|
|
|
|
|
|
|
log.Print("ready: listening on localhost:8080\n")
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-20 17:11:29 +01:00
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func base(w http.ResponseWriter, r *http.Request) {
|
2019-11-21 13:54:40 +01:00
|
|
|
// TODO: Init la base avec la date du jour
|
2019-11-20 17:11:29 +01:00
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
log.Printf("vote")
|
|
|
|
} else if r.Method == http.MethodGet {
|
|
|
|
home(w, r)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("400 - Unsupported Request Method!"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
2019-11-21 13:54:40 +01:00
|
|
|
foods := foodlist.GetFoodOfTheDay(Today)
|
|
|
|
paths := []string{
|
|
|
|
"./templates/index.tmpl",
|
2019-11-20 17:11:29 +01:00
|
|
|
}
|
2019-11-21 13:54:40 +01:00
|
|
|
t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
|
|
|
|
err := t.Execute(w, foods)
|
2019-11-20 17:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("\nExecute error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:54:40 +01:00
|
|
|
func results(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
// option := r.Form.Get("option")
|
|
|
|
// foodlist.VoteForFood(Today, option)
|
|
|
|
|
|
|
|
// paths := []string{
|
|
|
|
// "./templates/results.tmpl",
|
|
|
|
// }
|
|
|
|
// t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
|
|
|
// err := t.Execute(w, "")
|
|
|
|
// if err != nil {
|
|
|
|
// log.Printf("\nExecute error: %v", err)
|
|
|
|
// return
|
|
|
|
// }
|
2019-11-20 17:11:29 +01:00
|
|
|
}
|