diff --git a/.gitignore b/.gitignore index c94fcf9..8f2c791 100644 --- a/.gitignore +++ b/.gitignore @@ -93,6 +93,5 @@ fabric.properties .idea/ testdata/ -config/ bin/ *.sqlite \ No newline at end of file diff --git a/cmd/tlm-login-server/tlm-login-server.go b/cmd/tlm-login-server/tlm-login-server.go index aa5b539..06929f3 100644 --- a/cmd/tlm-login-server/tlm-login-server.go +++ b/cmd/tlm-login-server/tlm-login-server.go @@ -2,18 +2,16 @@ package app import ( "code.lila.network/lauralani/tlm-login-server/internal/app" + "code.lila.network/lauralani/tlm-login-server/internal/config" "fmt" ) func Run() error { - //err := db.InitializeDB() - //if err != nil { - // log.Fatalln(err) - //} - - //go app.CleanupLogins() - //go app.CleanupLoginsCronJob() + varserr := config.InitializeConfig() + if varserr != nil { + return varserr + } dberr := app.InitializeDB() if dberr != nil { diff --git a/internal/app/db.go b/internal/app/db.go index 46df3e0..89cd8af 100644 --- a/internal/app/db.go +++ b/internal/app/db.go @@ -1,6 +1,7 @@ package app import ( + "code.lila.network/lauralani/tlm-login-server/internal/config" "code.lila.network/lauralani/tlm-login-server/internal/models" "github.com/glebarez/sqlite" //"gorm.io/driver/sqlite" with CGo @@ -13,12 +14,11 @@ func InitializeDB() error { var err error var db *gorm.DB - var dbpath = os.Getenv("TLM_SQLITE_PATH") - db, err = gorm.Open(sqlite.Open(dbpath), &gorm.Config{}) + db, err = gorm.Open(sqlite.Open(config.SQLitePath), &gorm.Config{}) if err != nil { - slog.Error("Can't open DB") + slog.Error("Can't open DB", "path", config.SQLitePath) os.Exit(1) } diff --git a/internal/config/initialize.go b/internal/config/initialize.go new file mode 100644 index 0000000..bbdda63 --- /dev/null +++ b/internal/config/initialize.go @@ -0,0 +1,53 @@ +package config + +import ( + "errors" + "fmt" + "os" + "strconv" +) + +func InitializeConfig() error { + if os.Getenv("TLM_SQLITE_PATH") != "" { + SQLitePath = os.Getenv("TLM_SQLITE_PATH") + } else { + return errors.New("required env var is empty: TLM_SQLITE_PATH") + } + + if os.Getenv("TLM_SMTP_FROM") != "" { + SMTPFrom = os.Getenv("TLM_SMTP_FROM") + } else { + return errors.New("required env var is empty: TLM_SMTP_FROM") + } + + if os.Getenv("TLM_SMTP_SERVER") != "" { + SMTPServer = os.Getenv("TLM_SMTP_SERVER") + } else { + return errors.New("required env var is empty: TLM_SMTP_SERVER") + } + + if os.Getenv("TLM_SMTP_VERIFYSSL") != "" { + var err error + SMTPVerifySSL, err = strconv.ParseBool(os.Getenv("TLM_SMTP_VERIFYSSL")) + if err != nil { + return errors.New(fmt.Sprintf("Invalid value for env var TLM_SMTP_VERIFYSSL: %v", + os.Getenv("TLM_SMTP_VERIFYSSL"))) + } + + } else { + SMTPVerifySSL = true + } + + if os.Getenv("TLM_SMTP_PORT") != "" { + var err error + SMTPPort, err = strconv.Atoi(os.Getenv("TLM_SMTP_PORT")) + if err != nil { + return errors.New(fmt.Sprintf("Invalid value for env var TLM_SMTP_PORT: %v", + os.Getenv("TLM_SMTP_PORT"))) + } + } else { + SMTPPort = 465 + } + + return nil +} diff --git a/internal/config/vars.go b/internal/config/vars.go new file mode 100644 index 0000000..d4d263e --- /dev/null +++ b/internal/config/vars.go @@ -0,0 +1,9 @@ +package config + +var SMTPPort int +var SMTPHost string +var SMTPFrom string +var SMTPServer string +var SMTPVerifySSL bool + +var SQLitePath string diff --git a/internal/models/database.go b/internal/models/database.go index 213a34e..1a58f54 100644 --- a/internal/models/database.go +++ b/internal/models/database.go @@ -11,6 +11,6 @@ type User struct { gorm.Model Username string Email string - PasswordHash []byte `gorm:"size:255"` + PasswordHash []byte `gorm:"size:60"` LastLoginAt time.Time }