Merge branch 'v2.0.0' into v2/multi-database-support

This commit is contained in:
viktorstrate
2021-01-17 12:21:32 +01:00
9 changed files with 60 additions and 38 deletions

View File

@@ -1,10 +1,12 @@
package database
import (
"context"
"log"
"net/url"
"os"
"strings"
"time"
"github.com/photoview/photoview/api/graphql/models"
"github.com/pkg/errors"
@@ -60,11 +62,31 @@ func SetupDatabase() (*gorm.DB, error) {
}
db, err := gorm.Open(databaseDialect, &config)
if err != nil {
return nil, errors.Wrap(err, "could not connect to database")
sqlDB, dbErr := db.DB()
if dbErr != nil {
log.Println(dbErr)
return nil, dbErr
}
// TODO: Add connection retries
sqlDB.SetMaxOpenConns(80)
if err != nil {
for retryCount := 1; retryCount <= 5; retryCount++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := sqlDB.PingContext(ctx); err == nil {
cancel()
return db, nil
}
cancel()
log.Printf("WARN: Could not ping database: %s. Will retry after 5 seconds\n", err)
time.Sleep(time.Duration(5) * time.Second)
}
return nil, err
}
return db, nil
}

View File

@@ -2,17 +2,17 @@
MYSQL_URL=user:password@tcp(localhost)/dbname
API_LISTEN_IP=localhost
API_LISTEN_PORT=4001
PHOTOVIEW_LISTEN_IP=localhost
PHOTOVIEW_LISTEN_PORT=4001
# The url from which the server can be accessed publicly
API_ENDPOINT=http://localhost:4001/
UI_ENDPOINT=http://localhost:1234/
PHOTOVIEW_API_ENDPOINT=http://localhost:4001/
PHOTOVIEW_UI_ENDPOINT=http://localhost:1234/
# Set to 1 for the server to also serve the built static ui files
SERVE_UI=0
# When SERVE_UI is 1, PUBLIC_ENDPOINT is used instead of API_ENDPOINT and UI_ENDPOINT
#PUBLIC_ENDPOINT=http://localhost:4001/
PHOTOVIEW_SERVE_UI=0
# When PHOTOVIEW_SERVE_UI is 1, PHOTOVIEW_PUBLIC_ENDPOINT is used instead of PHOTOVIEW_API_ENDPOINT and PHOTOVIEW_UI_ENDPOINT
#PHOTOVIEW_PUBLIC_ENDPOINT=http://localhost:4001/
# Enter a valid mapbox token, to enable maps feature
# A token can be created for free at https://mapbox.com
@@ -20,4 +20,4 @@ SERVE_UI=0
# Set to 1 to set server in development mode, this enables graphql playground
# Remove this if running in production
DEVELOPMENT=1
PHOTOVIEW_DEVELOPMENT_MODE=1

View File

@@ -29,7 +29,7 @@ func main() {
log.Println("No .env file found")
}
devMode := os.Getenv("DEVELOPMENT") == "1"
devMode := os.Getenv("PHOTOVIEW_DEVELOPMENT_MODE") == "1"
db, err := database.SetupDatabase()
if err != nil {
@@ -92,7 +92,7 @@ func main() {
videoRouter := endpointRouter.PathPrefix("/video").Subrouter()
routes.RegisterVideoRoutes(db, videoRouter)
shouldServeUI := os.Getenv("SERVE_UI") == "1"
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
if shouldServeUI {
spa := routes.NewSpaHandler("/ui", "index.html")
@@ -111,7 +111,7 @@ func main() {
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)")
log.Printf("Notice: UI is not served by the the api (PHOTOVIEW_SERVE_UI=0)")
}
}

View File

@@ -30,7 +30,7 @@ func WebsocketUpgrader(devMode bool) websocket.Upgrader {
if uiEndpoint.Host == originURL.Host {
return true
} else {
log.Printf("Not allowing websocket request from %s because it doesn't match UI_ENDPOINT %s", originURL.Host, uiEndpoint.Host)
log.Printf("Not allowing websocket request from %s because it doesn't match PHOTOVIEW_UI_ENDPOINT %s", originURL.Host, uiEndpoint.Host)
return false
}
}

View File

@@ -12,7 +12,7 @@ import (
func ApiListenUrl() *url.URL {
const defaultPort = "4001"
shouldServeUI := os.Getenv("SERVE_UI") == "1"
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
apiPrefix := "/"
if shouldServeUI {
@@ -21,19 +21,19 @@ func ApiListenUrl() *url.URL {
var listenAddr string
listenAddr = os.Getenv("API_LISTEN_IP")
listenAddr = os.Getenv("PHOTOVIEW_LISTEN_IP")
if listenAddr == "" {
listenAddr = "127.0.0.1"
}
listenPortStr := os.Getenv("API_LISTEN_PORT")
listenPortStr := os.Getenv("PHOTOVIEW_LISTEN_PORT")
if listenPortStr == "" {
listenPortStr = defaultPort
}
listenPort, err := strconv.Atoi(listenPortStr)
if err != nil {
log.Fatalf("API_LISTEN_PORT must be a number: '%s'\n%s", listenPortStr, err)
log.Fatalf("PHOTOVIEW_LISTEN_PORT must be a number: '%s'\n%s", listenPortStr, err)
}
apiUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", listenAddr, listenPort))
@@ -46,16 +46,16 @@ func ApiListenUrl() *url.URL {
}
func ApiEndpointUrl() *url.URL {
apiEndpointStr := os.Getenv("API_ENDPOINT")
apiEndpointStr := os.Getenv("PHOTOVIEW_API_ENDPOINT")
shouldServeUI := os.Getenv("SERVE_UI") == "1"
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
if shouldServeUI {
apiEndpointStr = os.Getenv("PUBLIC_ENDPOINT")
apiEndpointStr = os.Getenv("PHOTOVIEW_PUBLIC_ENDPOINT")
}
apiEndpointUrl, err := url.Parse(apiEndpointStr)
if err != nil {
log.Fatalf("ERROR: Environment variable API_ENDPOINT is not a proper url")
log.Fatalf("ERROR: Environment variable PHOTOVIEW_API_ENDPOINT is not a proper url")
}
if shouldServeUI {
@@ -66,16 +66,16 @@ func ApiEndpointUrl() *url.URL {
}
func UiEndpointUrl() *url.URL {
uiEndpointStr := os.Getenv("UI_ENDPOINT")
uiEndpointStr := os.Getenv("PHOTOVIEW_UI_ENDPOINT")
shouldServeUI := os.Getenv("SERVE_UI") == "1"
shouldServeUI := os.Getenv("PHOTOVIEW_SERVE_UI") == "1"
if shouldServeUI {
uiEndpointStr = os.Getenv("PUBLIC_ENDPOINT")
uiEndpointStr = os.Getenv("PHOTOVIEW_PUBLIC_ENDPOINT")
}
uiEndpointUrl, err := url.Parse(uiEndpointStr)
if err != nil {
log.Fatalf("ERROR: Environment variable UI_ENDPOINT is not a proper url")
log.Fatalf("ERROR: Environment variable PHOTOVIEW_UI_ENDPOINT is not a proper url")
}
return uiEndpointUrl