2017-05-08 23:07:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-16 14:55:19 +02:00
|
|
|
"crypto/tls"
|
2021-04-21 11:37:38 +02:00
|
|
|
"embed"
|
2019-09-05 00:44:02 +02:00
|
|
|
"fmt"
|
2021-04-21 11:37:38 +02:00
|
|
|
"io/fs"
|
2017-05-08 23:07:07 +02:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2021-12-30 12:09:42 +01:00
|
|
|
"github.com/cloudlena/adapters/logging"
|
2022-03-24 08:46:21 +01:00
|
|
|
"github.com/cloudlena/s3manager/internal/app/s3manager"
|
2022-03-23 10:10:30 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-08-05 11:44:40 +02:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2022-03-23 10:10:30 +01:00
|
|
|
"github.com/spf13/viper"
|
2017-05-08 23:07:07 +02:00
|
|
|
)
|
|
|
|
|
2021-04-21 11:37:38 +02:00
|
|
|
//go:embed web/template
|
|
|
|
var templateFS embed.FS
|
|
|
|
|
2022-05-01 16:20:41 +02:00
|
|
|
//go:embed web/static
|
2022-04-30 12:33:46 +02:00
|
|
|
var staticFS embed.FS
|
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
func main() {
|
2022-03-24 08:46:21 +01:00
|
|
|
var accessKeyID, secretAccessKey, iamEndpoint string
|
2022-03-23 10:46:09 +01:00
|
|
|
|
2022-03-23 10:10:30 +01:00
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
viper.SetDefault("ENDPOINT", "s3.amazonaws.com")
|
|
|
|
endpoint := viper.GetString("ENDPOINT")
|
|
|
|
|
2022-03-23 10:46:09 +01:00
|
|
|
useIam := viper.GetBool("USE_IAM")
|
|
|
|
|
|
|
|
if useIam {
|
|
|
|
iamEndpoint = viper.GetString("IAM_ENDPOINT")
|
|
|
|
} else {
|
|
|
|
accessKeyID = viper.GetString("ACCESS_KEY_ID")
|
|
|
|
if len(accessKeyID) == 0 {
|
|
|
|
log.Fatal("please provide ACCESS_KEY_ID")
|
|
|
|
}
|
2022-03-23 10:10:30 +01:00
|
|
|
|
2022-03-23 10:46:09 +01:00
|
|
|
secretAccessKey = viper.GetString("SECRET_ACCESS_KEY")
|
|
|
|
if len(secretAccessKey) == 0 {
|
|
|
|
log.Fatal("please provide SECRET_ACCESS_KEY")
|
|
|
|
}
|
2018-11-30 23:09:00 +01:00
|
|
|
}
|
2022-03-23 10:10:30 +01:00
|
|
|
|
|
|
|
region := viper.GetString("REGION")
|
|
|
|
|
|
|
|
viper.SetDefault("ALLOW_DELETE", true)
|
|
|
|
allowDelete := viper.GetBool("ALLOW_DELETE")
|
|
|
|
|
|
|
|
viper.SetDefault("FORCE_DOWNLOAD", true)
|
|
|
|
forceDownload := viper.GetBool("FORCE_DOWNLOAD")
|
|
|
|
|
|
|
|
viper.SetDefault("USE_SSL", true)
|
|
|
|
useSSL := viper.GetBool("USE_SSL")
|
|
|
|
|
|
|
|
viper.SetDefault("SKIP_SSL_VERIFICATION", false)
|
|
|
|
skipSSLVerification := viper.GetBool("SKIP_SSL_VERIFICATION")
|
|
|
|
|
|
|
|
listRecursive := viper.GetBool("LIST_RECURSIVE")
|
|
|
|
|
|
|
|
viper.SetDefault("PORT", "8080")
|
|
|
|
port := viper.GetString("PORT")
|
2019-09-05 00:44:02 +02:00
|
|
|
|
2021-04-21 11:37:38 +02:00
|
|
|
// Set up templates
|
|
|
|
templates, err := fs.Sub(templateFS, "web/template")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2022-04-30 12:34:24 +02:00
|
|
|
// Set up statics
|
2022-05-01 16:20:41 +02:00
|
|
|
statics, err := fs.Sub(staticFS, "web/static")
|
2022-04-30 12:33:46 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-05-31 16:10:41 +02:00
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
// Set up S3 client
|
2021-08-05 11:44:40 +02:00
|
|
|
opts := &minio.Options{
|
|
|
|
Secure: useSSL,
|
|
|
|
}
|
2022-03-23 10:46:09 +01:00
|
|
|
if useIam {
|
|
|
|
opts.Creds = credentials.NewIAM(iamEndpoint)
|
|
|
|
} else {
|
|
|
|
opts.Creds = credentials.NewStaticV4(accessKeyID, secretAccessKey, "")
|
|
|
|
}
|
|
|
|
|
2021-08-05 11:44:40 +02:00
|
|
|
if region != "" {
|
|
|
|
opts.Region = region
|
2017-05-08 23:07:07 +02:00
|
|
|
}
|
2021-04-16 14:55:19 +02:00
|
|
|
if useSSL && skipSSLVerification {
|
2021-08-05 11:44:40 +02:00
|
|
|
opts.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}} //nolint:gosec
|
|
|
|
}
|
|
|
|
s3, err := minio.New(endpoint, opts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(fmt.Errorf("error creating s3 client: %w", err))
|
2021-04-16 14:55:19 +02:00
|
|
|
}
|
2017-05-08 23:07:07 +02:00
|
|
|
|
|
|
|
// Set up router
|
2022-03-23 10:10:30 +01:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.Handle("/", http.RedirectHandler("/buckets", http.StatusPermanentRedirect)).Methods(http.MethodGet)
|
2022-04-30 12:33:46 +02:00
|
|
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(statics)))).Methods(http.MethodGet)
|
2022-03-23 10:10:30 +01:00
|
|
|
r.Handle("/buckets", s3manager.HandleBucketsView(s3, templates, allowDelete)).Methods(http.MethodGet)
|
|
|
|
r.Handle("/buckets/{bucketName}", s3manager.HandleBucketView(s3, templates, allowDelete, listRecursive)).Methods(http.MethodGet)
|
|
|
|
r.Handle("/api/buckets", s3manager.HandleCreateBucket(s3)).Methods(http.MethodPost)
|
|
|
|
if allowDelete {
|
|
|
|
r.Handle("/api/buckets/{bucketName}", s3manager.HandleDeleteBucket(s3)).Methods(http.MethodDelete)
|
|
|
|
}
|
|
|
|
r.Handle("/api/buckets/{bucketName}/objects", s3manager.HandleCreateObject(s3)).Methods(http.MethodPost)
|
|
|
|
r.Handle("/api/buckets/{bucketName}/objects/{objectName:.*}", s3manager.HandleGetObject(s3, forceDownload)).Methods(http.MethodGet)
|
|
|
|
if allowDelete {
|
|
|
|
r.Handle("/api/buckets/{bucketName}/objects/{objectName:.*}", s3manager.HandleDeleteObject(s3)).Methods(http.MethodDelete)
|
|
|
|
}
|
2018-05-31 16:10:41 +02:00
|
|
|
|
|
|
|
lr := logging.Handler(os.Stdout)(r)
|
2018-11-30 23:09:00 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":"+port, lr))
|
2017-05-08 23:07:07 +02:00
|
|
|
}
|