2024-04-08 15:47:31 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-04-15 13:01:16 +01:00
|
|
|
"database/sql"
|
2024-04-08 15:47:31 +01:00
|
|
|
"os"
|
2024-04-15 13:01:16 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
|
2024-04-08 15:47:31 +01:00
|
|
|
|
|
|
|
toml "github.com/BurntSushi/toml"
|
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
)
|
|
|
|
|
2024-04-12 20:36:23 +01:00
|
|
|
type WorkerConfig struct {
|
|
|
|
NumberOfWorkers int `toml:"number_of_workers"`
|
|
|
|
Pulling string `toml:"pulling_time"`
|
|
|
|
}
|
|
|
|
|
2024-04-15 13:01:16 +01:00
|
|
|
type ServiceUser struct {
|
|
|
|
User string `toml:"user"`
|
|
|
|
UserId string `toml:"__user__id__"`
|
|
|
|
}
|
|
|
|
|
2024-04-08 15:47:31 +01:00
|
|
|
type Config struct {
|
2024-04-08 17:45:32 +01:00
|
|
|
Hostname string
|
|
|
|
Port int
|
2024-04-12 20:36:23 +01:00
|
|
|
NumberOfWorkers int `toml:"number_of_workers"`
|
|
|
|
SupressCuda int `toml:"supress_cuda"`
|
|
|
|
|
|
|
|
GpuWorker WorkerConfig `toml:"Worker"`
|
2024-04-15 13:01:16 +01:00
|
|
|
|
|
|
|
ServiceUser ServiceUser `toml:"ServiceUser"`
|
2024-04-08 15:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig() Config {
|
|
|
|
|
|
|
|
log.Info("Loading the config file")
|
|
|
|
|
|
|
|
dat, err := os.ReadFile("./config.toml")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to load config file", "err", err)
|
|
|
|
// Use default values
|
|
|
|
return Config{
|
2024-04-08 17:45:32 +01:00
|
|
|
Hostname: "localhost",
|
|
|
|
Port: 8000,
|
|
|
|
NumberOfWorkers: 10,
|
2024-04-12 20:36:23 +01:00
|
|
|
GpuWorker: WorkerConfig{
|
|
|
|
NumberOfWorkers: 1,
|
|
|
|
Pulling: "500ms",
|
|
|
|
},
|
2024-04-15 13:01:16 +01:00
|
|
|
ServiceUser: ServiceUser{
|
|
|
|
User: "Service",
|
|
|
|
UserId: "",
|
|
|
|
},
|
2024-04-08 15:47:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var conf Config
|
|
|
|
_, err = toml.Decode(string(dat), &conf)
|
2024-04-12 20:36:23 +01:00
|
|
|
|
2024-04-15 13:01:16 +01:00
|
|
|
if conf.SupressCuda == 1 {
|
|
|
|
log.Warn("Supressing Cuda Messages!")
|
|
|
|
os.Setenv("TF_CPP_MIN_VLOG_LEVEL", "3")
|
|
|
|
os.Setenv("TF_CPP_MIN_LOG_LEVEL", "3")
|
|
|
|
}
|
2024-04-12 20:36:23 +01:00
|
|
|
|
2024-04-08 15:47:31 +01:00
|
|
|
return conf
|
|
|
|
}
|
2024-04-15 13:01:16 +01:00
|
|
|
|
|
|
|
func (c *Config) GenerateToken(db *sql.DB) {
|
|
|
|
if c.ServiceUser.User == "" {
|
|
|
|
log.Fatal("A user needs to be set in a configuration file")
|
|
|
|
}
|
|
|
|
|
|
|
|
var user struct {
|
|
|
|
Password string `db:"password"`
|
|
|
|
UserId string `db:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := GetDBOnce(db, &user, "users where username=$1;", c.ServiceUser.User)
|
|
|
|
if err == NotFoundError {
|
|
|
|
var newUser = struct {
|
|
|
|
Username string
|
|
|
|
Email string
|
|
|
|
Salt string
|
|
|
|
Password string
|
|
|
|
UserType UserType `db:"user_type"`
|
|
|
|
}{
|
|
|
|
c.ServiceUser.User,
|
|
|
|
c.ServiceUser.User,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
User_Admin,
|
|
|
|
}
|
|
|
|
id, err := InsertReturnId(db, &newUser, "users", "id")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to create user", "err", err)
|
|
|
|
}
|
|
|
|
c.ServiceUser.UserId = id
|
|
|
|
} else if err != nil {
|
|
|
|
log.Fatal("To get user name", "err", err)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if len(strings.ReplaceAll(user.Password, " ", "")) != 0 {
|
|
|
|
log.Fatal("User already exists and is not the service user", "user", user)
|
|
|
|
}
|
|
|
|
c.ServiceUser.UserId = user.UserId
|
|
|
|
}
|
|
|
|
}
|