Added file management.
This commit is contained in:
parent
09b8393c8a
commit
3eab2f5562
12 changed files with 201 additions and 21 deletions
|
|
@ -4,11 +4,13 @@ import (
|
|||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/mikestefanello/backlite"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/mikestefanello/backlite"
|
||||
"github.com/spf13/afero"
|
||||
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
"github.com/labstack/echo/v4"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
|
@ -39,6 +41,9 @@ type Container struct {
|
|||
// Database stores the connection to the database
|
||||
Database *sql.DB
|
||||
|
||||
// Files stores the file system.
|
||||
Files afero.Fs
|
||||
|
||||
// ORM stores a client to the ORM
|
||||
ORM *ent.Client
|
||||
|
||||
|
|
@ -63,6 +68,7 @@ func NewContainer() *Container {
|
|||
c.initWeb()
|
||||
c.initCache()
|
||||
c.initDatabase()
|
||||
c.initFiles()
|
||||
c.initORM()
|
||||
c.initAuth()
|
||||
c.initTemplateRenderer()
|
||||
|
|
@ -159,6 +165,21 @@ func (c *Container) initDatabase() {
|
|||
}
|
||||
}
|
||||
|
||||
// initFiles initializes the file system.
|
||||
func (c *Container) initFiles() {
|
||||
// Use in-memory storage for tests.
|
||||
if c.Config.App.Environment == config.EnvTest {
|
||||
c.Files = afero.NewMemMapFs()
|
||||
return
|
||||
}
|
||||
|
||||
fs := afero.NewOsFs()
|
||||
if err := fs.MkdirAll(c.Config.Files.Directory, 0755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Files = afero.NewBasePathFs(fs, c.Config.Files.Directory)
|
||||
}
|
||||
|
||||
// initORM initializes the ORM
|
||||
func (c *Container) initORM() {
|
||||
drv := entsql.OpenDB(c.Config.Database.Driver, c.Database)
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ func TestNewContainer(t *testing.T) {
|
|||
assert.NotNil(t, c.Validator)
|
||||
assert.NotNil(t, c.Cache)
|
||||
assert.NotNil(t, c.Database)
|
||||
assert.NotNil(t, c.Files)
|
||||
assert.NotNil(t, c.ORM)
|
||||
assert.NotNil(t, c.Mail)
|
||||
assert.NotNil(t, c.Auth)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue