103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
|
package s3browser
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/graph-gophers/dataloader"
|
||
|
"github.com/graphql-go/graphql"
|
||
|
"github.com/graphql-go/handler"
|
||
|
"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
|
||
|
}
|
||
|
|
||
|
// 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"
|
||
|
|
||
|
// initHttp setup and start the http server. Blocking
|
||
|
func initHttp(schema graphql.Schema, s3Client *minio.Client, loaderMap map[string]*dataloader.Loader) {
|
||
|
h := handler.New(&handler.Config{
|
||
|
Schema: &schema,
|
||
|
Pretty: true,
|
||
|
GraphiQL: false,
|
||
|
Playground: true,
|
||
|
})
|
||
|
|
||
|
resolveContext := context.WithValue(context.Background(), "s3Client", s3Client)
|
||
|
resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
|
||
|
|
||
|
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
|
||
|
h.ContextHandler(resolveContext, rw, r)
|
||
|
})
|
||
|
|
||
|
http.ListenAndServe(":8080", nil)
|
||
|
}
|
||
|
|
||
|
// 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)
|
||
|
|
||
|
loader := createDataloader()
|
||
|
|
||
|
graphqlTypes()
|
||
|
schema, err := graphqlSchema()
|
||
|
|
||
|
if err != nil {
|
||
|
log.Panic(err)
|
||
|
}
|
||
|
|
||
|
initHttp(schema, s3Client, loader)
|
||
|
}
|