foodoles/server.go

157 lines
3.3 KiB
Go
Raw Normal View History

2019-11-20 17:11:29 +01:00
package main
import (
"html/template"
"log"
"net/http"
2019-11-27 12:18:35 +01:00
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pkg/errors"
2019-12-13 16:05:19 +01:00
2020-07-08 11:21:24 +02:00
"forge.cadoles.com/foodoles/auth"
2019-12-13 16:05:19 +01:00
"forge.cadoles.com/foodoles/bdd"
"forge.cadoles.com/foodoles/config"
"forge.cadoles.com/foodoles/foodlist"
"forge.cadoles.com/foodoles/vote"
2019-11-20 17:11:29 +01:00
)
2019-11-27 10:06:24 +01:00
// User is a user
type User struct {
Name string
Password string
}
2019-12-13 16:05:19 +01:00
const configFile string = "server.conf"
2019-11-28 14:36:49 +01:00
var secret string
2019-11-27 12:09:15 +01:00
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-28 14:36:49 +01:00
secret = conf.HTTP.Secret
2019-11-27 12:09:15 +01:00
2019-11-27 10:06:24 +01:00
bdd.InitDB()
2019-11-26 09:07:19 +01:00
vote.GetVotesOfTheDay()
2019-11-27 12:18:35 +01:00
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{})
2019-11-21 13:54:40 +01:00
2019-11-27 12:18:35 +01:00
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))
2019-11-20 17:11:29 +01:00
}
}
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-12-13 16:05:19 +01:00
return
2019-11-27 10:06:24 +01:00
}
2019-11-28 10:21:17 +01:00
2019-12-13 16:05:19 +01:00
u.Auth(r.FormValue("user"), r.FormValue("password"))
2019-11-28 14:39:20 +01:00
if r.Method == http.MethodPost && r.Form.Get("option") != "" && r.Form.Get("key") == secret {
2019-11-28 14:36:49 +01:00
VoteEndPoint(w, r)
2019-11-27 10:06:24 +01:00
return
}
2019-11-28 14:36:49 +01:00
if u.Name == "" {
LogInPage(w, r)
2019-11-27 10:06:24 +01:00
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 {
2020-07-08 11:21:24 +02:00
auth, _ := auth.LogIn(user, pass)
if auth {
u.Name = user
u.Password = pass
} else {
u.Name = ""
u.Password = ""
}
2019-11-27 10:06:24 +01:00
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...))
2019-12-13 16:05:19 +01:00
if err := t.Execute(w, ""); err != nil {
2019-11-27 10:06:24 +01:00
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) {
2019-11-27 14:42:08 +01:00
type HomeData struct {
2019-11-28 14:36:49 +01:00
Key string
2019-11-27 14:42:08 +01:00
Foods foodlist.FoodOfTheDay
Votes vote.VotesOfTheDay
}
2019-11-28 14:36:49 +01:00
datas := HomeData{secret, foodlist.GetFoodOfTheDay(), vote.GetVotesOfTheDay()}
2019-11-27 14:42:08 +01:00
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...))
2019-12-13 16:05:19 +01:00
if err := t.Execute(w, datas); err != nil {
2019-11-20 17:11:29 +01:00
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
2019-12-13 16:05:19 +01:00
if err := t.Execute(w, vote.GetVotesOfTheDay()); err != nil {
2019-11-21 16:43:02 +01:00
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
}