update env variables to set data storage

This commit is contained in:
Hayden 2022-09-14 19:04:11 -08:00
parent 3b893e2242
commit fa585db4db
8 changed files with 26 additions and 80 deletions

View file

@ -37,10 +37,6 @@ func main() {
path = os.Args[1]
}
if path == "" {
log.Warn().Msg("No configuration detected, using defaults")
}
cfg, err := config.NewConfig(path)
if err != nil {
panic(err)
@ -55,17 +51,22 @@ func main() {
func run(cfg *config.Config) error {
app := new(cfg)
app.setupLogger()
// =========================================================================
// Initialize Database & Repos
c, err := ent.Open(cfg.Database.GetDriver(), cfg.Database.GetUrl())
err := os.MkdirAll(cfg.Storage.Data, 0755)
if err != nil {
log.Fatal().Err(err).Msg("failed to create data directory")
}
c, err := ent.Open("sqlite3", cfg.Storage.SqliteUrl)
if err != nil {
log.Fatal().
Err(err).
Str("driver", cfg.Database.GetDriver()).
Str("url", cfg.Database.GetUrl()).
Str("driver", "sqlite").
Str("url", cfg.Storage.SqliteUrl).
Msg("failed opening connection to sqlite")
}
defer func(c *ent.Client) {
@ -74,12 +75,14 @@ func run(cfg *config.Config) error {
if err := c.Schema.Create(context.Background()); err != nil {
log.Fatal().
Err(err).
Str("driver", "sqlite").
Str("url", cfg.Storage.SqliteUrl).
Msg("failed creating schema resources")
}
app.db = c
app.repos = repo.EntAllRepos(c)
app.services = services.NewServices(app.repos)
app.services = services.NewServices(app.repos, cfg.Storage.Data)
// =========================================================================
// Start Server

View file

@ -1,19 +0,0 @@
---
mode: development
swagger:
host: localhost:7745
scheme: http
web:
port: 7745
host: ""
database:
driver: sqlite3
sqlite-url: ./homebox.db?_fk=1
logger:
level: debug
mailer:
host: smtp.example.com
port: 465
username:
password:
from: example@email.com

View file

@ -17,12 +17,12 @@ const (
)
type Config struct {
Mode string `yaml:"mode" conf:"default:development"` // development or production
Web WebConfig `yaml:"web"`
Database Database `yaml:"database"`
Log LoggerConf `yaml:"logger"`
Mailer MailerConf `yaml:"mailer"`
Swagger SwaggerConf `yaml:"swagger"`
Mode string `yaml:"mode" conf:"default:development"` // development or production
Web WebConfig `yaml:"web"`
Storage Storage `yaml:"storage"`
Log LoggerConf `yaml:"logger"`
Mailer MailerConf `yaml:"mailer"`
Swagger SwaggerConf `yaml:"swagger"`
}
type SwaggerConf struct {

View file

@ -4,20 +4,8 @@ const (
DriverSqlite3 = "sqlite3"
)
type Database struct {
Driver string `yaml:"driver" conf:"default:sqlite3"`
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
}
func (d *Database) GetDriver() string {
return d.Driver
}
func (d *Database) GetUrl() string {
switch d.Driver {
case DriverSqlite3:
return d.SqliteUrl
default:
panic("unknown database driver")
}
type Storage struct {
// Data is the path to the root directory
Data string `yaml:"data" conf:"default:./homebox-data"`
SqliteUrl string `yaml:"sqlite-url" conf:"default:./homebox-data/homebox.db?_fk=1"`
}

View file

@ -1,26 +0,0 @@
package config
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_DatabaseConfig_Sqlite(t *testing.T) {
dbConf := &Database{
Driver: DriverSqlite3,
SqliteUrl: "file:ent?mode=memory&cache=shared&_fk=1",
}
assert.Equal(t, "sqlite3", dbConf.GetDriver())
assert.Equal(t, "file:ent?mode=memory&cache=shared&_fk=1", dbConf.GetUrl())
}
func Test_DatabaseConfig_Unknown(t *testing.T) {
dbConf := &Database{
Driver: "null",
}
assert.Panics(t, func() { dbConf.GetUrl() })
}

View file

@ -6,5 +6,5 @@ import (
)
func GetMockServices(repos *repo.AllRepos) *services.AllServices {
return services.NewServices(repos)
return services.NewServices(repos, "/tmp/homebox")
}

View file

@ -10,7 +10,7 @@ type AllServices struct {
Items *ItemService
}
func NewServices(repos *repo.AllRepos) *AllServices {
func NewServices(repos *repo.AllRepos, root string) *AllServices {
return &AllServices{
User: &UserService{repos},
Admin: &AdminService{repos},
@ -18,7 +18,7 @@ func NewServices(repos *repo.AllRepos) *AllServices {
Labels: &LabelService{repos},
Items: &ItemService{
repo: repos,
filepath: "/tmp/content",
filepath: root,
},
}
}

View file

@ -63,7 +63,7 @@ func TestMain(m *testing.M) {
tClient = client
tRepos = repo.EntAllRepos(tClient)
tSvc = NewServices(tRepos)
tSvc = NewServices(tRepos, "/tmp/homebox")
defer client.Close()
bootstrap()