First checkpoint
This commit is contained in:
34
app/config.go
Normal file
34
app/config.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/caarlos0/env/v11"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int `env:"PORT" envDefault:"8080"`
|
||||
DBURL string `env:"DB_URL"`
|
||||
|
||||
S3Endpoint string `env:"S3_ENDPOINT"`
|
||||
S3AccessKey string `env:"S3_ACCESS_KEY"`
|
||||
S3SecretKey string `env:"S3_SECRET_KEY"`
|
||||
S3Bucket string `env:"S3_BUCKET" envDefault:"geofiles"`
|
||||
S3UseSSL bool `env:"S3_USE_SSL" envDefault:"false"`
|
||||
}
|
||||
|
||||
func loadConfig() (*Config, error) {
|
||||
|
||||
if err := godotenv.Load(); err != nil {
|
||||
log.Println("No .env file found, relying on system env")
|
||||
}
|
||||
|
||||
cfg := &Config{}
|
||||
|
||||
if err := env.Parse(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
29
app/database.go
Normal file
29
app/database.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func newDB(ctx context.Context, cfg *Config) (*Store, error) {
|
||||
pool, err := pgxpool.New(ctx, cfg.DBURL)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Store{pool: pool}, nil
|
||||
}
|
||||
|
||||
func (s *Store) closeDB() {
|
||||
s.pool.Close()
|
||||
}
|
||||
44
app/init.go
Normal file
44
app/init.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/minio/minio-go/v7"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Ctx context.Context
|
||||
Cfg *Config
|
||||
Db *pgxpool.Pool
|
||||
S3 *minio.Client
|
||||
Validator *validator.Validate
|
||||
}
|
||||
|
||||
func NewApp(ctx context.Context) *App {
|
||||
cfg, err := loadConfig()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
db, err := newDB(ctx, cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
s3, err := newS3Client(ctx, cfg)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return &App{
|
||||
Ctx: ctx,
|
||||
Cfg: cfg,
|
||||
Db: db.pool,
|
||||
S3: s3,
|
||||
Validator: validator.New(validator.WithRequiredStructEnabled()),
|
||||
}
|
||||
}
|
||||
31
app/storage.go
Normal file
31
app/storage.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
func newS3Client(ctx context.Context, cfg *Config) (*minio.Client, error) {
|
||||
client, err := minio.New(cfg.S3Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.S3AccessKey, cfg.S3SecretKey, ""),
|
||||
Secure: cfg.S3UseSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 client: %w", err)
|
||||
}
|
||||
|
||||
exists, err := client.BucketExists(ctx, cfg.S3Bucket)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 bucket check: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
if err := client.MakeBucket(ctx, cfg.S3Bucket, minio.MakeBucketOptions{}); err != nil {
|
||||
return nil, fmt.Errorf("s3 make bucket: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
Reference in New Issue
Block a user