use go-chi

This commit is contained in:
Matthieu Lamalle 2019-11-27 12:18:35 +01:00
parent 86c2cf49f0
commit cbd85b5326
3 changed files with 15 additions and 7 deletions

BIN
foods.db

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -8,6 +8,11 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pkg/errors"
) )
// User is a user // User is a user
@ -31,15 +36,18 @@ func main() {
vote.GetVotesOfTheDay() vote.GetVotesOfTheDay()
mux := http.NewServeMux() r := chi.NewRouter()
mux.Handle("/", &User{}) r.Use(middleware.Logger)
s := &http.Server{Addr: "localhost" + conf.HTTP.Address, Handler: mux} r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
r.Handle("/", &User{})
r.Handle("/results", &User{})
log.Print("\nready: listening on localhost" + conf.HTTP.Address + "\n") log.Printf("listening on '%s'", conf.HTTP.Address)
if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil {
if err := s.ListenAndServe(); err != nil { panic(errors.Wrapf(err, "error while listening on '%s'", conf.HTTP.Address))
panic(err)
} }
} }
// ServerHTTP is the entry point to all requests // ServerHTTP is the entry point to all requests