From 4c4ef95ee768fbe11510245f178edc25d0994a85 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 11 Sep 2021 22:17:22 +0200 Subject: [PATCH] changed http handler to mux --- go.mod | 1 + go.sum | 2 ++ internal/debug.go | 4 +++- internal/httpServer.go | 36 +++++++++++++++++++++--------------- internal/staticFiles.go | 4 ++-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 75904f7..65d6a6b 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/alexflint/go-arg v1.4.2 + github.com/gorilla/mux v1.8.0 github.com/graph-gophers/dataloader v5.0.0+incompatible github.com/graphql-go/graphql v0.7.9 github.com/graphql-go/handler v0.2.3 diff --git a/go.sum b/go.sum index c3770ec..0019e80 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/graph-gophers/dataloader v5.0.0+incompatible h1:R+yjsbrNq1Mo3aPG+Z/EKYrXrXXUNJHOgbRt+U6jOug= github.com/graph-gophers/dataloader v5.0.0+incompatible/go.mod h1:jk4jk0c5ZISbKaMe8WsVopGB5/15GvGHMdMdPtwlRp4= github.com/graphql-go/graphql v0.7.9 h1:5Va/Rt4l5g3YjwDnid3vFfn43faaQBq7rMcIZ0VnV34= diff --git a/internal/debug.go b/internal/debug.go index 1994573..ed11b4b 100644 --- a/internal/debug.go +++ b/internal/debug.go @@ -3,7 +3,9 @@ package s3browser +import "github.com/gorilla/mux" + // Since we dont have the static directory when developing we replace the function with an empty one -func initStatic() { +func initStatic(r *mux.Router) { // NOOP } diff --git a/internal/httpServer.go b/internal/httpServer.go index cf91c37..3e025ea 100644 --- a/internal/httpServer.go +++ b/internal/httpServer.go @@ -8,6 +8,7 @@ import ( "net/http" "path/filepath" + "github.com/gorilla/mux" "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/gqlerrors" "github.com/graphql-go/handler" @@ -18,7 +19,9 @@ import ( // initHttp setup and start the http server. Blocking func initHttp(resolveContext context.Context, schema graphql.Schema, address string) error { - h := handler.New(&handler.Config{ + r := mux.NewRouter() + + gqlHandler := handler.New(&handler.Config{ Schema: &schema, Pretty: true, GraphiQL: false, @@ -34,26 +37,29 @@ func initHttp(resolveContext context.Context, schema graphql.Schema, address str }, }) - http.HandleFunc("/api/graphql", func(rw http.ResponseWriter, r *http.Request) { - h.ContextHandler(resolveContext, rw, r) + r.Use(func(h http.Handler) http.Handler { + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + // TODO: handle auth + h.ServeHTTP(rw, r) + }) }) - http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) { - if r.Method == "GET" { - httpGetFile(resolveContext, rw, r) - return - } - - if r.Method == "POST" { - httpPostFile(resolveContext, rw, r) - return - } + r.HandleFunc("/api/graphql", func(rw http.ResponseWriter, r *http.Request) { + gqlHandler.ContextHandler(resolveContext, rw, r) }) + r.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) { + httpGetFile(resolveContext, rw, r) + }).Methods("GET") + + r.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) { + httpPostFile(resolveContext, rw, r) + }).Methods("POST") + // Init the embedded static files - initStatic() + initStatic(r) - return http.ListenAndServe(address, nil) + return http.ListenAndServe(address, r) } func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { diff --git a/internal/staticFiles.go b/internal/staticFiles.go index 3c36c17..5556cb7 100644 --- a/internal/staticFiles.go +++ b/internal/staticFiles.go @@ -26,7 +26,7 @@ func (spa *spaFileSystem) Open(name string) (http.File, error) { return f, err } -func initStatic() { +func initStatic(e *mux.Router) { staticFS, _ := fs.Sub(staticFiles, "static") - http.Handle("/", http.FileServer(&spaFileSystem{http.FS(staticFS)})) + r.Handle("/", http.FileServer(&spaFileSystem{http.FS(staticFS)})) }