changed http handler to mux

This commit is contained in:
Djeeberjr 2021-09-11 22:17:22 +02:00
parent 0daa71dc72
commit 4c4ef95ee7
5 changed files with 29 additions and 18 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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" {
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)
return
}
}).Methods("GET")
if r.Method == "POST" {
r.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) {
httpPostFile(resolveContext, rw, r)
return
}
})
}).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) {

View File

@ -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)}))
}