Use constants

This commit is contained in:
Lena Fuhrimann 2017-04-07 10:07:23 +02:00
parent ce5f1ea0a1
commit fb10a7ac5a
6 changed files with 19 additions and 10 deletions

View file

@ -27,8 +27,8 @@ func BucketViewHandler(s3 S3Client) http.Handler {
bucketName := mux.Vars(r)["bucketName"] bucketName := mux.Vars(r)["bucketName"]
var objs []ObjectWithIcon var objs []ObjectWithIcon
l := path.Join("templates", "layout.html.tmpl") l := path.Join(tmplDirectory, "layout.html.tmpl")
p := path.Join("templates", "bucket.html.tmpl") p := path.Join(tmplDirectory, "bucket.html.tmpl")
t, err := template.ParseFiles(l, p) t, err := template.ParseFiles(l, p)
if err != nil { if err != nil {

View file

@ -9,8 +9,8 @@ import (
// BucketsViewHandler shows all buckets // BucketsViewHandler shows all buckets
func BucketsViewHandler(s3 S3Client) http.Handler { func BucketsViewHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := path.Join("templates", "layout.html.tmpl") l := path.Join(tmplDirectory, "layout.html.tmpl")
p := path.Join("templates", "buckets.html.tmpl") p := path.Join(tmplDirectory, "buckets.html.tmpl")
t, err := template.ParseFiles(l, p) t, err := template.ParseFiles(l, p)
if err != nil { if err != nil {

View file

@ -26,7 +26,7 @@ func CreateBucketHandler(s3 S3Client) http.Handler {
return return
} }
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set(headerContentType, contentTypeJSON)
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket) err = json.NewEncoder(w).Encode(bucket)

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
minio "github.com/minio/minio-go" minio "github.com/minio/minio-go"
@ -22,7 +23,7 @@ func CreateObjectHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
if r.Header.Get("Content-Type") == "application/json" { if strings.Contains(r.Header.Get(headerContentType), contentTypeJSON) {
var copy CopyObjectInfo var copy CopyObjectInfo
err := json.NewDecoder(r.Body).Decode(&copy) err := json.NewDecoder(r.Body).Decode(&copy)
@ -41,7 +42,7 @@ func CreateObjectHandler(s3 S3Client) http.Handler {
return return
} }
w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Header().Set(headerContentType, contentTypeJSON)
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(copy) err = json.NewEncoder(w).Encode(copy)
@ -66,7 +67,7 @@ func CreateObjectHandler(s3 S3Client) http.Handler {
} }
defer file.Close() defer file.Close()
_, err = s3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream") _, err = s3.PutObject(vars["bucketName"], handler.Filename, file, contentTypeOctetStream)
if err != nil { if err != nil {
msg := "error putting object" msg := "error putting object"
handleHTTPError(w, msg, err, http.StatusInternalServerError) handleHTTPError(w, msg, err, http.StatusInternalServerError)

View file

@ -21,8 +21,8 @@ func GetObjectHandler(s3 S3Client) http.Handler {
return return
} }
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", objectName)) w.Header().Set(headerContentDisposition, fmt.Sprintf("attachment; filename=\"%s\"", objectName))
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set(headerContentType, contentTypeOctetStream)
_, err = io.Copy(w, object) _, err = io.Copy(w, object)
if err != nil { if err != nil {

View file

@ -10,6 +10,14 @@ import (
"github.com/mastertinner/adapters/logging" "github.com/mastertinner/adapters/logging"
) )
const (
tmplDirectory = "templates"
headerContentType = "Content-Type"
headerContentDisposition = "Content-Disposition"
contentTypeJSON = "application/json"
contentTypeOctetStream = "application/octet-stream"
)
func main() { func main() {
s3 := newMinioClient() s3 := newMinioClient()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime) logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)