52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"encoding/json"
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
|
|
|
"reichard.io/imagini/graph"
|
|
"reichard.io/imagini/graph/generated"
|
|
)
|
|
|
|
func (api *API) registerRoutes() {
|
|
|
|
// Set up Directives
|
|
c := generated.Config{ Resolvers: &graph.Resolver{ DB: api.DB } }
|
|
c.Directives.HasMinRole = hasMinRoleDirective
|
|
c.Directives.Meta = metaDirective
|
|
srv := handler.NewDefaultServer(generated.NewExecutableSchema(c))
|
|
|
|
// Handle GraphQL
|
|
api.Router.Handle("/playground", playground.Handler("GraphQL playground", "/query"))
|
|
api.Router.Handle("/query", api.injectContextMiddleware(srv))
|
|
|
|
// Handle Resource Route
|
|
api.Router.HandleFunc("/media/", multipleMiddleware(
|
|
api.mediaHandler,
|
|
api.authMiddleware,
|
|
))
|
|
|
|
}
|
|
|
|
func errorJSON(w http.ResponseWriter, err string, code int) {
|
|
errStruct := &APIResponse{Error: &APIError{Message: err, Code: int64(code)}}
|
|
responseJSON(w, errStruct, code)
|
|
}
|
|
|
|
func responseJSON(w http.ResponseWriter, msg interface{}, code int) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.WriteHeader(code)
|
|
json.NewEncoder(w).Encode(msg)
|
|
}
|
|
|
|
func successJSON(w http.ResponseWriter, msg string, code int) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.WriteHeader(code)
|
|
json.NewEncoder(w).Encode(map[string]interface{}{"success": msg})
|
|
}
|