1
Fork 0
photoview/api/server.go

144 lines
4.2 KiB
Go
Raw Normal View History

2020-01-30 14:28:14 +01:00
package main
import (
"log"
"net/http"
2020-02-19 21:33:28 +01:00
"net/url"
2020-01-30 14:28:14 +01:00
"os"
2020-02-19 21:33:28 +01:00
"path"
2020-03-01 02:06:18 +01:00
"path/filepath"
2020-01-30 14:28:14 +01:00
"github.com/gorilla/mux"
2020-02-05 14:51:46 +01:00
"github.com/joho/godotenv"
2020-01-30 14:49:39 +01:00
"github.com/viktorstrate/photoview/api/database"
2020-01-31 23:30:34 +01:00
"github.com/viktorstrate/photoview/api/graphql/auth"
2020-02-09 12:53:21 +01:00
"github.com/viktorstrate/photoview/api/routes"
"github.com/viktorstrate/photoview/api/server"
2020-01-30 14:49:39 +01:00
2020-01-30 14:28:14 +01:00
"github.com/99designs/gqlgen/handler"
photoview_graphql "github.com/viktorstrate/photoview/api/graphql"
2020-02-05 14:51:46 +01:00
"github.com/viktorstrate/photoview/api/graphql/resolvers"
2020-01-30 14:28:14 +01:00
)
2020-01-30 14:49:39 +01:00
const defaultPort = "4001"
2020-01-30 14:28:14 +01:00
2020-03-01 02:06:18 +01:00
// spaHandler implements the http.Handler interface, so we can use it
// to respond to HTTP requests. The path to the static directory and
// path to the index file within that static directory are used to
// serve the SPA in the given static directory.
type spaHandler struct {
staticPath string
indexPath string
}
// ServeHTTP inspects the URL path to locate a file within the static dir
// on the SPA handler. If a file is found, it will be served. If not, the
// file located at the index path on the SPA handler will be served. This
// is suitable behavior for serving an SPA (single page application).
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// get the absolute path to prevent directory traversal
path, err := filepath.Abs(r.URL.Path)
if err != nil {
// if we failed to get the absolute path respond with a 400 bad request
// and stop
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// prepend the path with the path to the static directory
path = filepath.Join(h.staticPath, path)
// check whether a file exists at the given path
_, err = os.Stat(path)
if os.IsNotExist(err) {
// file does not exist, serve index.html
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
} else if err != nil {
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// otherwise, use http.FileServer to serve the static dir
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}
2020-01-30 14:28:14 +01:00
func main() {
2020-01-30 14:49:39 +01:00
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
devMode := os.Getenv("DEVELOPMENT") == "1"
2020-02-19 21:33:28 +01:00
port := os.Getenv("API_LISTEN_PORT")
2020-01-30 14:28:14 +01:00
if port == "" {
port = defaultPort
}
2020-01-31 15:22:58 +01:00
db := database.SetupDatabase()
defer db.Close()
// Migrate database
if err := database.MigrateDatabase(db); err != nil {
log.Fatalf("Could not migrate database: %s\n", err)
}
rootRouter := mux.NewRouter()
2020-01-31 23:30:34 +01:00
2020-02-21 20:51:50 +01:00
rootRouter.Use(auth.Middleware(db))
rootRouter.Use(server.LoggingMiddleware)
rootRouter.Use(server.CORSMiddleware(devMode))
2020-02-19 21:33:28 +01:00
2020-02-05 14:51:46 +01:00
graphqlResolver := resolvers.Resolver{Database: db}
2020-01-31 23:30:34 +01:00
graphqlDirective := photoview_graphql.DirectiveRoot{}
graphqlDirective.IsAdmin = photoview_graphql.IsAdmin(db)
graphqlConfig := photoview_graphql.Config{
Resolvers: &graphqlResolver,
Directives: graphqlDirective,
}
2020-01-30 14:49:39 +01:00
2020-02-19 21:33:28 +01:00
endpointURL, err := url.Parse(os.Getenv("API_ENDPOINT"))
if err != nil {
log.Println("WARN: Environment variable API_ENDPOINT not specified")
endpointURL, _ = url.Parse("/")
}
endpointRouter := rootRouter.PathPrefix(endpointURL.Path).Subrouter()
if devMode {
2020-03-01 02:06:18 +01:00
endpointRouter.Handle("/api", handler.Playground("GraphQL playground", path.Join(endpointURL.Path, "/graphql")))
} else {
2020-03-01 02:06:18 +01:00
endpointRouter.HandleFunc("/api", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("photoview api endpoint"))
})
}
2020-03-01 02:06:18 +01:00
endpointRouter.Handle("/api/graphql",
2020-02-21 17:53:04 +01:00
handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig),
handler.IntrospectionEnabled(devMode),
handler.WebsocketUpgrader(server.WebsocketUpgrader(devMode)),
handler.WebsocketInitFunc(auth.AuthWebsocketInit(db)),
),
)
2020-01-30 14:28:14 +01:00
2020-03-01 02:06:18 +01:00
photoRouter := endpointRouter.PathPrefix("/api/photo").Subrouter()
routes.RegisterPhotoRoutes(db, photoRouter)
2020-02-09 12:53:21 +01:00
2020-03-01 02:06:18 +01:00
spa := spaHandler{staticPath: "/ui", indexPath: "index.html"}
endpointRouter.PathPrefix("/").Handler(spa)
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, rootRouter))
2020-01-30 14:28:14 +01:00
}