106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package s3browser
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// AppConfig general config
|
|
type AppConfig struct {
|
|
S3Endoint string
|
|
S3AccessKey string
|
|
S3SecretKey string
|
|
S3SSL bool
|
|
S3Buket string
|
|
CacheTTL time.Duration
|
|
CacheCleanup time.Duration
|
|
}
|
|
|
|
// File represents a file with its metadata
|
|
type File struct {
|
|
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"`
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
var bucketName = "dev"
|
|
|
|
// setupS3Client connect the s3Client
|
|
func setupS3Client(config 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.S3Buket)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !exists {
|
|
return nil, fmt.Errorf("Bucket '%s' does not exist", config.S3Buket)
|
|
}
|
|
|
|
return minioClient, nil
|
|
}
|
|
|
|
// Start starts the app
|
|
func Start(config AppConfig) {
|
|
|
|
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")
|
|
graphqlTypes()
|
|
schema, err := 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 = initHttp(resolveContext, schema)
|
|
|
|
if err != nil {
|
|
log.Error("Failed to start webserver: ", err.Error())
|
|
return
|
|
}
|
|
}
|