Enable api devMode to secure endpoint in prod
This commit is contained in:
parent
a505d28f00
commit
8f0489c706
|
@ -3,4 +3,8 @@
|
|||
MYSQL_URL=user:password@tcp(localhost)/dbname
|
||||
|
||||
API_ENDPOINT=http://localhost:4001/
|
||||
API_LISTEN_PORT=4001
|
||||
API_LISTEN_PORT=4001
|
||||
|
||||
# Set to 1 to set server in development mode, this enables graphql playground
|
||||
# Remove this if running in production
|
||||
DEVELOPMENT=1
|
||||
|
|
|
@ -29,6 +29,8 @@ func main() {
|
|||
log.Println("No .env file found")
|
||||
}
|
||||
|
||||
devMode := os.Getenv("DEVELOPMENT") == "1"
|
||||
|
||||
port := os.Getenv("API_LISTEN_PORT")
|
||||
if port == "" {
|
||||
port = defaultPort
|
||||
|
@ -71,12 +73,24 @@ func main() {
|
|||
}
|
||||
|
||||
router.Route(endpointURL.Path, func(router chi.Router) {
|
||||
router.Handle("/", handler.Playground("GraphQL playground", path.Join(endpointURL.Path, "/graphql")))
|
||||
router.Handle("/graphql", handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig)))
|
||||
if devMode {
|
||||
router.Handle("/", handler.Playground("GraphQL playground", path.Join(endpointURL.Path, "/graphql")))
|
||||
} else {
|
||||
router.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Write([]byte("photoview api endpoint"))
|
||||
})
|
||||
}
|
||||
|
||||
router.Handle("/graphql", handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig), handler.IntrospectionEnabled(devMode)))
|
||||
|
||||
router.Mount("/photo", routes.PhotoRoutes(db))
|
||||
})
|
||||
|
||||
log.Printf("🚀 Graphql playground ready at %s", endpointURL.String())
|
||||
if devMode {
|
||||
log.Printf("🚀 Graphql playground ready at %s", endpointURL.String())
|
||||
} else {
|
||||
log.Printf("Photoview API endpoint available at %s", endpointURL.String())
|
||||
}
|
||||
|
||||
log.Fatal(http.ListenAndServe(":"+port, router))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue