Handle option to switch between local and s3
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2022-09-04 23:32:03 +02:00
parent f1d98b605f
commit f008669bd8
2 changed files with 42 additions and 1 deletions

View File

@ -2,12 +2,14 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
"io" "io"
"log" "log"
"net/http" "net/http"
"os"
"path" "path"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -17,8 +19,35 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/aws/aws-sdk-go/service/s3/s3manager"
) )
var (
s3_endpoint string
s3_region = "us"
s3_bucket string
s3_access_key string
s3_secret_key string
s3_path_style bool
)
func init() { func init() {
existing_backends = append(existing_backends, "s3") existing_backends = append(existing_backends, "s3")
s3_endpoint, _ = os.LookupEnv("S3_ENDPOINT")
if region, ok := os.LookupEnv("S3_REGION"); ok {
s3_region = region
}
s3_bucket, _ = os.LookupEnv("S3_BUCKET")
s3_access_key, _ = os.LookupEnv("S3_ACCESS_KEY")
s3_secret_key, _ = os.LookupEnv("S3_SECRET_KEY")
if path_style, ok := os.LookupEnv("S3_PATH_STYLE"); ok {
s3_path_style = path_style == "1" || path_style == "ON" || path_style == "on" || path_style == "TRUE" || path_style == "true" || path_style == "yes" || path_style == "YES"
}
flag.StringVar(&s3_endpoint, "s3-endpoint", s3_endpoint, "When using S3 backend, endpoint to use")
flag.StringVar(&s3_region, "s3-region", s3_region, "When using S3 backend, region to use")
flag.StringVar(&s3_bucket, "s3-bucket", s3_bucket, "When using S3 backend, bucket to use")
flag.StringVar(&s3_access_key, "s3-access-key", s3_access_key, "When using S3 backend, Access Key")
flag.StringVar(&s3_secret_key, "s3-secret-key", s3_secret_key, "When using S3 backend, Secret Key")
flag.BoolVar(&s3_path_style, "s3-path-style", s3_path_style, "When using S3 backend, force path style (when using minio)")
} }
type S3FileBackend struct { type S3FileBackend struct {

14
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -16,6 +17,8 @@ func main() {
htpasswd_file := flag.String("htpasswd", "", "Admin passwords file, Apache htpasswd format") htpasswd_file := flag.String("htpasswd", "", "Admin passwords file, Apache htpasswd format")
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")
backend := flag.String("storage-backend", "local", 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()
@ -51,8 +54,17 @@ func main() {
http.FileServer(Assets).ServeHTTP(w, r) http.FileServer(Assets).ServeHTTP(w, r)
}) })
var storage_backend FileBackend
if backend == nil || *backend == "local" {
storage_backend = &LocalFileBackend{*baseDir}
} else if *backend == "s3" {
storage_backend = &S3FileBackend{s3_endpoint, s3_region, s3_bucket, s3_access_key, s3_secret_key, s3_path_style, *baseDir}
} else {
log.Fatalf("%q is not a valid storage backend.", *backend)
}
pe := &PictureExplorer{ pe := &PictureExplorer{
FileBackend: &LocalFileBackend{"./images/"}, FileBackend: storage_backend,
PublishedImgDir: *publishedImgDir, PublishedImgDir: *publishedImgDir,
NextImgDir: *nextImgDir, NextImgDir: *nextImgDir,
} }