s3browser-backend/internal/s3Broswer.go

112 lines
2.4 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-08-01 11:00:47 +00:00
"time"
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
)
// AppConfig general config
type AppConfig struct {
2021-08-01 11:00:47 +00:00
S3Endoint string
S3AccessKey string
S3SecretKey string
S3SSL bool
2021-09-14 13:12:09 +00:00
S3Bucket string
2021-08-01 11:00:47 +00:00
CacheTTL time.Duration
CacheCleanup time.Duration
2021-09-09 11:41:38 +00:00
Address string
LogDebug bool
2021-07-26 12:52:36 +00:00
}
// File represents a file with its metadata
type File struct {
2021-08-06 17:25:07 +00:00
ID string `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
ContentType string `json:"contentType"`
ETag string `json:"etag"`
LastModified time.Time `json:"lastModified"`
2021-07-26 12:52:36 +00:00
}
// Directory represents a directory with its metadata
type Directory struct {
ID string `json:"id"`
Name string `json:"name"`
Files []File `json:"files"`
Directorys []Directory `json:"directorys"`
}
2021-09-23 21:54:53 +00:00
var bucketName string
2021-07-26 12:52:36 +00:00
// setupS3Client connect the s3Client
2021-08-12 15:48:28 +00:00
func setupS3Client(config 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
func Start(config AppConfig) {
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-07-26 12:52:36 +00:00
graphqlTypes()
schema, err := graphqlSchema()
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-09 11:41:38 +00:00
err = 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
}