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/ .idea/
testdata/ testdata/
config/
bin/ bin/
*.sqlite *.sqlite

View file

@ -2,18 +2,16 @@ package app
import ( import (
"code.lila.network/lauralani/tlm-login-server/internal/app" "code.lila.network/lauralani/tlm-login-server/internal/app"
"code.lila.network/lauralani/tlm-login-server/internal/config"
"fmt" "fmt"
) )
func Run() error { func Run() error {
//err := db.InitializeDB() varserr := config.InitializeConfig()
//if err != nil { if varserr != nil {
// log.Fatalln(err) return varserr
//} }
//go app.CleanupLogins()
//go app.CleanupLoginsCronJob()
dberr := app.InitializeDB() dberr := app.InitializeDB()
if dberr != nil { if dberr != nil {

View file

@ -1,6 +1,7 @@
package app package app
import ( import (
"code.lila.network/lauralani/tlm-login-server/internal/config"
"code.lila.network/lauralani/tlm-login-server/internal/models" "code.lila.network/lauralani/tlm-login-server/internal/models"
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
//"gorm.io/driver/sqlite" with CGo //"gorm.io/driver/sqlite" with CGo
@ -13,12 +14,11 @@ func InitializeDB() error {
var err error var err error
var db *gorm.DB 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 { if err != nil {
slog.Error("Can't open DB") slog.Error("Can't open DB", "path", config.SQLitePath)
os.Exit(1) 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 gorm.Model
Username string Username string
Email string Email string
PasswordHash []byte `gorm:"size:255"` PasswordHash []byte `gorm:"size:60"`
LastLoginAt time.Time LastLoginAt time.Time
} }