1
Fork 0
photoview/api/server.go

139 lines
4.0 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
"path"
"time"
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
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/database"
"github.com/photoview/photoview/api/dataloader"
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/graphql/auth"
"github.com/photoview/photoview/api/routes"
"github.com/photoview/photoview/api/scanner"
2021-04-03 21:39:32 +02:00
"github.com/photoview/photoview/api/scanner/exif"
2021-02-15 17:35:28 +01:00
"github.com/photoview/photoview/api/scanner/face_detection"
"github.com/photoview/photoview/api/scanner/media_encoding/executable_worker"
2020-12-17 22:51:43 +01:00
"github.com/photoview/photoview/api/server"
"github.com/photoview/photoview/api/utils"
2020-01-30 14:49:39 +01:00
2020-01-30 14:28:14 +01:00
"github.com/99designs/gqlgen/handler"
2020-12-17 22:51:43 +01:00
photoview_graphql "github.com/photoview/photoview/api/graphql"
"github.com/photoview/photoview/api/graphql/resolvers"
2020-01-30 14:28:14 +01:00
)
func main() {
2020-01-30 14:49:39 +01:00
log.Println("Starting Photoview...")
2020-01-30 14:49:39 +01:00
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
devMode := utils.DevelopmentMode()
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
// 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)
}
executable_worker.InitializeExecutableWorkers()
2021-04-03 21:39:32 +02:00
exif.InitializeEXIFParser()
2021-02-15 17:35:28 +01:00
if err := face_detection.InitializeFaceDetector(db); err != nil {
log.Panicf("Could not initialize face detector: %s\n", err)
}
rootRouter := mux.NewRouter()
2020-01-31 23:30:34 +01:00
rootRouter.Use(dataloader.Middleware(db))
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
graphqlDirective.IsAuthorized = photoview_graphql.IsAuthorized
2020-01-31 23:30:34 +01:00
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.WebsocketKeepAliveDuration(time.Second*10),
2020-02-21 17:53:04 +01:00
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 := utils.ShouldServeUI()
if shouldServeUI {
spa := routes.NewSpaHandler(utils.UIPath(), "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())
apiEndpoint := utils.ApiEndpointUrl()
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
if uiEndpoint := utils.UiEndpointUrl(); uiEndpoint != nil {
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
} else {
log.Println("Photoview UI public endpoint ready at /")
}
if !shouldServeUI {
log.Printf("Notice: UI is not served by the the api (%s=0)", utils.EnvServeUI.GetName())
}
}
log.Panic(http.ListenAndServe(":"+apiListenURL.Port(), handlers.CompressHandler(rootRouter)))
2020-01-30 14:28:14 +01:00
}