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"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pkg/errors"
)
// User is a user
@ -31,15 +36,18 @@ func main() {
vote.GetVotesOfTheDay()
mux := http.NewServeMux()
mux.Handle("/", &User{})
s := &http.Server{Addr: "localhost" + conf.HTTP.Address, Handler: mux}
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{})
log.Print("\nready: listening on localhost" + conf.HTTP.Address + "\n")
if err := s.ListenAndServe(); err != nil {
panic(err)
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))
}
}
// ServerHTTP is the entry point to all requests