changed http handler to mux

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

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" {
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) {

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