Add ability to set custom port
This commit is contained in:
Daniel Mason 2021-03-27 19:33:27 +13:00
parent a22f282a6d
commit 08f0cb6c80
Signed by: idanoo
GPG Key ID: 387387CDBC02F132
5 changed files with 26 additions and 5 deletions

View File

@ -6,4 +6,5 @@ MYSQL_DB=
JWT_SECRET= JWT_SECRET=
JWT_EXPIRY=86400 JWT_EXPIRY=86400
REVERSE_PROXIES=127.0.0.1 REVERSE_PROXIES=127.0.0.1
PORT=42069

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"strconv" "strconv"
@ -12,6 +13,7 @@ import (
) )
func main() { func main() {
fmt.Println("Starting goscrobble")
err := godotenv.Load() err := godotenv.Load()
if err != nil { if err != nil {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
@ -35,10 +37,16 @@ func main() {
// Ignore reverse proxies // Ignore reverse proxies
goscrobble.ReverseProxies = strings.Split(os.Getenv("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() goscrobble.InitDb()
defer goscrobble.CloseDbConn() defer goscrobble.CloseDbConn()
// Boot up API webserver \o/ // Boot up API webserver \o/
goscrobble.HandleRequests() goscrobble.HandleRequests(port)
} }

View File

@ -48,6 +48,7 @@ func CloseDbConn() {
} }
func runMigrations() { func runMigrations() {
fmt.Println("Checking database migrations")
driver, err := mysql.WithInstance(db, &mysql.Config{}) driver, err := mysql.WithInstance(db, &mysql.Config{})
if err != nil { if err != nil {
log.Fatalf("Unable to run migrations! %v", err) log.Fatalf("Unable to run migrations! %v", err)
@ -66,11 +67,14 @@ func runMigrations() {
if err != nil { if err != nil {
// Skip 'no change'. This is fine. Everything is fine. // Skip 'no change'. This is fine. Everything is fine.
if err.Error() == "no change" { if err.Error() == "no change" {
fmt.Println("Database already up to date")
return return
} }
panic(fmt.Errorf("Error running DB Migrations %v", err)) panic(fmt.Errorf("Error running DB Migrations %v", err))
} }
fmt.Println("Database migrations complete")
} }
func getDbCount(query string, args ...interface{}) (int, error) { func getDbCount(query string, args ...interface{}) (int, error) {

View File

@ -38,7 +38,7 @@ func enableCors(w *http.ResponseWriter) {
} }
// HandleRequests - Boot HTTP! // HandleRequests - Boot HTTP!
func HandleRequests() { func HandleRequests(port string) {
// Create a new router // Create a new router
r := mux.NewRouter().StrictSlash(true) r := mux.NewRouter().StrictSlash(true)
@ -70,7 +70,8 @@ func HandleRequests() {
handler := c.Handler(r) handler := c.Handler(r)
// Serve it up! // Serve it up!
log.Fatal(http.ListenAndServe(":42069", handler)) fmt.Printf("Goscrobble listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, handler))
} }
// MIDDLEWARE // MIDDLEWARE

7
migrations/4_mbid.up.sql Normal file
View File

@ -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;