diff --git a/.env.example b/.env.example index 39b5b62..009ac70 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,9 @@ ENV=prod -WAKAPI_DB_TYPE=mysql # mysql, postgres, sqlite3 -WAKAPI_DB_NAME=wakapi_db # file path for sqlite, e.g. /tmp/wakapi.db +WAKAPI_DB_TYPE=mysql # mysql, postgres, sqlite3 +WAKAPI_DB_NAME=wakapi_db # file path for sqlite, e.g. /tmp/wakapi.db WAKAPI_DB_USER=myuser WAKAPI_DB_PASSWORD=shhh WAKAPI_DB_HOST=localhost WAKAPI_DB_PORT=3306 +WAKAPI_DEFAULT_USER_NAME=admin +WAKAPI_DEFAULT_USER_PASSWORD=admin # CHANGE! \ No newline at end of file diff --git a/main.go b/main.go index bb7a3e9..3dc6ba0 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,8 @@ func readConfig() *models.Config { dbHost := utils.LookupFatal("WAKAPI_DB_HOST") dbName := utils.LookupFatal("WAKAPI_DB_NAME") dbPortStr := utils.LookupFatal("WAKAPI_DB_PORT") + defaultUserName := utils.LookupFatal("WAKAPI_DEFAULT_USER_NAME") + defaultUserPassword := utils.LookupFatal("WAKAPI_DEFAULT_USER_PASSWORD") dbPort, err := strconv.Atoi(dbPortStr) cfg, err := ini.Load("config.ini") @@ -96,19 +98,21 @@ func readConfig() *models.Config { } return &models.Config{ - Env: env, - Port: port, - Addr: addr, - DbHost: dbHost, - DbPort: uint(dbPort), - DbUser: dbUser, - DbPassword: dbPassword, - DbName: dbName, - DbDialect: dbType, - DbMaxConn: dbMaxConn, - CleanUp: cleanUp, - CustomLanguages: customLangs, - LanguageColors: colors, + Env: env, + Port: port, + Addr: addr, + DbHost: dbHost, + DbPort: uint(dbPort), + DbUser: dbUser, + DbPassword: dbPassword, + DbName: dbName, + DbDialect: dbType, + DbMaxConn: dbMaxConn, + CleanUp: cleanUp, + DefaultUserName: defaultUserName, + DefaultUserPassword: defaultUserPassword, + CustomLanguages: customLangs, + LanguageColors: colors, } } @@ -263,16 +267,16 @@ func migrateLanguages(db *gorm.DB, cfg *models.Config) { } func addDefaultUser(db *gorm.DB, cfg *models.Config) { - pw := md5.Sum([]byte(models.DefaultPassword)) + pw := md5.Sum([]byte(cfg.DefaultUserPassword)) pwString := hex.EncodeToString(pw[:]) apiKey := uuid.NewV4().String() - u := &models.User{ID: models.DefaultUser, Password: pwString, ApiKey: apiKey} + u := &models.User{ID: cfg.DefaultUserName, Password: pwString, ApiKey: apiKey} result := db.FirstOrCreate(u, &models.User{ID: u.ID}) if result.Error != nil { log.Println("Unable to create default user.") log.Fatal(result.Error) } if result.RowsAffected > 0 { - log.Printf("Created default user '%s' with password '%s' and API key '%s'.\n", u.ID, models.DefaultPassword, u.ApiKey) + log.Printf("Created default user '%s' with password '%s' and API key '%s'.\n", u.ID, cfg.DefaultUserPassword, u.ApiKey) } } diff --git a/models/config.go b/models/config.go index 8cc21a7..0e07e15 100644 --- a/models/config.go +++ b/models/config.go @@ -1,19 +1,21 @@ package models type Config struct { - Env string - Port int - Addr string - DbHost string - DbPort uint - DbUser string - DbPassword string - DbName string - DbDialect string - DbMaxConn uint - CleanUp bool - CustomLanguages map[string]string - LanguageColors map[string]string + Env string + Port int + Addr string + DbHost string + DbPort uint + DbUser string + DbPassword string + DbName string + DbDialect string + DbMaxConn uint + CleanUp bool + DefaultUserName string + DefaultUserPassword string + CustomLanguages map[string]string + LanguageColors map[string]string } func (c *Config) IsDev() bool { diff --git a/models/user.go b/models/user.go index 98fa99b..7cb5cdc 100644 --- a/models/user.go +++ b/models/user.go @@ -1,10 +1,5 @@ package models -const ( - DefaultUser string = "admin" - DefaultPassword string = "admin" -) - type User struct { ID string `json:"id" gorm:"primary_key"` ApiKey string `json:"api_key" gorm:"unique"` diff --git a/version.txt b/version.txt index cb174d5..d2d61a7 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.2.1 \ No newline at end of file +1.2.2 \ No newline at end of file