mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 09:10:26 +00:00
update env variables to set data storage
This commit is contained in:
parent
3b893e2242
commit
fa585db4db
8 changed files with 26 additions and 80 deletions
|
@ -37,10 +37,6 @@ func main() {
|
||||||
path = os.Args[1]
|
path = os.Args[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
if path == "" {
|
|
||||||
log.Warn().Msg("No configuration detected, using defaults")
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.NewConfig(path)
|
cfg, err := config.NewConfig(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -55,17 +51,22 @@ func main() {
|
||||||
|
|
||||||
func run(cfg *config.Config) error {
|
func run(cfg *config.Config) error {
|
||||||
app := new(cfg)
|
app := new(cfg)
|
||||||
|
|
||||||
app.setupLogger()
|
app.setupLogger()
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Initialize Database & Repos
|
// 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 {
|
if err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Err(err).
|
Err(err).
|
||||||
Str("driver", cfg.Database.GetDriver()).
|
Str("driver", "sqlite").
|
||||||
Str("url", cfg.Database.GetUrl()).
|
Str("url", cfg.Storage.SqliteUrl).
|
||||||
Msg("failed opening connection to sqlite")
|
Msg("failed opening connection to sqlite")
|
||||||
}
|
}
|
||||||
defer func(c *ent.Client) {
|
defer func(c *ent.Client) {
|
||||||
|
@ -74,12 +75,14 @@ func run(cfg *config.Config) error {
|
||||||
if err := c.Schema.Create(context.Background()); err != nil {
|
if err := c.Schema.Create(context.Background()); err != nil {
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Err(err).
|
Err(err).
|
||||||
|
Str("driver", "sqlite").
|
||||||
|
Str("url", cfg.Storage.SqliteUrl).
|
||||||
Msg("failed creating schema resources")
|
Msg("failed creating schema resources")
|
||||||
}
|
}
|
||||||
|
|
||||||
app.db = c
|
app.db = c
|
||||||
app.repos = repo.EntAllRepos(c)
|
app.repos = repo.EntAllRepos(c)
|
||||||
app.services = services.NewServices(app.repos)
|
app.services = services.NewServices(app.repos, cfg.Storage.Data)
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
// Start Server
|
// Start Server
|
||||||
|
|
|
@ -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
|
|
|
@ -17,12 +17,12 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Mode string `yaml:"mode" conf:"default:development"` // development or production
|
Mode string `yaml:"mode" conf:"default:development"` // development or production
|
||||||
Web WebConfig `yaml:"web"`
|
Web WebConfig `yaml:"web"`
|
||||||
Database Database `yaml:"database"`
|
Storage Storage `yaml:"storage"`
|
||||||
Log LoggerConf `yaml:"logger"`
|
Log LoggerConf `yaml:"logger"`
|
||||||
Mailer MailerConf `yaml:"mailer"`
|
Mailer MailerConf `yaml:"mailer"`
|
||||||
Swagger SwaggerConf `yaml:"swagger"`
|
Swagger SwaggerConf `yaml:"swagger"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SwaggerConf struct {
|
type SwaggerConf struct {
|
||||||
|
|
|
@ -4,20 +4,8 @@ const (
|
||||||
DriverSqlite3 = "sqlite3"
|
DriverSqlite3 = "sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Storage struct {
|
||||||
Driver string `yaml:"driver" conf:"default:sqlite3"`
|
// Data is the path to the root directory
|
||||||
SqliteUrl string `yaml:"sqlite-url" conf:"default:file:ent?mode=memory&cache=shared&_fk=1"`
|
Data string `yaml:"data" conf:"default:./homebox-data"`
|
||||||
}
|
SqliteUrl string `yaml:"sqlite-url" conf:"default:./homebox-data/homebox.db?_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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() })
|
|
||||||
|
|
||||||
}
|
|
|
@ -6,5 +6,5 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMockServices(repos *repo.AllRepos) *services.AllServices {
|
func GetMockServices(repos *repo.AllRepos) *services.AllServices {
|
||||||
return services.NewServices(repos)
|
return services.NewServices(repos, "/tmp/homebox")
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ type AllServices struct {
|
||||||
Items *ItemService
|
Items *ItemService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServices(repos *repo.AllRepos) *AllServices {
|
func NewServices(repos *repo.AllRepos, root string) *AllServices {
|
||||||
return &AllServices{
|
return &AllServices{
|
||||||
User: &UserService{repos},
|
User: &UserService{repos},
|
||||||
Admin: &AdminService{repos},
|
Admin: &AdminService{repos},
|
||||||
|
@ -18,7 +18,7 @@ func NewServices(repos *repo.AllRepos) *AllServices {
|
||||||
Labels: &LabelService{repos},
|
Labels: &LabelService{repos},
|
||||||
Items: &ItemService{
|
Items: &ItemService{
|
||||||
repo: repos,
|
repo: repos,
|
||||||
filepath: "/tmp/content",
|
filepath: root,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ func TestMain(m *testing.M) {
|
||||||
|
|
||||||
tClient = client
|
tClient = client
|
||||||
tRepos = repo.EntAllRepos(tClient)
|
tRepos = repo.EntAllRepos(tClient)
|
||||||
tSvc = NewServices(tRepos)
|
tSvc = NewServices(tRepos, "/tmp/homebox")
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
bootstrap()
|
bootstrap()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue