s3browser-backend/internal/s3Broswer.go

83 lines
1.9 KiB
Go
Raw Normal View History

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-10-20 14:11:18 +00:00
"git.kapelle.org/niklas/s3browser/internal/db"
2021-09-24 13:39:23 +00:00
gql "git.kapelle.org/niklas/s3browser/internal/gql"
httpserver "git.kapelle.org/niklas/s3browser/internal/httpserver"
2021-10-14 17:00:11 +00:00
"git.kapelle.org/niklas/s3browser/internal/loader"
2021-09-24 13:39:23 +00:00
types "git.kapelle.org/niklas/s3browser/internal/types"
)
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-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")
s3Client, err := setupS3Client(config)
if err != nil {
log.Error("Failed to setup s3 client: ", err.Error())
return
}
2021-11-04 19:28:37 +00:00
dbStore, err := db.NewDB(config.DSN)
2021-10-20 14:11:18 +00:00
if err != nil {
log.Error("Failed to connect DB: ", err.Error())
}
2021-10-14 17:00:11 +00:00
loader := loader.NewLoader(config)
2021-07-26 12:52:36 +00:00
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)
2021-10-14 17:00:11 +00:00
resolveContext = context.WithValue(resolveContext, "loader", loader)
2021-10-20 14:11:18 +00:00
resolveContext = context.WithValue(resolveContext, "dbStore", dbStore)
2021-08-03 21:10:23 +00:00
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
}