s3browser-backend/internal/s3Broswer.go
2021-09-24 15:39:23 +02:00

84 lines
1.9 KiB
Go

package s3browser
import (
"context"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
log "github.com/sirupsen/logrus"
gql "git.kapelle.org/niklas/s3browser/internal/gql"
httpserver "git.kapelle.org/niklas/s3browser/internal/httpserver"
types "git.kapelle.org/niklas/s3browser/internal/types"
)
var bucketName string
// setupS3Client connect the s3Client
func setupS3Client(config types.AppConfig) (*minio.Client, error) {
minioClient, err := minio.New(config.S3Endoint, &minio.Options{
Creds: credentials.NewStaticV4(config.S3AccessKey, config.S3SecretKey, ""),
Secure: config.S3SSL,
})
if err != nil {
return nil, err
}
exists, err := minioClient.BucketExists(context.Background(), config.S3Bucket)
if err != nil {
return nil, err
}
if !exists {
return nil, fmt.Errorf("Bucket '%s' does not exist", config.S3Bucket)
}
bucketName = config.S3Bucket
return minioClient, nil
}
// Start starts the app
func Start(config types.AppConfig) {
if config.LogDebug {
log.SetLevel(log.DebugLevel)
}
log.Info("Starting")
log.Debug("Setting up s3 client")
s3Client, err := setupS3Client(config)
if err != nil {
log.Error("Failed to setup s3 client: ", err.Error())
return
}
log.Info("s3 client connected")
log.Debug("Creating dataloader")
loaderMap := createDataloader(config)
log.Debug("Generating graphq schema")
gql.GraphqlTypes()
schema, err := gql.GraphqlSchema()
if err != nil {
log.Error("Failed to generate graphq schemas: ", err.Error())
return
}
resolveContext := context.WithValue(context.Background(), "s3Client", s3Client)
resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
log.Debug("Starting HTTP server")
err = httpserver.InitHttp(resolveContext, schema, config.Address)
if err != nil {
log.Error("Failed to start webserver: ", err.Error())
return
}
}