s3manager-web/main.go

107 lines
2.3 KiB
Go
Raw Normal View History

2016-12-18 22:54:21 +01:00
package main
import (
"log"
"net/http"
"os"
2017-03-09 21:20:40 +01:00
2017-03-10 10:54:30 +01:00
"github.com/gorilla/mux"
2017-04-03 14:08:01 +02:00
"github.com/mastertinner/adapters"
"github.com/mastertinner/adapters/logging"
2016-12-18 22:54:21 +01:00
)
2017-04-07 10:07:23 +02:00
const (
tmplDirectory = "templates"
headerContentType = "Content-Type"
headerContentDisposition = "Content-Disposition"
contentTypeJSON = "application/json"
2017-04-14 19:52:16 +02:00
contentTypeMultipartForm = "multipart/form-data"
2017-04-07 10:07:23 +02:00
contentTypeOctetStream = "application/octet-stream"
)
2016-12-18 22:54:21 +01:00
func main() {
2017-04-09 22:27:41 +02:00
s3, err := newMinioClient()
if err != nil {
log.Fatalln("error creating s3 client:", err)
}
2017-03-31 09:56:06 +02:00
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
2017-03-10 10:54:30 +01:00
2017-04-09 22:27:41 +02:00
r := mux.NewRouter()
r.
2017-04-07 08:59:24 +02:00
Methods(http.MethodGet).
2017-03-10 10:54:30 +01:00
Path("/").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-10 15:55:47 +02:00
http.RedirectHandler("/buckets", http.StatusPermanentRedirect),
2017-04-03 14:08:01 +02:00
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
2017-04-09 22:27:41 +02:00
r.
2017-04-07 08:59:24 +02:00
Methods(http.MethodGet).
2017-03-10 10:54:30 +01:00
Path("/buckets").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
BucketsViewHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
2017-04-09 22:27:41 +02:00
r.
2017-04-07 08:59:24 +02:00
Methods(http.MethodGet).
2017-03-10 10:54:30 +01:00
Path("/buckets/{bucketName}").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
BucketViewHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
2017-03-10 10:54:30 +01:00
2017-04-09 22:27:41 +02:00
api := r.PathPrefix("/api").Subrouter()
2017-03-10 10:54:30 +01:00
2017-03-30 22:48:27 +02:00
br := api.PathPrefix("/buckets").Subrouter()
br.
2017-04-07 08:59:24 +02:00
Methods(http.MethodPost).
2017-03-10 10:54:30 +01:00
Path("").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
CreateBucketHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
br.
2017-04-07 08:59:24 +02:00
Methods(http.MethodDelete).
2017-03-10 10:54:30 +01:00
Path("/{bucketName}").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
DeleteBucketHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
br.
2017-04-07 08:59:24 +02:00
Methods(http.MethodPost).
2017-04-14 19:52:16 +02:00
Headers(headerContentType, contentTypeJSON).
2017-03-10 10:54:30 +01:00
Path("/{bucketName}/objects").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-21 10:36:22 +02:00
CopyObjectHandler(s3),
2017-04-14 19:52:16 +02:00
logging.Handler(logger),
))
br.
Methods(http.MethodPost).
HeadersRegexp(headerContentType, contentTypeMultipartForm).
Path("/{bucketName}/objects").
Handler(adapters.Adapt(
2017-04-21 10:36:22 +02:00
CreateObjectHandler(s3),
2017-04-03 14:08:01 +02:00
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
br.
2017-04-07 08:59:24 +02:00
Methods(http.MethodGet).
2017-03-10 10:54:30 +01:00
Path("/{bucketName}/objects/{objectName}").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
GetObjectHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
br.
2017-04-07 08:59:24 +02:00
Methods(http.MethodDelete).
2017-03-10 10:54:30 +01:00
Path("/{bucketName}/objects/{objectName}").
2017-03-30 22:48:27 +02:00
Handler(adapters.Adapt(
2017-04-03 14:08:01 +02:00
DeleteObjectHandler(s3),
logging.Handler(logger),
2017-03-30 22:48:27 +02:00
))
2016-12-18 22:54:21 +01:00
2017-03-30 14:00:06 +02:00
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
2017-04-09 22:27:41 +02:00
log.Fatal(http.ListenAndServe(":"+port, r))
2016-12-18 22:54:21 +01:00
}