2021-07-26 12:52:36 +00:00
|
|
|
package s3browser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-12 15:48:28 +00:00
|
|
|
"fmt"
|
2021-07-26 12:52:36 +00:00
|
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2021-08-12 15:48:28 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-07-26 12:52:36 +00:00
|
|
|
|
2021-09-24 13:39:23 +00:00
|
|
|
gql "git.kapelle.org/niklas/s3browser/internal/gql"
|
|
|
|
httpserver "git.kapelle.org/niklas/s3browser/internal/httpserver"
|
|
|
|
types "git.kapelle.org/niklas/s3browser/internal/types"
|
|
|
|
)
|
2021-07-26 12:52:36 +00:00
|
|
|
|
2021-09-23 21:54:53 +00:00
|
|
|
var bucketName string
|
2021-07-26 12:52:36 +00:00
|
|
|
|
|
|
|
// setupS3Client connect the s3Client
|
2021-09-24 13:39:23 +00:00
|
|
|
func setupS3Client(config types.AppConfig) (*minio.Client, error) {
|
2021-07-26 12:52:36 +00:00
|
|
|
minioClient, err := minio.New(config.S3Endoint, &minio.Options{
|
|
|
|
Creds: credentials.NewStaticV4(config.S3AccessKey, config.S3SecretKey, ""),
|
|
|
|
Secure: config.S3SSL,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-08-12 15:48:28 +00:00
|
|
|
return nil, err
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 13:12:09 +00:00
|
|
|
exists, err := minioClient.BucketExists(context.Background(), config.S3Bucket)
|
2021-07-26 12:52:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-12 15:48:28 +00:00
|
|
|
return nil, err
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2021-09-14 13:12:09 +00:00
|
|
|
return nil, fmt.Errorf("Bucket '%s' does not exist", config.S3Bucket)
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 21:54:53 +00:00
|
|
|
bucketName = config.S3Bucket
|
|
|
|
|
2021-08-12 15:48:28 +00:00
|
|
|
return minioClient, nil
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the app
|
2021-09-24 13:39:23 +00:00
|
|
|
func Start(config types.AppConfig) {
|
2021-07-26 12:52:36 +00:00
|
|
|
|
2021-09-09 11:41:38 +00:00
|
|
|
if config.LogDebug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
2021-08-12 15:48:28 +00:00
|
|
|
|
|
|
|
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")
|
2021-08-06 11:48:49 +00:00
|
|
|
loaderMap := createDataloader(config)
|
2021-07-26 12:52:36 +00:00
|
|
|
|
2021-08-12 15:48:28 +00:00
|
|
|
log.Debug("Generating graphq schema")
|
2021-09-24 13:39:23 +00:00
|
|
|
gql.GraphqlTypes()
|
|
|
|
schema, err := gql.GraphqlSchema()
|
2021-07-26 12:52:36 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-12 15:48:28 +00:00
|
|
|
log.Error("Failed to generate graphq schemas: ", err.Error())
|
|
|
|
return
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 21:10:23 +00:00
|
|
|
resolveContext := context.WithValue(context.Background(), "s3Client", s3Client)
|
|
|
|
resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
|
|
|
|
|
2021-08-12 15:48:28 +00:00
|
|
|
log.Debug("Starting HTTP server")
|
2021-09-24 13:39:23 +00:00
|
|
|
err = httpserver.InitHttp(resolveContext, schema, config.Address)
|
2021-08-06 14:31:07 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-08-12 15:48:28 +00:00
|
|
|
log.Error("Failed to start webserver: ", err.Error())
|
|
|
|
return
|
2021-08-06 14:31:07 +00:00
|
|
|
}
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|