2021-01-16 22:00:17 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-02-03 03:55:35 +00:00
|
|
|
"encoding/json"
|
2021-02-01 23:24:09 +00:00
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2021-02-03 03:55:35 +00:00
|
|
|
|
2021-02-02 20:34:10 +00:00
|
|
|
"reichard.io/imagini/graph"
|
|
|
|
"reichard.io/imagini/graph/generated"
|
2021-01-16 22:00:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (api *API) registerRoutes() {
|
2021-02-02 20:34:10 +00:00
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
// 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
|
2021-02-02 20:34:10 +00:00
|
|
|
api.Router.Handle("/playground", playground.Handler("GraphQL playground", "/query"))
|
2021-02-03 03:55:35 +00:00
|
|
|
api.Router.Handle("/query", api.injectContextMiddleware(srv))
|
2021-02-02 20:34:10 +00:00
|
|
|
|
2021-02-03 03:55:35 +00:00
|
|
|
// Handle Resource Route
|
2021-01-22 05:00:55 +00:00
|
|
|
api.Router.HandleFunc("/media/", multipleMiddleware(
|
|
|
|
api.mediaHandler,
|
|
|
|
api.authMiddleware,
|
|
|
|
))
|
2021-02-03 03:55:35 +00:00
|
|
|
|
2021-01-16 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func errorJSON(w http.ResponseWriter, err string, code int) {
|
2021-02-03 03:55:35 +00:00
|
|
|
errStruct := &APIResponse{Error: &APIError{Message: err, Code: int64(code)}}
|
2021-02-01 23:24:09 +00:00
|
|
|
responseJSON(w, errStruct, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func responseJSON(w http.ResponseWriter, msg interface{}, code int) {
|
2021-01-16 22:00:17 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.WriteHeader(code)
|
2021-02-01 23:24:09 +00:00
|
|
|
json.NewEncoder(w).Encode(msg)
|
2021-01-16 22:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
}
|