add configuration initialization and logic

This commit is contained in:
Adora Laura Kalb 2023-12-15 15:42:29 +01:00
parent e501ea51e4
commit 22cbc94da6
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
6 changed files with 71 additions and 12 deletions

1
.gitignore vendored
View file

@ -93,6 +93,5 @@ fabric.properties
.idea/
testdata/
config/
bin/
*.sqlite

View file

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

View file

@ -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)
}

View file

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

9
internal/config/vars.go Normal file
View file

@ -0,0 +1,9 @@
package config
var SMTPPort int
var SMTPHost string
var SMTPFrom string
var SMTPServer string
var SMTPVerifySSL bool
var SQLitePath string

View file

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