From 5a664e95c199c5e420b1bd2f5034b6795b471f42 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 25 Dec 2017 01:28:35 -0600 Subject: [PATCH] simplify routing --- app/rest/middleware.go | 2 +- app/rest/server.go | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/rest/middleware.go b/app/rest/middleware.go index a8f1b5e9..1e16011b 100644 --- a/app/rest/middleware.go +++ b/app/rest/middleware.go @@ -111,7 +111,7 @@ func Recoverer(next http.Handler) http.Handler { type contextKey string // Auth adds auth from session and populate user info -func Auth(sessionStore *sessions.FilesystemStore, admins []string, devMode bool) func(http.Handler) http.Handler { +func Auth(sessionStore *sessions.FilesystemStore, devMode bool, admins []string) func(http.Handler) http.Handler { f := func(h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { diff --git a/app/rest/server.go b/app/rest/server.go index 0c5f383f..444338e3 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -38,6 +38,7 @@ type Server struct { // Run the lister and request's router, activate rest server func (s *Server) Run() { log.Print("[INFO] activate rest server") + router := chi.NewRouter() router.Use(middleware.RealIP, Recoverer) router.Use(middleware.Throttle(1000), middleware.Timeout(60*time.Second)) @@ -55,16 +56,15 @@ func (s *Server) Run() { rapi.Get("/last/{max}", s.lastCommentsCtrl) rapi.Get("/count", s.countCtrl) - rapi.With(Auth(s.SessionStore, s.Admins, s.DevMode)).Group(func(rauth chi.Router) { + rapi.With(Auth(s.SessionStore, s.DevMode, s.Admins)).Group(func(rauth chi.Router) { rauth.Post("/comment", s.createCommentCtrl) rauth.Get("/user", s.userInfoCtrl) rauth.Put("/vote/{id}", s.voteCtrl) + + s.mod = admin{dataStore: s.Store, exporter: s.Exporter} + rauth.Mount("/admin", s.mod.routes()) }) - rapi.With(Auth(s.SessionStore, s.Admins, s.DevMode)).Group(func(radmin chi.Router) { - s.mod = admin{dataStore: s.Store, exporter: s.Exporter} - radmin.Mount("/admin", s.mod.routes()) - }) }) s.addFileServer(router, "/web", http.Dir(filepath.Join(".", "web"))) @@ -87,7 +87,6 @@ func (s *Server) addFileServer(r chi.Router, path string, root http.FileSystem) http.NotFound(w, r) return } - fs.ServeHTTP(w, r) })) } @@ -199,7 +198,6 @@ func (s *Server) commentByIDCtrl(w http.ResponseWriter, r *http.Request) { httpError(w, r, http.StatusInternalServerError, err, "can't get comment by id") return } - render.Status(r, http.StatusOK) renderJSONWithHTML(w, r, comment) } @@ -247,7 +245,7 @@ func (s *Server) voteCtrl(w http.ResponseWriter, r *http.Request) { return } - render.JSON(w, r, comment) + render.JSON(w, r, JSON{"id": comment.ID, "score": comment.Score}) } func httpError(w http.ResponseWriter, r *http.Request, code int, err error, details string) {