s3manager-web/cmd/s3manager/main.go

62 lines
1.8 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package main
import (
2019-09-05 00:44:02 +02:00
"fmt"
2017-05-08 23:07:07 +02:00
"log"
"net/http"
"os"
2018-03-14 21:53:35 +01:00
"path/filepath"
2020-05-04 15:35:59 +02:00
"strings"
2017-05-08 23:07:07 +02:00
2018-01-06 14:09:06 +01:00
"github.com/mastertinner/adapters/logging"
2018-03-14 21:53:35 +01:00
"github.com/mastertinner/s3manager/internal/app/s3manager"
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
2017-05-08 23:07:07 +02:00
minio "github.com/minio/minio-go"
)
func main() {
2020-05-04 10:33:23 +02:00
endpoint, ok := os.LookupEnv("ENDPOINT")
if !ok {
endpoint = "s3.amazonaws.com"
}
2018-11-30 23:09:00 +01:00
accessKeyID, ok := os.LookupEnv("ACCESS_KEY_ID")
if !ok {
log.Fatal("please provide ACCESS_KEY_ID")
}
secretAccessKey, ok := os.LookupEnv("SECRET_ACCESS_KEY")
if !ok {
log.Fatal("please provide SECRET_ACCESS_KEY")
}
2020-05-04 15:35:59 +02:00
useSSLEnvVar, ok := os.LookupEnv("USE_SSL")
if !ok {
useSSLEnvVar = "true"
}
useSSL := strings.ToLower(useSSLEnvVar) == "true"
2018-11-30 23:09:00 +01:00
port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
}
2019-09-05 00:44:02 +02:00
2018-05-31 16:10:41 +02:00
tmplDir := filepath.Join("web", "template")
2017-05-08 23:07:07 +02:00
// Set up S3 client
2020-05-04 15:35:59 +02:00
s3, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
2017-05-08 23:07:07 +02:00
if err != nil {
2019-09-05 00:44:02 +02:00
log.Fatalln(fmt.Errorf("error creating s3 client: %w", err))
2017-05-08 23:07:07 +02:00
}
// Set up router
2018-05-31 16:10:41 +02:00
r := way.NewRouter()
r.Handle(http.MethodGet, "/", http.RedirectHandler("/buckets", http.StatusPermanentRedirect))
r.Handle(http.MethodGet, "/buckets", s3manager.HandleBucketsView(s3, tmplDir))
r.Handle(http.MethodGet, "/buckets/:bucketName", s3manager.HandleBucketView(s3, tmplDir))
r.Handle(http.MethodPost, "/api/buckets", s3manager.HandleCreateBucket(s3))
r.Handle(http.MethodDelete, "/api/buckets/:bucketName", s3manager.HandleDeleteBucket(s3))
r.Handle(http.MethodPost, "/api/buckets/:bucketName/objects", s3manager.HandleCreateObject(s3))
r.Handle(http.MethodGet, "/api/buckets/:bucketName/objects/:objectName", s3manager.HandleGetObject(s3))
r.Handle(http.MethodDelete, "/api/buckets/:bucketName/objects/:objectName", s3manager.HandleDeleteObject(s3))
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
}