Added storage
This commit is contained in:
@@ -15,5 +15,6 @@ func Start(logger *zap.Logger) error {
|
||||
|
||||
return curre.NewManager().
|
||||
Register(NewGinHttpServer(deps)).
|
||||
Register(NewStorageServer(logger.With(zap.String("app", "storageServer")), deps)).
|
||||
Run(ctx)
|
||||
}
|
||||
|
26
internal/server/storage_server.go
Normal file
26
internal/server/storage_server.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.front.kjuulh.io/kjuulh/curre"
|
||||
"git.front.kjuulh.io/kjuulh/kraken/internal/serverdeps"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func NewStorageServer(logger *zap.Logger, deps *serverdeps.ServerDeps) curre.Component {
|
||||
storage := deps.GetStorageService()
|
||||
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
|
||||
InitFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
||||
logger.Debug("Initializing storage")
|
||||
return storage.InitializeStorage(ctx)
|
||||
},
|
||||
StartFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
||||
return nil
|
||||
},
|
||||
StopFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
||||
logger.Debug("Cleaning up storage")
|
||||
return storage.CleanupStorage(ctx)
|
||||
},
|
||||
})
|
||||
}
|
@@ -1,13 +1,29 @@
|
||||
package serverdeps
|
||||
|
||||
import "go.uber.org/zap"
|
||||
import (
|
||||
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ServerDeps struct {
|
||||
logger *zap.Logger
|
||||
logger *zap.Logger
|
||||
storageConfig *storage.StorageConfig
|
||||
}
|
||||
|
||||
func NewServerDeps(logger *zap.Logger) *ServerDeps {
|
||||
return &ServerDeps{
|
||||
deps := &ServerDeps{
|
||||
logger: logger.With(zap.String("app", "serverdeps")),
|
||||
}
|
||||
|
||||
if storageCfg, err := storage.NewDefaultStorageConfig(); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
deps.storageConfig = storageCfg
|
||||
}
|
||||
|
||||
return deps
|
||||
}
|
||||
|
||||
func (deps *ServerDeps) GetStorageService() *storage.Service {
|
||||
return storage.NewService(deps.storageConfig)
|
||||
}
|
||||
|
2
internal/services/actions/action.go
Normal file
2
internal/services/actions/action.go
Normal file
@@ -0,0 +1,2 @@
|
||||
package action
|
||||
|
7
internal/services/storage/models.go
Normal file
7
internal/services/storage/models.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package storage
|
||||
|
||||
type (
|
||||
Area struct {
|
||||
Path string
|
||||
}
|
||||
)
|
60
internal/services/storage/storage.go
Normal file
60
internal/services/storage/storage.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// The idea behind storage is that we have file dir, with a git repo.
|
||||
// This file repo can now take certain actions
|
||||
|
||||
type StorageConfig struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func NewDefaultStorageConfig() (*StorageConfig, error) {
|
||||
tempDir, err := os.MkdirTemp(os.TempDir(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &StorageConfig{
|
||||
Path: path.Join(tempDir, "kraken"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
cfg *StorageConfig
|
||||
}
|
||||
|
||||
func NewService(cfg *StorageConfig) *Service {
|
||||
return &Service{cfg: cfg}
|
||||
}
|
||||
|
||||
func (s *Service) getStoragePath(ctx context.Context) string {
|
||||
return path.Join(s.cfg.Path, "storage")
|
||||
}
|
||||
|
||||
func (s *Service) InitializeStorage(ctx context.Context) error {
|
||||
return os.MkdirAll(s.getStoragePath(ctx), 0755)
|
||||
}
|
||||
|
||||
func (s *Service) CleanupStorage(ctx context.Context) error {
|
||||
return os.RemoveAll(s.getStoragePath(ctx))
|
||||
}
|
||||
|
||||
func (s *Service) CreateArea(ctx context.Context) (*Area, error) {
|
||||
dir, err := os.MkdirTemp(s.getStoragePath(ctx), "*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Area{
|
||||
Path: dir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) RemoveArea(ctx context.Context, area *Area) error {
|
||||
return os.RemoveAll(area.Path)
|
||||
}
|
Reference in New Issue
Block a user