update
This commit is contained in:
111
internal/config/config.go
Normal file
111
internal/config/config.go
Normal file
@ -0,0 +1,111 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.rouggy.com/rouggy/RaceBot/internal/domain"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var configTemplate = `# config.toml
|
||||
host = "127.0.0.1"
|
||||
port = "3000"
|
||||
# TMDbApiKey is required
|
||||
tmdbApiKey = ""
|
||||
dbName = "racer.db"
|
||||
uploadFolder = "/home/rouggy/torrents/rtorrent/Race"
|
||||
`
|
||||
|
||||
func writeConfig(configPath string, configFile string) error {
|
||||
cfgPath := filepath.Join(configPath, configFile)
|
||||
|
||||
// check if configPath exists, if not create it
|
||||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.MkdirAll(configPath, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// check if config exists, if not create it
|
||||
if _, err := os.Stat(cfgPath); errors.Is(err, os.ErrNotExist) {
|
||||
|
||||
f, err := os.Create(cfgPath)
|
||||
if err != nil { // perm 0666
|
||||
// handle failed create
|
||||
log.Printf("error creating file: %q", err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// setup text template to inject variables into
|
||||
_, err = template.New("config").Parse(configTemplate)
|
||||
if err != nil {
|
||||
log.Println("Could not create config file:", err)
|
||||
}
|
||||
|
||||
return f.Sync()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
Config *domain.Config
|
||||
}
|
||||
|
||||
func New(configPath string) *AppConfig {
|
||||
c := &AppConfig{}
|
||||
c.defaults()
|
||||
c.Config.ConfigPath = configPath
|
||||
|
||||
c.load(configPath)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *AppConfig) defaults() {
|
||||
c.Config = &domain.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: "3000",
|
||||
TMDbApiKey: "",
|
||||
DbName: "racer.db",
|
||||
UploadFolder: "/home/rouggy/torrents/rtorrent/Race",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *AppConfig) load(configPath string) {
|
||||
|
||||
viper.SetConfigType("toml")
|
||||
|
||||
// clean trailing slash from configPath
|
||||
configPath = path.Clean(configPath)
|
||||
|
||||
if configPath != "" {
|
||||
if err := writeConfig(configPath, "config.toml"); err != nil {
|
||||
log.Printf("write error: %q", err)
|
||||
}
|
||||
|
||||
viper.SetConfigFile(path.Join(configPath, "config.toml"))
|
||||
} else {
|
||||
viper.SetConfigName("config")
|
||||
|
||||
// Search config in directories
|
||||
viper.AddConfigPath("./config")
|
||||
}
|
||||
|
||||
// read config
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Printf("config read error: %q", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&c.Config); err != nil {
|
||||
log.Fatalf("Could not unmarshal config file: %v", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
22
internal/database/database.go
Normal file
22
internal/database/database.go
Normal file
@ -0,0 +1,22 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"git.rouggy.com/rouggy/RaceBot/internal/domain"
|
||||
"github.com/jinzhu/gorm"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
var err error
|
||||
|
||||
type DB struct {
|
||||
handler *gorm.DB
|
||||
}
|
||||
|
||||
func NewDB(cfg domain.Config) {
|
||||
|
||||
}
|
||||
|
||||
// GetDB : get current connection
|
||||
func GetDB() *gorm.DB {
|
||||
return db
|
||||
}
|
21
internal/database/sqlite.go
Normal file
21
internal/database/sqlite.go
Normal file
@ -0,0 +1,21 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.rouggy.com/rouggy/RaceBot/internal/domain"
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// SQLiteDBConnect : Create Connection to database
|
||||
func SQLiteDBConnect(cfg *domain.Config) {
|
||||
//Connect to database, exit when errored
|
||||
db, err = gorm.Open("sqlite3", "./"+cfg.DbName+".db")
|
||||
if err != nil {
|
||||
panic("[Database] Failed to connect to database")
|
||||
}
|
||||
fmt.Println("[Database] Database successfully connected")
|
||||
//If set true then print all executed queries to the console
|
||||
db.LogMode(true)
|
||||
|
||||
}
|
10
internal/domain/config.go
Normal file
10
internal/domain/config.go
Normal file
@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
type Config struct {
|
||||
ConfigPath string
|
||||
Host string `toml:"host"`
|
||||
Port string `toml:"port"`
|
||||
DbName string `toml:"dbName"`
|
||||
UploadFolder string `toml:"uploadFolder"`
|
||||
TMDbApiKey string `toml:"tmdbApiKey"`
|
||||
}
|
14
internal/race/race.go
Normal file
14
internal/race/race.go
Normal file
@ -0,0 +1,14 @@
|
||||
package race
|
||||
|
||||
import "github.com/jinzhu/gorm"
|
||||
|
||||
type Race struct {
|
||||
gorm.Model
|
||||
Hash string `gorm:"column:hash;type:varchar(50)" form:"hash" json:"hash" binding:"max=50"`
|
||||
TorrentName string `gorm:"column:torrent_name;varchar(100);not null" form:"torrent_name" json:"torrent_name" binding:"required,max=100"`
|
||||
Category string `gorm:"column:category;varchar(50);not null" form:"category" json:"category" binding:"max=50"`
|
||||
ContentPath string `gorm:"column:content_path;varchar(100);not null" form:"content_path" json:"content_path" binding:"max=100"`
|
||||
RootPath string `gorm:"column:root_path;varchar(100)" form:"root_path" json:"root_path" binding:"max=100"`
|
||||
Size string `gorm:"column:size;varchar(15)" form:"size" json:"size" binding:"max=15"`
|
||||
Won bool `gorm:"column:won;bool" form:"won" json:"won"`
|
||||
}
|
Reference in New Issue
Block a user