foodoles/server.go

137 lines
2.8 KiB
Go
Raw Normal View History

2019-11-20 17:11:29 +01:00
package main
import (
2019-11-21 16:43:02 +01:00
"cadoles/foodoles/bdd"
2019-11-27 12:09:15 +01:00
"cadoles/foodoles/config"
2019-11-21 13:54:40 +01:00
"cadoles/foodoles/foodlist"
2019-11-26 09:07:19 +01:00
"cadoles/foodoles/vote"
2019-11-20 17:11:29 +01:00
"html/template"
"log"
"net/http"
)
2019-11-27 10:06:24 +01:00
// User is a user
type User struct {
Name string
Password string
}
2019-11-27 12:09:15 +01:00
var configFile = "server.conf"
2019-11-20 17:11:29 +01:00
func main() {
2019-11-27 12:09:15 +01:00
var conf *config.Config
var conferr error
conf, conferr = config.NewFromFile(configFile)
if conferr != nil {
conf = config.NewDefault()
}
2019-11-27 10:06:24 +01:00
bdd.InitDB()
2019-11-26 09:07:19 +01:00
vote.GetVotesOfTheDay()
2019-11-27 10:06:24 +01:00
mux := http.NewServeMux()
mux.Handle("/", &User{})
2019-11-27 12:09:15 +01:00
s := &http.Server{Addr: "localhost" + conf.HTTP.Address, Handler: mux}
2019-11-27 10:06:24 +01:00
2019-11-27 12:09:15 +01:00
log.Print("\nready: listening on localhost" + conf.HTTP.Address + "\n")
2019-11-21 13:54:40 +01:00
2019-11-27 10:06:24 +01:00
if err := s.ListenAndServe(); err != nil {
2019-11-20 17:11:29 +01:00
panic(err)
}
}
2019-11-27 10:06:24 +01:00
// 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)
2019-11-20 17:11:29 +01:00
} else {
2019-11-27 10:06:24 +01:00
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
2019-11-20 17:11:29 +01:00
}
}
2019-11-26 09:07:19 +01:00
// HomePage is the homepage of the app
func HomePage(w http.ResponseWriter, r *http.Request) {
foods := foodlist.GetFoodOfTheDay()
2019-11-21 13:54:40 +01:00
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-26 09:07:19 +01:00
// ResultsPage is the page displaiyng the votes results
func ResultsPage(w http.ResponseWriter, r *http.Request) {
2019-11-21 16:43:02 +01:00
paths := []string{
"./templates/results.tmpl",
}
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
2019-11-27 10:06:24 +01:00
err := t.Execute(w, vote.GetVotesOfTheDay())
2019-11-21 16:43:02 +01:00
if err != nil {
log.Printf("\nExecute error: %v", err)
return
}
}
2019-11-21 13:54:40 +01:00
2019-11-27 10:06:24 +01:00
// VoteEndPoint is the endpoint to add votes
func VoteEndPoint(w http.ResponseWriter, r *http.Request) {
2019-11-21 16:43:02 +01:00
r.ParseForm()
option := r.Form.Get("option")
2019-11-26 09:07:19 +01:00
2019-11-21 16:43:02 +01:00
log.Print("vote for : ", option)
2019-11-26 09:07:19 +01:00
vote.ForFood(option)
2019-11-21 13:54:40 +01:00
2019-11-21 16:43:02 +01:00
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("Success vote for: " + option))
2019-11-20 17:11:29 +01:00
}