From ef66e1c8dfdabdec51fa775b4c62d9ef323e2117 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 25 Dec 2021 13:30:55 +1300 Subject: [PATCH] Update scripts for new location --- .env.example | 4 +++- .gitlab-ci.yml | 2 +- cmd/go-scrobble/main.go | 12 ++++++++---- docs/config.md | 5 ++++- init/deploy.sh | 3 ++- init/goscrobble.service | 2 +- internal/goscrobble/image.go | 9 ++------- internal/goscrobble/server.go | 9 +++++---- 8 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.env.example b/.env.example index 90e2c543..87097742 100644 --- a/.env.example +++ b/.env.example @@ -22,4 +22,6 @@ MAIL_FROM_NAME= DEV_MODE=false GOSCROBBLE_DOMAIN="" -STATIC_DIR="web" + +DATA_DIR="/var/www/goscrobble-data" +FRONTEND_DIRECTORY="/var/www/goscrobble-web" diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 51a3efb2..989695e5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,7 +5,7 @@ variables: VERSION: 0.1.001 build-go: - image: golang:1.16.7 + image: golang:1.16 stage: build only: - master diff --git a/cmd/go-scrobble/main.go b/cmd/go-scrobble/main.go index 7d132ce8..e1e05751 100644 --- a/cmd/go-scrobble/main.go +++ b/cmd/go-scrobble/main.go @@ -51,10 +51,14 @@ func main() { goscrobble.RefereshExpiry = time.Duration(i) * time.Second } - goscrobble.StaticDirectory = "web" - staticDirectoryStr := os.Getenv("STATIC_DIR") - if staticDirectoryStr != "" { - goscrobble.StaticDirectory = staticDirectoryStr + goscrobble.DataDirectory = os.Getenv("DATA_DIRECTORY") + if goscrobble.DataDirectory == "" { + panic("DATA_DIRECTORY required in .env") + } + + goscrobble.FrontendDirectory = os.Getenv("FRONTEND_DIRECTORY") + if goscrobble.FrontendDirectory == "" { + panic("FRONTEND_DIRECTORY required in .env") } // Ignore reverse proxies diff --git a/docs/config.md b/docs/config.md index 8b066223..e3d123d8 100644 --- a/docs/config.md +++ b/docs/config.md @@ -32,4 +32,7 @@ These are stored in `web/.env.production` and `web/.env.development` DEV_MODE=false // true|false - Defaults false GOSCROBBLE_DOMAIN="" // Full domain for email links (https://goscrobble.com)) - STATIC_DIR="web" // Location to store images (This will serve from web/static) + + DATA_DIRECTORY="/var/www/goscrobble-data" + FRONTEND_DIRECTORY="/var/www/goscrobble-web" + diff --git a/init/deploy.sh b/init/deploy.sh index 33a8cd12..9ab148c7 100755 --- a/init/deploy.sh +++ b/init/deploy.sh @@ -2,12 +2,13 @@ # Easy deploy script.. echo 'Fetching latest git commit' +cd /var/www/goscrobble-api git pull echo 'Building backend' go build -o goscrobble cmd/go-scrobble/*.go -cd web +cd /var/www/goscrobble-web echo 'Installing frontend packages' npm install --production diff --git a/init/goscrobble.service b/init/goscrobble.service index 30578c8f..0483226d 100644 --- a/init/goscrobble.service +++ b/init/goscrobble.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=simple User=root -ExecStart=/bin/bash -c 'cd /root/go-scrobble && ./goscrobble' +ExecStart=/bin/bash -c 'cd /var/www/goscrobble-api && ./goscrobble' Restart=on-failure [Install] diff --git a/internal/goscrobble/image.go b/internal/goscrobble/image.go index 6e42f230..cb1b584c 100644 --- a/internal/goscrobble/image.go +++ b/internal/goscrobble/image.go @@ -8,13 +8,8 @@ import ( ) func importImage(uuid string, url string) error { - // Create the file - path, err := os.Getwd() - if err != nil { - return err - } - - out, err := os.Create(path + string(os.PathSeparator) + StaticDirectory + string(os.PathSeparator) + "img" + string(os.PathSeparator) + uuid + "_full.jpg") + // Create image + out, err := os.Create(DataDirectory + string(os.PathSeparator) + "img" + string(os.PathSeparator) + uuid + "_full.jpg") if err != nil { return err } diff --git a/internal/goscrobble/server.go b/internal/goscrobble/server.go index 8a910734..243ee09f 100644 --- a/internal/goscrobble/server.go +++ b/internal/goscrobble/server.go @@ -22,8 +22,9 @@ type jsonResponse struct { // List of Reverse proxies var ReverseProxies []string -// Static image directory -var StaticDirectory string +// Directories +var FrontendDirectory string +var DataDirectory string // RequestRequest - Incoming JSON! type RequestRequest struct { @@ -96,14 +97,14 @@ func HandleRequests(port string) { r.PathPrefix("/api") // SERVE STATIC FILES - NO AUTH - spaStatic := spaStaticHandler{staticPath: StaticDirectory} + spaStatic := spaStaticHandler{staticPath: DataDirectory} r.PathPrefix("/img").Handler(spaStatic) apiDocs := spaStaticHandler{staticPath: "docs/api/build"} r.PathPrefix("/docs").Handler(apiDocs) // SERVE FRONTEND - NO AUTH - spa := spaHandler{staticPath: "web/build", indexPath: "index.html"} + spa := spaHandler{staticPath: FrontendDirectory + string(os.PathSeparator) + "build", indexPath: "index.html"} r.PathPrefix("/").Handler(spa) c := cors.New(cors.Options{