2020-01-30 14:28:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-17 05:55:20 +01:00
|
|
|
"github.com/photoview/photoview/api/scanner/watcher_scanner"
|
2020-01-30 14:28:14 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
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"
|
2020-02-21 16:50:50 +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
|
|
|
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/database"
|
2021-05-06 21:54:31 +02:00
|
|
|
"github.com/photoview/photoview/api/dataloader"
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/graphql/auth"
|
2021-11-11 18:57:02 +01:00
|
|
|
graphql_endpoint "github.com/photoview/photoview/api/graphql/endpoint"
|
2020-12-17 22:51:43 +01:00
|
|
|
"github.com/photoview/photoview/api/routes"
|
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"
|
2021-05-06 21:54:31 +02:00
|
|
|
"github.com/photoview/photoview/api/scanner/media_encoding/executable_worker"
|
2022-02-14 23:57:45 +01:00
|
|
|
"github.com/photoview/photoview/api/scanner/periodic_scanner"
|
|
|
|
"github.com/photoview/photoview/api/scanner/scanner_queue"
|
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
|
|
|
|
2021-11-11 18:57:02 +01:00
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2020-01-30 14:28:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-01-30 14:49:39 +01:00
|
|
|
|
2021-03-03 13:32:44 +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")
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
devMode := utils.DevelopmentMode()
|
2020-02-19 22:28:21 +01:00
|
|
|
|
2020-04-15 16:18:51 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-14 23:57:45 +01:00
|
|
|
if err := scanner_queue.InitializeScannerQueue(db); err != nil {
|
2020-09-21 21:32:30 +02:00
|
|
|
log.Panicf("Could not initialize scanner queue: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:57:45 +01:00
|
|
|
if err := periodic_scanner.InitializePeriodicScanner(db); err != nil {
|
2020-09-21 21:32:30 +02:00
|
|
|
log.Panicf("Could not initialize periodic scanner: %s", err)
|
|
|
|
}
|
2020-06-23 00:40:47 +02:00
|
|
|
|
2023-01-17 05:55:20 +01:00
|
|
|
if err := watcher_scanner.InitializeWatcherScanner(db); err != nil {
|
|
|
|
log.Panicf("Could not initialize watcher scanner: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-06 21:54:31 +02:00
|
|
|
executable_worker.InitializeExecutableWorkers()
|
2020-09-23 14:34:26 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-21 16:50:50 +01:00
|
|
|
rootRouter := mux.NewRouter()
|
2020-01-31 23:30:34 +01:00
|
|
|
|
2021-02-07 22:17:33 +01:00
|
|
|
rootRouter.Use(dataloader.Middleware(db))
|
2020-02-21 20:51:50 +01:00
|
|
|
rootRouter.Use(auth.Middleware(db))
|
|
|
|
rootRouter.Use(server.LoggingMiddleware)
|
2020-02-21 16:50:50 +01:00
|
|
|
rootRouter.Use(server.CORSMiddleware(devMode))
|
2020-02-19 21:33:28 +01:00
|
|
|
|
2021-03-01 22:03:57 +01:00
|
|
|
apiListenURL := utils.ApiListenUrl()
|
2020-02-19 21:33:28 +01:00
|
|
|
|
2021-03-01 22:03:57 +01:00
|
|
|
endpointRouter := rootRouter.PathPrefix(apiListenURL.Path).Subrouter()
|
2020-02-21 16:50:50 +01:00
|
|
|
|
|
|
|
if devMode {
|
2021-11-11 18:57:02 +01:00
|
|
|
endpointRouter.Handle("/", playground.Handler("GraphQL playground", path.Join(apiListenURL.Path, "/graphql")))
|
2020-02-21 16:50:50 +01:00
|
|
|
} else {
|
2020-04-06 22:16:25 +02:00
|
|
|
endpointRouter.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
2020-02-21 16:50:50 +01:00
|
|
|
w.Write([]byte("photoview api endpoint"))
|
|
|
|
})
|
|
|
|
}
|
2020-02-19 22:28:21 +01:00
|
|
|
|
2021-11-11 18:57:02 +01:00
|
|
|
endpointRouter.Handle("/graphql", graphql_endpoint.GraphqlEndpoint(db))
|
2020-01-30 14:28:14 +01:00
|
|
|
|
2020-04-06 22:16:25 +02:00
|
|
|
photoRouter := endpointRouter.PathPrefix("/photo").Subrouter()
|
2020-02-21 16:50:50 +01:00
|
|
|
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)
|
|
|
|
|
2021-09-26 00:21:02 +02:00
|
|
|
downloadsRouter := endpointRouter.PathPrefix("/download").Subrouter()
|
|
|
|
routes.RegisterDownloadRoutes(db, downloadsRouter)
|
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
shouldServeUI := utils.ShouldServeUI()
|
2020-04-06 22:16:25 +02:00
|
|
|
|
|
|
|
if shouldServeUI {
|
2021-02-26 15:10:28 +01:00
|
|
|
spa := routes.NewSpaHandler(utils.UIPath(), "index.html")
|
2020-04-06 22:16:25 +02:00
|
|
|
rootRouter.PathPrefix("/").Handler(spa)
|
|
|
|
}
|
2020-03-01 02:06:18 +01:00
|
|
|
|
2020-02-19 22:28:21 +01:00
|
|
|
if devMode {
|
2021-03-01 22:03:57 +01:00
|
|
|
log.Printf("🚀 Graphql playground ready at %s\n", apiListenURL.String())
|
2020-02-19 22:28:21 +01:00
|
|
|
} else {
|
2021-03-01 22:03:57 +01:00
|
|
|
log.Printf("Photoview API endpoint listening at %s\n", apiListenURL.String())
|
2020-04-06 22:16:25 +02:00
|
|
|
|
|
|
|
apiEndpoint := utils.ApiEndpointUrl()
|
|
|
|
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
|
2021-03-01 22:03:57 +01:00
|
|
|
|
|
|
|
if uiEndpoint := utils.UiEndpointUrl(); uiEndpoint != nil {
|
|
|
|
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
|
2021-03-01 22:10:20 +01:00
|
|
|
} else {
|
|
|
|
log.Println("Photoview UI public endpoint ready at /")
|
2021-03-01 22:03:57 +01:00
|
|
|
}
|
2020-04-05 18:54:13 +02:00
|
|
|
|
2020-04-30 14:59:29 +02:00
|
|
|
if !shouldServeUI {
|
2021-01-17 16:50:48 +01:00
|
|
|
log.Printf("Notice: UI is not served by the the api (%s=0)", utils.EnvServeUI.GetName())
|
2020-04-30 14:59:29 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 22:28:21 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 22:03:57 +01:00
|
|
|
log.Panic(http.ListenAndServe(":"+apiListenURL.Port(), handlers.CompressHandler(rootRouter)))
|
2020-01-30 14:28:14 +01:00
|
|
|
}
|