1
Fork 0
photoview/api/server.go

122 lines
3.3 KiB
Go
Raw Normal View History

2020-01-30 14:28:14 +01:00
package main
import (
"log"
"net/http"
"os"
2020-02-19 21:33:28 +01:00
"path"
2020-01-30 14:28:14 +01:00
2020-10-04 16:34:47 +02:00
"github.com/gorilla/handlers"
"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/scanner"
"github.com/viktorstrate/photoview/api/server"
"github.com/viktorstrate/photoview/api/utils"
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
)
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"
db, err := database.SetupDatabase()
if err != nil {
log.Panicf("Could not connect to database: %s\n", err)
}
2020-01-31 15:22:58 +01:00
defer db.Close()
// Migrate database
if err := database.MigrateDatabase(db); err != nil {
2020-04-15 16:20:09 +02:00
log.Panicf("Could not migrate database: %s\n", err)
2020-01-31 15:22:58 +01:00
}
if err := scanner.InitializeScannerQueue(db); err != nil {
log.Panicf("Could not initialize scanner queue: %s\n", err)
}
if err := scanner.InitializePeriodicScanner(db); err != nil {
log.Panicf("Could not initialize periodic scanner: %s", err)
}
scanner.InitializeExecutableWorkers()
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
apiListenUrl := utils.ApiListenUrl()
2020-02-19 21:33:28 +01:00
endpointRouter := rootRouter.PathPrefix(apiListenUrl.Path).Subrouter()
if devMode {
endpointRouter.Handle("/", handler.Playground("GraphQL playground", path.Join(apiListenUrl.Path, "/graphql")))
} else {
endpointRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("photoview api endpoint"))
})
}
endpointRouter.Handle("/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
photoRouter := endpointRouter.PathPrefix("/photo").Subrouter()
routes.RegisterPhotoRoutes(db, photoRouter)
2020-02-09 12:53:21 +01:00
2020-07-11 15:57:58 +02:00
videoRouter := endpointRouter.PathPrefix("/video").Subrouter()
routes.RegisterVideoRoutes(db, videoRouter)
shouldServeUI := os.Getenv("SERVE_UI") == "1"
if shouldServeUI {
2020-04-06 22:21:58 +02:00
spa := routes.NewSpaHandler("/ui", "index.html")
rootRouter.PathPrefix("/").Handler(spa)
}
2020-03-01 02:06:18 +01:00
if devMode {
log.Printf("🚀 Graphql playground ready at %s\n", apiListenUrl.String())
} else {
log.Printf("Photoview API endpoint listening at %s\n", apiListenUrl.String())
uiEndpoint := utils.UiEndpointUrl()
apiEndpoint := utils.ApiEndpointUrl()
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
if !shouldServeUI {
log.Printf("Notice: UI is not served by the the api (SERVE_UI=0)")
}
}
2020-10-04 16:34:47 +02:00
log.Panic(http.ListenAndServe(":"+apiListenUrl.Port(), handlers.CompressHandler(rootRouter)))
2020-01-30 14:28:14 +01:00
}