diff --git a/cmd/cmd.go b/cmd/cmd.go index c0ca150..8e300a6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -36,6 +36,7 @@ var ( awsSecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY") awsDefaultRegion = os.Getenv("AWS_DEFAULT_REGION") awsEndpointURL = os.Getenv("AWS_ENDPOINT_URL") + s3Bucket = os.Getenv("S3_BUCKET") ) func getEnv(key string, d string) string { @@ -58,6 +59,8 @@ func init() { log.Fatal("AWS_DEFAULT_REGION should not be a empty value") case awsEndpointURL == "": log.Fatal("AWS_ENDPOINT_URL should not be a empty value") + case s3Bucket == "": + log.Fatal("S3_BUCKET should not be a empty value") } } @@ -111,8 +114,9 @@ func main() { } app, err := comicverse.New(comicverse.Config{ - DB: db, - S3: storage, + DB: db, + S3: storage, + Bucket: s3Bucket, }, opts...) if err != nil { log.Error("Failed to initiate comicverse app", slog.String("error", err.Error())) diff --git a/comicverse.go b/comicverse.go index f58baf1..689a5b8 100644 --- a/comicverse.go +++ b/comicverse.go @@ -20,8 +20,9 @@ import ( func New(cfg Config, opts ...Option) (http.Handler, error) { app := &app{ - db: cfg.DB, - s3: cfg.S3, + db: cfg.DB, + s3: cfg.S3, + bucket: cfg.Bucket, staticFiles: static.Files(), developmentMode: false, @@ -41,6 +42,9 @@ func New(cfg Config, opts ...Option) (http.Handler, error) { if app.s3 == nil { return nil, errors.New("s3 client must not be nil") } + if app.bucket == "" { + return nil, errors.New("bucket must not be a empty string") + } if app.staticFiles == nil { return nil, errors.New("static files must not be a nil interface") @@ -62,8 +66,9 @@ func New(cfg Config, opts ...Option) (http.Handler, error) { } type Config struct { - DB *sql.DB - S3 *s3.Client + DB *sql.DB + S3 *s3.Client + Bucket string } type Option func(*app) @@ -89,8 +94,9 @@ func WithDevelopmentMode() Option { } type app struct { - db *sql.DB - s3 *s3.Client + db *sql.DB + s3 *s3.Client + bucket string ctx context.Context @@ -106,6 +112,7 @@ type app struct { func (app *app) setup() error { app.assert.NotNil(app.db) app.assert.NotNil(app.s3) + app.assert.NotZero(app.bucket) app.assert.NotNil(app.ctx) app.assert.NotNil(app.staticFiles) app.assert.NotNil(app.logger) @@ -123,8 +130,9 @@ func (app *app) setup() error { } service, err := service.New(service.Config{ - S3: app.s3, DB: database, + S3: app.s3, + Bucket: app.bucket, Context: app.ctx, diff --git a/service/service.go b/service/service.go index 7cb64b4..b1aca8c 100644 --- a/service/service.go +++ b/service/service.go @@ -12,6 +12,8 @@ import ( type Service struct { db *database.Database + s3 *s3.Client + bucket string ctx context.Context @@ -26,6 +28,9 @@ func New(cfg Config) (*Service, error) { if cfg.S3 == nil { return nil, errors.New("s3 client should not be a nil pointer") } + if cfg.Bucket == "" { + return nil, errors.New("bucket should not be a empty string") + } if cfg.Context == nil { return nil, errors.New("context should not be a nil interface") } @@ -35,8 +40,10 @@ func New(cfg Config) (*Service, error) { if cfg.Logger == nil { return nil, errors.New("logger should not be a nil pointer") } - db: cfg.DB, return &Service{ + db: cfg.DB, + s3: cfg.S3, + bucket: cfg.Bucket, ctx: cfg.Context, @@ -46,8 +53,9 @@ func New(cfg Config) (*Service, error) { } type Config struct { - S3 *s3.Client DB *database.Database + S3 *s3.Client + Bucket string Context context.Context