Skip to content

Commit

Permalink
Merge pull request #116 from rebuy-de/chi
Browse files Browse the repository at this point in the history
Replace httprouter with chi
  • Loading branch information
svenwltr authored Apr 8, 2022
2 parents 6da0041 + 849a550 commit a7eedd1
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 83 deletions.
7 changes: 0 additions & 7 deletions examples/full/cmd/assets/cdnmirror/bootstrap-5.1.3-min.js

This file was deleted.

26 changes: 0 additions & 26 deletions examples/full/cmd/assets/cdnmirror/hotwired-turbo-7.1.0-min.js

This file was deleted.

13 changes: 8 additions & 5 deletions examples/full/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/http"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-redis/redis/v8"
"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/rebuy-de/rebuy-go-sdk/v4/pkg/logutil"
"github.com/rebuy-de/rebuy-go-sdk/v4/pkg/redisutil"
Expand Down Expand Up @@ -58,9 +59,11 @@ func (s *Server) setupHTTPServer(ctx context.Context, group *errgroup.Group, htm
// process, so we can see what triggered a specific log message later.
ctx = logutil.Start(ctx, "http-server")

router := httprouter.New()
router.GET("/", webutil.Presenter(s.indexModel, html.View("index.html")))
router.ServeFiles("/assets/*filepath", http.FS(s.AssetFS))
router := chi.NewRouter()
router.Use(middleware.Logger)

router.Get("/", webutil.Presenter(s.indexModel, html.View("index.html")))
router.Mount("/assets", http.StripPrefix("/assets", http.FileServer(http.FS(s.AssetFS))))

group.Go(func() error {
logutil.Get(ctx).Info("http server listening on port 8080")
Expand All @@ -69,7 +72,7 @@ func (s *Server) setupHTTPServer(ctx context.Context, group *errgroup.Group, htm
})
}

func (s *Server) indexModel(r *http.Request, _ httprouter.Params) (interface{}, int, error) {
func (s *Server) indexModel(r *http.Request) (interface{}, int, error) {
InstIndexRequest(r.Context(), r)
return map[string]interface{}{
"now": time.Now(),
Expand Down
2 changes: 1 addition & 1 deletion examples/full/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ replace github.com/rebuy-de/rebuy-go-sdk/v4 => ../..

require (
github.com/alicebob/miniredis/v2 v2.19.0
github.com/go-chi/chi/v5 v5.0.7
github.com/go-redis/redis/v8 v8.11.5
github.com/julienschmidt/httprouter v1.3.0
github.com/pkg/errors v0.9.1
github.com/rebuy-de/rebuy-go-sdk/v4 v4.0.0
github.com/sirupsen/logrus v1.8.1
Expand Down
3 changes: 2 additions & 1 deletion examples/full/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ github.com/gemnasium/logrus-graylog-hook/v3 v3.1.0 h1:SLtCnpI5ZZaz4l7RSatEhppB1B
github.com/gemnasium/logrus-graylog-hook/v3 v3.1.0/go.mod h1:wi1zWv9tIvyLSMLCAzgRP+YR24oLVQVBHfPPKjtht44=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
Expand Down Expand Up @@ -265,7 +267,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kevinburke/ssh_config v1.1.0 h1:pH/t1WS9NzT8go394IqZeJTMHVm6Cr6ZJ6AQ+mdNo/o=
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/gorilla/securecookie v1.1.1
github.com/gorilla/sessions v1.2.1
github.com/hashicorp/vault/api v1.4.1
github.com/julienschmidt/httprouter v1.3.0
github.com/mitchellh/mapstructure v1.4.3
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kevinburke/ssh_config v1.1.0 h1:pH/t1WS9NzT8go394IqZeJTMHVm6Cr6ZJ6AQ+mdNo/o=
Expand Down
2 changes: 1 addition & 1 deletion pkg/webutil/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func init() {
//
// Endpoint "/auth/callback" gets called by the user after being redirected
// from GitHub after a successful login.
func AuthMiddleware(teams ...string) Middleware {
func AuthMiddleware(teams ...string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return authMiddlewareFunc(next, teams...)
}
Expand Down
33 changes: 0 additions & 33 deletions pkg/webutil/middleware.go

This file was deleted.

11 changes: 5 additions & 6 deletions pkg/webutil/mvp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/fs"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand All @@ -17,7 +16,7 @@ import (
// interface for data generation that is used by templates. This has the
// advantage that we can reuse models for multiple views (eg JSON and HTML) and
// that the data generation is isolated from representation.
type Model func(*http.Request, httprouter.Params) (interface{}, int, error)
type Model func(*http.Request) (interface{}, int, error)

// View should be used by with the Presenter and its puropose is to avoid
// having to implement the Golang template rendering for the gazillionth time.
Expand All @@ -27,9 +26,9 @@ type View func(http.ResponseWriter, *http.Request, interface{}, int, error)
// Presenter (from Model-view-presenter [1]) acts as a middleman between Model
// and View.
// [1]: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter
func Presenter(m Model, v View) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
data, code, err := m(r, ps)
func Presenter(m Model, v View) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data, code, err := m(r)
if err != nil {
logrus.
WithField("stacktrace", fmt.Sprintf("%+v", err)).
Expand Down Expand Up @@ -58,7 +57,7 @@ func Presenter(m Model, v View) httprouter.Handle {

// NilModel is a Model that contains no data. Useful for rendering templates
// that do not need any data.
func NilModel(*http.Request, httprouter.Params) (interface{}, int, error) {
func NilModel(*http.Request) (interface{}, int, error) {
return nil, http.StatusOK, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/webutil/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func SessionFromRequest(r *http.Request) (*sessions.Session, error) {

// SessionMiddleware inizializes the session store and injects it into the
// context of the requests.
func SessionMiddleware(secret SessionSecret, opts ...SessionMiddlewareOption) Middleware {
func SessionMiddleware(secret SessionSecret, opts ...SessionMiddlewareOption) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return sessionMiddlewareFunc(next, secret, opts...)
}
Expand Down

0 comments on commit a7eedd1

Please sign in to comment.