2021-07-26 12:52:36 +00:00
|
|
|
package s3browser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AppConfig general config
|
|
|
|
type AppConfig struct {
|
2021-08-01 11:00:47 +00:00
|
|
|
S3Endoint string
|
|
|
|
S3AccessKey string
|
|
|
|
S3SecretKey string
|
|
|
|
S3SSL bool
|
|
|
|
S3Buket string
|
|
|
|
CacheTTL time.Duration
|
|
|
|
CacheCleanup time.Duration
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2021-08-06 11:48:49 +00:00
|
|
|
loaderMap := createDataloader(config)
|
2021-07-26 12:52:36 +00:00
|
|
|
|
|
|
|
graphqlTypes()
|
|
|
|
schema, err := graphqlSchema()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2021-08-03 21:10:23 +00:00
|
|
|
resolveContext := context.WithValue(context.Background(), "s3Client", s3Client)
|
|
|
|
resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
|
|
|
|
|
2021-08-06 14:31:07 +00:00
|
|
|
err = initHttp(resolveContext, schema)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to start webserver: %s", err.Error())
|
|
|
|
}
|
2021-07-26 12:52:36 +00:00
|
|
|
}
|