s3browser-backend/internal/s3Broswer.go
2021-08-03 23:10:23 +02:00

87 lines
1.9 KiB
Go

package s3browser
import (
"context"
"log"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
// 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"`
}
// 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 {
minioClient, err := minio.New(config.S3Endoint, &minio.Options{
Creds: credentials.NewStaticV4(config.S3AccessKey, config.S3SecretKey, ""),
Secure: config.S3SSL,
})
if err != nil {
log.Fatalln(err)
}
exists, err := minioClient.BucketExists(context.Background(), config.S3Buket)
if err != nil {
log.Fatalln(err)
}
if !exists {
log.Fatalf("Bucket '%s' does not exist", config.S3Buket)
} else {
log.Print("S3 client connected")
}
return minioClient
}
// Start starts the app
func Start(config AppConfig) {
s3Client := setupS3Client(config)
loaderMap, objCache := createDataloader(config)
graphqlTypes()
schema, err := graphqlSchema()
if err != nil {
log.Panic(err)
}
resolveContext := context.WithValue(context.Background(), "s3Client", s3Client)
resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
resolveContext = context.WithValue(resolveContext, "objCache", objCache)
initHttp(schema, resolveContext)
}