foodoles/server.go

67 lines
1.4 KiB
Go

package main
import (
"cadoles/foodoles/foodlist"
"html/template"
"log"
"net/http"
"time"
)
// Today is today
var Today = time.Now().Format("02/01/2006")
func main() {
http.HandleFunc("/", base)
http.HandleFunc("/results", results)
log.Print("ready: listening on localhost:8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func base(w http.ResponseWriter, r *http.Request) {
// TODO: Init la base avec la date du jour
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) {
foods := foodlist.GetFoodOfTheDay(Today)
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
}
}
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
// }
}