From 08f0cb6c8091eb79cc48b7b878faa38b5bb8f8da Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 27 Mar 2021 19:33:27 +1300 Subject: [PATCH] Add MBID Add ability to set custom port --- .env.example | 3 ++- cmd/go-scrobble/main.go | 12 ++++++++++-- internal/goscrobble/db.go | 4 ++++ internal/goscrobble/server.go | 5 +++-- migrations/4_mbid.up.sql | 7 +++++++ 5 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 migrations/4_mbid.up.sql diff --git a/.env.example b/.env.example index ceaba4b8..911a1e45 100644 --- a/.env.example +++ b/.env.example @@ -6,4 +6,5 @@ MYSQL_DB= JWT_SECRET= JWT_EXPIRY=86400 -REVERSE_PROXIES=127.0.0.1 \ No newline at end of file +REVERSE_PROXIES=127.0.0.1 +PORT=42069 \ No newline at end of file diff --git a/cmd/go-scrobble/main.go b/cmd/go-scrobble/main.go index 4662ef56..252ce049 100644 --- a/cmd/go-scrobble/main.go +++ b/cmd/go-scrobble/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "os" "strconv" @@ -12,6 +13,7 @@ import ( ) func main() { + fmt.Println("Starting goscrobble") err := godotenv.Load() if err != nil { log.Fatal("Error loading .env file") @@ -35,10 +37,16 @@ func main() { // Ignore reverse proxies goscrobble.ReverseProxies = strings.Split(os.Getenv("REVERSE_PROXIES"), ",") - // // Boot up DB connection for life of application + // Store port + port := os.Getenv("PORT") + if port == "" { + port = "42069" + } + + // Boot up DB connection for life of application goscrobble.InitDb() defer goscrobble.CloseDbConn() // Boot up API webserver \o/ - goscrobble.HandleRequests() + goscrobble.HandleRequests(port) } diff --git a/internal/goscrobble/db.go b/internal/goscrobble/db.go index b30fdb1c..d3b7f9a0 100644 --- a/internal/goscrobble/db.go +++ b/internal/goscrobble/db.go @@ -48,6 +48,7 @@ func CloseDbConn() { } func runMigrations() { + fmt.Println("Checking database migrations") driver, err := mysql.WithInstance(db, &mysql.Config{}) if err != nil { log.Fatalf("Unable to run migrations! %v", err) @@ -66,11 +67,14 @@ func runMigrations() { if err != nil { // Skip 'no change'. This is fine. Everything is fine. if err.Error() == "no change" { + fmt.Println("Database already up to date") return } panic(fmt.Errorf("Error running DB Migrations %v", err)) } + + fmt.Println("Database migrations complete") } func getDbCount(query string, args ...interface{}) (int, error) { diff --git a/internal/goscrobble/server.go b/internal/goscrobble/server.go index d66c146a..8a844e38 100644 --- a/internal/goscrobble/server.go +++ b/internal/goscrobble/server.go @@ -38,7 +38,7 @@ func enableCors(w *http.ResponseWriter) { } // HandleRequests - Boot HTTP! -func HandleRequests() { +func HandleRequests(port string) { // Create a new router r := mux.NewRouter().StrictSlash(true) @@ -70,7 +70,8 @@ func HandleRequests() { handler := c.Handler(r) // Serve it up! - log.Fatal(http.ListenAndServe(":42069", handler)) + fmt.Printf("Goscrobble listening on port %s", port) + log.Fatal(http.ListenAndServe(":"+port, handler)) } // MIDDLEWARE diff --git a/migrations/4_mbid.up.sql b/migrations/4_mbid.up.sql new file mode 100644 index 00000000..e7627442 --- /dev/null +++ b/migrations/4_mbid.up.sql @@ -0,0 +1,7 @@ +START TRANSACTION; + +ALTER TABLE albums ADD COLUMN `mbid` VARCHAR(36) DEFAULT NULL; +ALTER TABLE artists ADD COLUMN `mbid` VARCHAR(36) DEFAULT NULL; +ALTER TABLE tracks ADD COLUMN `mbid` VARCHAR(36) DEFAULT NULL; + +COMMIT;