feat: complete API-driven projects with infrastructure and business acceptance
This commit is contained in:
49
examples/userService/internal/servicecontext/storage.go
Normal file
49
examples/userService/internal/servicecontext/storage.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package servicecontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"git.yuntuo.hk/k2safe/go-omni/storage/local"
|
||||
|
||||
"example.com/userService/internal/settings"
|
||||
"git.yuntuo.hk/k2safe/go-omni/storage"
|
||||
)
|
||||
|
||||
// InitStorage is called once, before serving requests. The composition root
|
||||
// owns provider clients; business services receive only capability interfaces.
|
||||
func (c *Context) InitStorage(ctx context.Context, cfg settings.StorageConfig) error {
|
||||
if c == nil || ctx == nil {
|
||||
return errors.New("storage initialization requires context")
|
||||
}
|
||||
c.closeMu.Lock()
|
||||
defer c.closeMu.Unlock()
|
||||
if c.closed {
|
||||
return errors.New("service dependencies are closed")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.Files != nil {
|
||||
return errors.New("storage already initialized")
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files, err := local.New(cfg.Root, cfg.MaxBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
signer, err := storage.NewUploadSigner(files, cfg.PublicURL, []byte(cfg.SigningKey), cfg.Policy())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uploader, err := storage.NewUploader(files, cfg.Policy())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.Files, c.Uploads, c.Downloads, c.UploadLinks, c.FileHTTP = files, uploader, signer, signer, signer
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user