s3manager-web/buckets/create-handler.go

42 lines
1 KiB
Go
Raw Normal View History

2017-03-30 22:48:27 +02:00
package buckets
import (
"encoding/json"
"net/http"
2017-04-02 17:10:36 +02:00
"github.com/mastertinner/s3-manager/datasources"
"github.com/mastertinner/s3-manager/utils"
2017-03-30 22:48:27 +02:00
minio "github.com/minio/minio-go"
)
// CreateHandler creates a new bucket
2017-04-02 17:10:36 +02:00
func CreateHandler(s3 datasources.S3Client) http.Handler {
2017-03-30 22:48:27 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
err := json.NewDecoder(r.Body).Decode(&bucket)
if err != nil {
msg := "error decoding json"
2017-04-02 17:10:36 +02:00
utils.HandleHTTPError(w, msg, err, http.StatusUnprocessableEntity)
2017-03-30 22:48:27 +02:00
return
}
err = s3.MakeBucket(bucket.Name, "")
if err != nil {
msg := "error making bucket"
2017-04-02 17:10:36 +02:00
utils.HandleHTTPError(w, msg, err, http.StatusInternalServerError)
2017-03-30 22:48:27 +02:00
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
msg := "error encoding json"
2017-04-02 17:10:36 +02:00
utils.HandleHTTPError(w, msg, err, http.StatusInternalServerError)
2017-03-30 22:48:27 +02:00
return
}
})
}