Don't use global variables

This commit is contained in:
Lena Fuhrimann 2017-03-09 21:20:40 +01:00
parent c7d509f248
commit 3fc3b15562
7 changed files with 65 additions and 47 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
vendor
s3manager
s3-manager
glide.lock

View file

@ -18,8 +18,8 @@ type CopyObjectInfo struct {
SourceObjectName string `json:"sourceObjectName"`
}
// createBucketHandler creates a new bucket
func createBucketHandler(w http.ResponseWriter, r *http.Request) {
// CreateBucketHandler creates a new bucket
func (s *Server) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
err := json.NewDecoder(r.Body).Decode(&bucket)
@ -28,7 +28,7 @@ func createBucketHandler(w http.ResponseWriter, r *http.Request) {
return
}
err = minioClient.MakeBucket(bucket.Name, "")
err = s.s3.MakeBucket(bucket.Name, "")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -36,6 +36,7 @@ func createBucketHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@ -44,24 +45,24 @@ func createBucketHandler(w http.ResponseWriter, r *http.Request) {
}
// deleteBucketHandler deletes a bucket
func deleteBucketHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) deleteBucketHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := minioClient.RemoveBucket(vars["bucketName"])
err := s.s3.RemoveBucket(vars["bucketName"])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.WriteHeader(http.StatusNoContent)
}
// getObjectHandler downloads an object to the client
func getObjectHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) getObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
objectName := vars["objectName"]
object, err := minioClient.GetObject(vars["bucketName"], objectName)
object, err := s.s3.GetObject(vars["bucketName"], objectName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -78,7 +79,7 @@ func getObjectHandler(w http.ResponseWriter, r *http.Request) {
}
// createObjectHandler allows to upload a new object
func createObjectHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) createObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if r.Header.Get("Content-Type") == "application/json" {
@ -94,7 +95,7 @@ func createObjectHandler(w http.ResponseWriter, r *http.Request) {
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
fmt.Println(copy)
fmt.Println(objectSource)
err = minioClient.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
err = s.s3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -120,7 +121,7 @@ func createObjectHandler(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()
_, err = minioClient.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
_, err = s.s3.PutObject(vars["bucketName"], handler.Filename, file, "application/octet-stream")
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
@ -131,10 +132,10 @@ func createObjectHandler(w http.ResponseWriter, r *http.Request) {
}
// deleteObjectHandler deletes an object
func deleteObjectHandler(w http.ResponseWriter, r *http.Request) {
func (s *Server) deleteObjectHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := minioClient.RemoveObject(vars["bucketName"], vars["objectName"])
err := s.s3.RemoveObject(vars["bucketName"], vars["objectName"])
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return

View file

@ -4,8 +4,15 @@ import (
"log"
"net/http"
"os"
minio "github.com/minio/minio-go"
)
// Server is a server containing a minio client
type Server struct {
s3 *minio.Client
}
func main() {
port := os.Getenv("PORT")
if len(port) == 0 {

View file

@ -1,16 +1,15 @@
package main
import (
"log"
"os"
minio "github.com/minio/minio-go"
)
var minioClient *minio.Client
func init() {
// NewMinioClient creates a new Minio client
func NewMinioClient() *minio.Client {
var err error
var client *minio.Client
s3Endpoint := os.Getenv("S3_ENDPOINT")
if len(s3Endpoint) == 0 {
@ -19,20 +18,22 @@ func init() {
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
if len(s3AccessKeyID) == 0 {
log.Fatalln("Please set S3_ACCESS_KEY_ID")
panic("Please set S3_ACCESS_KEY_ID")
}
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
if len(s3SecretAccessKey) == 0 {
log.Fatalln("Please set S3_SECRET_ACCESS_KEY")
panic("Please set S3_SECRET_ACCESS_KEY")
}
if os.Getenv("V2_SIGNING") == "true" {
minioClient, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
client, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
} else {
minioClient, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
client, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
}
if err != nil {
log.Fatalln(err)
panic(err)
}
return client
}

View file

@ -1,7 +1,6 @@
package main
import (
"fmt"
"html/template"
"net/http"
"path"
@ -27,29 +26,32 @@ func indexPageHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/buckets", http.StatusPermanentRedirect)
}
// bucketsHandler handles the main page
func bucketsPageHandler(w http.ResponseWriter, r *http.Request) {
// bucketsPageHandler shows all buckets
func (s *Server) bucketsPageHandler(w http.ResponseWriter, r *http.Request) {
lp := path.Join("templates", "layout.html")
ip := path.Join("templates", "index.html")
t, err := template.ParseFiles(lp, ip)
if err != nil {
panic(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
buckets, err := minioClient.ListBuckets()
buckets, err := s.s3.ListBuckets()
if err != nil {
panic(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = t.ExecuteTemplate(w, "layout", buckets)
if err != nil {
panic(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
// bucketHandler shows the details page of a bucket
func bucketPageHandler(w http.ResponseWriter, r *http.Request) {
// bucketPageHandler shows the details page of a bucket
func (s *Server) bucketPageHandler(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]
var objects []ObjectWithIcon
@ -58,18 +60,19 @@ func bucketPageHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(lp, bp)
if err != nil {
panic(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
doneCh := make(chan struct{})
objectCh := minioClient.ListObjectsV2(bucketName, "", false, doneCh)
objectCh := s.s3.ListObjectsV2(bucketName, "", false, doneCh)
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
w.WriteHeader(http.StatusInternalServerError)
return
}
objectWithIcon := ObjectWithIcon{object, getIcon(object.Key)}
objectWithIcon := ObjectWithIcon{object, icon(object.Key)}
objects = append(objects, objectWithIcon)
}
@ -80,11 +83,13 @@ func bucketPageHandler(w http.ResponseWriter, r *http.Request) {
err = t.ExecuteTemplate(w, "layout", bucketPage)
if err != nil {
panic(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func getIcon(fileName string) string {
// icon returns an icon for a file type
func icon(fileName string) string {
e := path.Ext(fileName)
switch e {

View file

@ -12,6 +12,10 @@ type Route struct {
// Routes is an array of routes
type Routes []Route
var s = &Server{
s3: NewMinioClient(),
}
var routes = Routes{
Route{
"GET",
@ -21,36 +25,36 @@ var routes = Routes{
Route{
"GET",
"/buckets",
Chain(bucketsPageHandler, Logger()),
Chain(s.bucketsPageHandler, Logger()),
},
Route{
"GET",
"/buckets/{bucketName}",
Chain(bucketPageHandler, Logger()),
Chain(s.bucketPageHandler, Logger()),
},
Route{
"POST",
"/api/buckets",
Chain(createBucketHandler, Logger()),
Chain(s.CreateBucketHandler, Logger()),
},
Route{
"DELETE",
"/api/buckets/{bucketName}",
Chain(deleteBucketHandler, Logger()),
Chain(s.deleteBucketHandler, Logger()),
},
Route{
"GET",
"/api/buckets/{bucketName}/objects/{objectName}",
Chain(getObjectHandler, Logger()),
Chain(s.getObjectHandler, Logger()),
},
Route{
"POST",
"/api/buckets/{bucketName}/objects",
Chain(createObjectHandler, Logger()),
Chain(s.createObjectHandler, Logger()),
},
Route{
"DELETE",
"/api/buckets/{bucketName}/objects/{objectName}",
Chain(deleteObjectHandler, Logger()),
Chain(s.deleteObjectHandler, Logger()),
},
}

View file

@ -10,7 +10,7 @@
<title>S3 Manager</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/css/materialize.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css">
</head>
<body>
@ -18,7 +18,7 @@
{{ template "content" . }}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.8/js/materialize.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>
<script>
$(document).ready(function(){
$('.modal').modal();