Return error from s3 constructor

This commit is contained in:
Lena Fuhrimann 2017-04-09 22:27:41 +02:00
parent 39521e331d
commit a40479a25f
2 changed files with 20 additions and 19 deletions

20
main.go
View file

@ -19,25 +19,29 @@ const (
)
func main() {
s3 := newMinioClient()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
router := mux.NewRouter()
s3, err := newMinioClient()
if err != nil {
log.Fatalln("error creating s3 client:", err)
}
router.
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
r := mux.NewRouter()
r.
Methods(http.MethodGet).
Path("/").
Handler(adapters.Adapt(
IndexViewHandler(),
logging.Handler(logger),
))
router.
r.
Methods(http.MethodGet).
Path("/buckets").
Handler(adapters.Adapt(
BucketsViewHandler(s3),
logging.Handler(logger),
))
router.
r.
Methods(http.MethodGet).
Path("/buckets/{bucketName}").
Handler(adapters.Adapt(
@ -45,7 +49,7 @@ func main() {
logging.Handler(logger),
))
api := router.PathPrefix("/api").Subrouter()
api := r.PathPrefix("/api").Subrouter()
br := api.PathPrefix("/buckets").Subrouter()
br.
@ -88,5 +92,5 @@ func main() {
if len(port) == 0 {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, router))
log.Fatal(http.ListenAndServe(":"+port, r))
}

View file

@ -1,16 +1,16 @@
package main
import (
"log"
"errors"
"os"
"github.com/minio/minio-go"
)
// newMinioClient creates a new Minio client
func newMinioClient() *minio.Client {
func newMinioClient() (*minio.Client, error) {
var err error
var client *minio.Client
var c *minio.Client
s3Endpoint := os.Getenv("S3_ENDPOINT")
if s3Endpoint == "" {
@ -19,22 +19,19 @@ func newMinioClient() *minio.Client {
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
if s3AccessKeyID == "" {
log.Fatal("Please set S3_ACCESS_KEY_ID")
return c, errors.New("no S3_ACCESS_KEY_ID found")
}
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
if s3SecretAccessKey == "" {
log.Fatal("Please set S3_SECRET_ACCESS_KEY")
return c, errors.New("no S3_SECRET_ACCESS_KEY found")
}
if os.Getenv("V2_SIGNING") == "true" {
client, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
c, err = minio.NewV2(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
} else {
client, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
}
if err != nil {
log.Fatal(err)
c, err = minio.New(s3Endpoint, s3AccessKeyID, s3SecretAccessKey, true)
}
return client
return c, err
}