1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: ability to set default username and password

This commit is contained in:
Ferdinand Mütsch 2020-04-26 14:00:53 +02:00
parent 91a4bb2db3
commit 2b47a7d73f
5 changed files with 40 additions and 37 deletions

View File

@ -1,7 +1,9 @@
ENV=prod ENV=prod
WAKAPI_DB_TYPE=mysql # mysql, postgres, sqlite3 WAKAPI_DB_TYPE=mysql # mysql, postgres, sqlite3
WAKAPI_DB_NAME=wakapi_db # file path for sqlite, e.g. /tmp/wakapi.db WAKAPI_DB_NAME=wakapi_db # file path for sqlite, e.g. /tmp/wakapi.db
WAKAPI_DB_USER=myuser WAKAPI_DB_USER=myuser
WAKAPI_DB_PASSWORD=shhh WAKAPI_DB_PASSWORD=shhh
WAKAPI_DB_HOST=localhost WAKAPI_DB_HOST=localhost
WAKAPI_DB_PORT=3306 WAKAPI_DB_PORT=3306
WAKAPI_DEFAULT_USER_NAME=admin
WAKAPI_DEFAULT_USER_PASSWORD=admin # CHANGE!

36
main.go
View File

@ -47,6 +47,8 @@ func readConfig() *models.Config {
dbHost := utils.LookupFatal("WAKAPI_DB_HOST") dbHost := utils.LookupFatal("WAKAPI_DB_HOST")
dbName := utils.LookupFatal("WAKAPI_DB_NAME") dbName := utils.LookupFatal("WAKAPI_DB_NAME")
dbPortStr := utils.LookupFatal("WAKAPI_DB_PORT") 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) dbPort, err := strconv.Atoi(dbPortStr)
cfg, err := ini.Load("config.ini") cfg, err := ini.Load("config.ini")
@ -96,19 +98,21 @@ func readConfig() *models.Config {
} }
return &models.Config{ return &models.Config{
Env: env, Env: env,
Port: port, Port: port,
Addr: addr, Addr: addr,
DbHost: dbHost, DbHost: dbHost,
DbPort: uint(dbPort), DbPort: uint(dbPort),
DbUser: dbUser, DbUser: dbUser,
DbPassword: dbPassword, DbPassword: dbPassword,
DbName: dbName, DbName: dbName,
DbDialect: dbType, DbDialect: dbType,
DbMaxConn: dbMaxConn, DbMaxConn: dbMaxConn,
CleanUp: cleanUp, CleanUp: cleanUp,
CustomLanguages: customLangs, DefaultUserName: defaultUserName,
LanguageColors: colors, 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) { func addDefaultUser(db *gorm.DB, cfg *models.Config) {
pw := md5.Sum([]byte(models.DefaultPassword)) pw := md5.Sum([]byte(cfg.DefaultUserPassword))
pwString := hex.EncodeToString(pw[:]) pwString := hex.EncodeToString(pw[:])
apiKey := uuid.NewV4().String() 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}) result := db.FirstOrCreate(u, &models.User{ID: u.ID})
if result.Error != nil { if result.Error != nil {
log.Println("Unable to create default user.") log.Println("Unable to create default user.")
log.Fatal(result.Error) log.Fatal(result.Error)
} }
if result.RowsAffected > 0 { 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)
} }
} }

View File

@ -1,19 +1,21 @@
package models package models
type Config struct { type Config struct {
Env string Env string
Port int Port int
Addr string Addr string
DbHost string DbHost string
DbPort uint DbPort uint
DbUser string DbUser string
DbPassword string DbPassword string
DbName string DbName string
DbDialect string DbDialect string
DbMaxConn uint DbMaxConn uint
CleanUp bool CleanUp bool
CustomLanguages map[string]string DefaultUserName string
LanguageColors map[string]string DefaultUserPassword string
CustomLanguages map[string]string
LanguageColors map[string]string
} }
func (c *Config) IsDev() bool { func (c *Config) IsDev() bool {

View File

@ -1,10 +1,5 @@
package models package models
const (
DefaultUser string = "admin"
DefaultPassword string = "admin"
)
type User struct { type User struct {
ID string `json:"id" gorm:"primary_key"` ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"` ApiKey string `json:"api_key" gorm:"unique"`

View File

@ -1 +1 @@
1.2.1 1.2.2