s3manager-web/cmd/s3manager/main.go

116 lines
2.9 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/mastertinner/adapters"
"github.com/mastertinner/adapters/logging"
"github.com/mastertinner/s3manager"
minio "github.com/minio/minio-go"
)
func main() {
var (
port = flag.String("port", "8080", "the port the app should listen on")
endpoint = flag.String("endpoint", "s3.amazonaws.com", "the s3 endpoint to use")
accessKeyID = flag.String("access-key-id", "", "your s3 access key ID")
secretAccessKey = flag.String("secret-access-key", "", "your s3 secret access key")
v2Signing = flag.Bool("v2-signing", false, "set this flag if your S3 provider still uses V2 signing")
)
flag.Parse()
if *accessKeyID == "" || *secretAccessKey == "" {
flag.Usage()
os.Exit(2)
}
// Set up S3 client
var s3 *minio.Client
var err error
if *v2Signing {
s3, err = minio.NewV2(*endpoint, *accessKeyID, *secretAccessKey, true)
} else {
s3, err = minio.New(*endpoint, *accessKeyID, *secretAccessKey, true)
}
if err != nil {
log.Fatalln(err)
}
// Set up logger
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
// Set up router
r := mux.NewRouter().StrictSlash(true)
r.
Methods(http.MethodGet).
Path("/").
Handler(adapters.Adapt(
http.RedirectHandler("/buckets", http.StatusPermanentRedirect),
logging.Handler(logger),
))
r.
Methods(http.MethodGet).
Path("/buckets").
Handler(adapters.Adapt(
s3manager.BucketsViewHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodGet).
Path("/buckets/{bucketName}").
Handler(adapters.Adapt(
s3manager.BucketViewHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodPost).
Path("/api/buckets").
Handler(adapters.Adapt(
s3manager.CreateBucketHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodDelete).
Path("/api/buckets/{bucketName}").
Handler(adapters.Adapt(
s3manager.DeleteBucketHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodPost).
Headers(s3manager.HeaderContentType, s3manager.ContentTypeJSON).
Path("/api/buckets/{bucketName}/objects").
Handler(adapters.Adapt(
s3manager.CopyObjectHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodPost).
HeadersRegexp(s3manager.HeaderContentType, s3manager.ContentTypeMultipartForm).
Path("/api/buckets/{bucketName}/objects").
Handler(adapters.Adapt(
s3manager.CreateObjectHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodGet).
Path("/api/buckets/{bucketName}/objects/{objectName}").
Handler(adapters.Adapt(
s3manager.GetObjectHandler(s3),
logging.Handler(logger),
))
r.
Methods(http.MethodDelete).
Path("/api/buckets/{bucketName}/objects/{objectName}").
Handler(adapters.Adapt(
s3manager.DeleteObjectHandler(s3),
logging.Handler(logger),
))
log.Fatal(http.ListenAndServe(":"+*port, r))
}