Automatically select s3 backend storage if S3_xxx env are presents
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2022-09-07 11:48:28 +02:00
parent a2903b73a0
commit ecda17fc7d
2 changed files with 14 additions and 7 deletions

View File

@ -31,8 +31,12 @@ var (
func init() { func init() {
existing_backends = append(existing_backends, "s3") existing_backends = append(existing_backends, "s3")
s3_endpoint, _ = os.LookupEnv("S3_ENDPOINT") if endpoint, ok := os.LookupEnv("S3_ENDPOINT"); ok {
backend = "s3"
s3_endpoint = endpoint
}
if region, ok := os.LookupEnv("S3_REGION"); ok { if region, ok := os.LookupEnv("S3_REGION"); ok {
backend = "s3"
s3_region = region s3_region = region
} }
s3_bucket, _ = os.LookupEnv("S3_BUCKET") s3_bucket, _ = os.LookupEnv("S3_BUCKET")

15
main.go
View File

@ -11,7 +11,10 @@ import (
var mux = http.NewServeMux() var mux = http.NewServeMux()
var ThumbsDir string var (
ThumbsDir string
backend = "local"
)
func main() { func main() {
bind := flag.String("bind", ":8080", "Bind port/socket") bind := flag.String("bind", ":8080", "Bind port/socket")
@ -19,7 +22,7 @@ func main() {
publishedImgDir := flag.String("publishedimgdir", "published/", "Directory where save published pictures") publishedImgDir := flag.String("publishedimgdir", "published/", "Directory where save published pictures")
nextImgDir := flag.String("nextimgdir", "next/", "Directory where save pictures to review") nextImgDir := flag.String("nextimgdir", "next/", "Directory where save pictures to review")
baseDir := flag.String("basedir", "images/", "Local base directory where find published and next dirs") baseDir := flag.String("basedir", "images/", "Local base directory where find published and next dirs")
backend := flag.String("storage-backend", "local", fmt.Sprintf("Storage backend to use (between: %s)", existing_backends)) flag.StringVar(&backend, "storage-backend", backend, fmt.Sprintf("Storage backend to use (between: %s)", existing_backends))
flag.StringVar(&ThumbsDir, "thumbsdir", "./images/thumbs/", "Directory where generate thumbs") flag.StringVar(&ThumbsDir, "thumbsdir", "./images/thumbs/", "Directory where generate thumbs")
flag.Parse() flag.Parse()
@ -56,7 +59,7 @@ func main() {
}) })
var storage_backend FileBackend var storage_backend FileBackend
if backend == nil || *backend == "local" { if backend == "local" {
storage_backend = &LocalFileBackend{*baseDir} storage_backend = &LocalFileBackend{*baseDir}
if _, err := os.Stat(path.Join(*baseDir, *publishedImgDir)); os.IsNotExist(err) { if _, err := os.Stat(path.Join(*baseDir, *publishedImgDir)); os.IsNotExist(err) {
if err := os.MkdirAll(path.Join(*baseDir, *publishedImgDir), 0755); err != nil { if err := os.MkdirAll(path.Join(*baseDir, *publishedImgDir), 0755); err != nil {
@ -68,12 +71,12 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
} }
} else if *backend == "s3" { } else if backend == "s3" {
storage_backend = &S3FileBackend{s3_endpoint, s3_region, s3_bucket, s3_access_key, s3_secret_key, s3_path_style, *baseDir} storage_backend = &S3FileBackend{s3_endpoint, s3_region, s3_bucket, s3_access_key, s3_secret_key, s3_path_style, *baseDir}
} else { } else {
log.Fatalf("%q is not a valid storage backend.", *backend) log.Fatalf("%q is not a valid storage backend.", backend)
} }
log.Printf("Using %s storage backend", *backend) log.Printf("Using %s storage backend", backend)
pe := &PictureExplorer{ pe := &PictureExplorer{
FileBackend: storage_backend, FileBackend: storage_backend,