s3browser-backend/internal/s3Broswer.go

83 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"
"git.kapelle.org/niklas/s3browser/internal/db"
gql "git.kapelle.org/niklas/s3browser/internal/gql"
httpserver "git.kapelle.org/niklas/s3browser/internal/httpserver"
"git.kapelle.org/niklas/s3browser/internal/loader"
types "git.kapelle.org/niklas/s3browser/internal/types"
)
// 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)
}
return minioClient, nil
}
// Start starts the app
func Start(config types.AppConfig) {
if config.LogDebug {
log.SetLevel(log.DebugLevel)
}
log.Info("Starting")
s3Client, err := setupS3Client(config)
if err != nil {
log.Error("Failed to setup s3 client: ", err.Error())
return
}
dbStore, err := db.NewDB("mysql", "s3Browser:hunter2@/s3Browser")
if err != nil {
log.Error("Failed to connect DB: ", err.Error())
}
loader := loader.NewLoader(config)
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", loader)
resolveContext = context.WithValue(resolveContext, "dbStore", dbStore)
err = httpserver.InitHttp(resolveContext, schema, config.Address)
if err != nil {
log.Error("Failed to start webserver: ", err.Error())
return
}
}