1
Fork 0

Enable api devMode to secure endpoint in prod

This commit is contained in:
viktorstrate 2020-02-19 22:28:21 +01:00
parent a505d28f00
commit 8f0489c706
2 changed files with 22 additions and 4 deletions

View File

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

View File

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