diff --git a/controller/migrator.go b/controller/migrator.go index 46a564d..3624255 100644 --- a/controller/migrator.go +++ b/controller/migrator.go @@ -20,9 +20,13 @@ func CheckDBVersionAndMigrate(db *sqlx.DB) { ) oldDBVersion := 0 - - err := db.Get(&oldDBVersion, "SELECT MAX(version) FROM schema_version") + schemaVersionCount := 0 + err := db.Get(&schemaVersionCount, "SELECT COUNT(*) FROM schema_version") if err != nil { panic(err) } + if schemaVersionCount > 0 { + err := db.Get(&oldDBVersion, "SELECT MAX(version) FROM schema_version") + if err != nil { panic(err) } + } for oldDBVersion < DB_VERSION { switch oldDBVersion { diff --git a/global/config.go b/global/config.go index 0115c3f..20e152f 100644 --- a/global/config.go +++ b/global/config.go @@ -13,6 +13,7 @@ import ( type ( dbConfig struct { Host string `toml:"host"` + Port int64 `toml:"port"` Name string `toml:"name"` User string `toml:"user"` Pass string `toml:"pass"` @@ -42,6 +43,12 @@ var Config = func() config { config := config{ BaseUrl: "https://arimelody.me", Port: 8080, + DB: dbConfig{ + Host: "127.0.0.1", + Port: 5432, + User: "arimelody", + Name: "arimelody", + }, } data, err := os.ReadFile(configFile) @@ -80,6 +87,10 @@ func handleConfigOverrides(config *config) error { if env, has := os.LookupEnv("ARIMELODY_DATA_DIR"); has { config.DataDirectory = env } if env, has := os.LookupEnv("ARIMELODY_DB_HOST"); has { config.DB.Host = env } + if env, has := os.LookupEnv("ARIMELODY_DB_PORT"); has { + config.DB.Port, err = strconv.ParseInt(env, 10, 0) + if err != nil { return errors.New("ARIMELODY_DB_PORT: " + err.Error()) } + } if env, has := os.LookupEnv("ARIMELODY_DB_NAME"); has { config.DB.Name = env } if env, has := os.LookupEnv("ARIMELODY_DB_USER"); has { config.DB.User = env } if env, has := os.LookupEnv("ARIMELODY_DB_PASS"); has { config.DB.Pass = env } diff --git a/main.go b/main.go index e257da5..cbda0a7 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,10 @@ const DEFAULT_PORT int64 = 8080 func main() { fmt.Printf("made with <3 by ari melody\n\n") + // TODO: refactor `global` to `AppState` + // this should contain `Config` and `DB`, and be passed through to all + // handlers that need it. it's better than weird static globals everywhere! + // initialise database connection if global.Config.DB.Host == "" { fmt.Fprintf(os.Stderr, "FATAL: db.host not provided! Exiting...\n") @@ -52,8 +56,9 @@ func main() { global.DB, err = sqlx.Connect( "postgres", fmt.Sprintf( - "host=%s user=%s dbname=%s password='%s' sslmode=disable", + "host=%s port=%d user=%s dbname=%s password='%s' sslmode=disable", global.Config.DB.Host, + global.Config.DB.Port, global.Config.DB.User, global.Config.DB.Name, global.Config.DB.Pass, @@ -319,7 +324,7 @@ func main() { os.Exit(1) } - fmt.Fprintf(os.Stdout, "No accounts exist! Generated invite code: " + string(invite.Code) + "\nUse this at %s/admin/register.\n", global.Config.BaseUrl) + fmt.Printf("No accounts exist! Generated invite code: %s\n", invite.Code) } // delete expired invites @@ -331,7 +336,7 @@ func main() { // start the web server! mux := createServeMux() - fmt.Printf("Now serving at http://127.0.0.1:%d\n", global.Config.Port) + fmt.Printf("Now serving at %s:%d\n", global.Config.BaseUrl, global.Config.Port) log.Fatal( http.ListenAndServe(fmt.Sprintf(":%d", global.Config.Port), global.HTTPLog(global.DefaultHeaders(mux)), diff --git a/schema_migration/000-init.sql b/schema_migration/000-init.sql index 2c6e5b1..3f4c723 100644 --- a/schema_migration/000-init.sql +++ b/schema_migration/000-init.sql @@ -1,10 +1,4 @@ -CREATE SCHEMA arimelody; - --- Schema verison -CREATE TABLE arimelody.schema_version ( - version INTEGER PRIMARY KEY, - applied_at TIMESTAMP DEFAULT current_timestamp -); +CREATE SCHEMA IF NOT EXISTS arimelody; -- -- Tables