Compare commits
3 Commits
926701125d
...
33237b6564
| Author | SHA1 | Date | |
|---|---|---|---|
| 33237b6564 | |||
| 447a0647b2 | |||
| 7253a9195d |
1
go.mod
1
go.mod
@@ -9,4 +9,5 @@ require (
|
||||
github.com/minio/minio-go/v7 v7.0.12
|
||||
github.com/opentracing/opentracing-go v1.2.0 // indirect
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
)
|
||||
|
||||
@@ -8,10 +8,12 @@ import (
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
"github.com/minio/minio-go/v7"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// listObjectsBatch batch func for calling s3.ListObjects()
|
||||
func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
log.Debug("listObjectsBatch: ", k.Keys())
|
||||
var results []*dataloader.Result
|
||||
|
||||
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
||||
@@ -32,6 +34,7 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result
|
||||
|
||||
// listObjects helper func for listObjectsBatch
|
||||
func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool) []minio.ObjectInfo {
|
||||
log.Debug("S3 call 'ListObjects': ", path)
|
||||
objectCh := s3Client.ListObjects(context.Background(), bukitName, minio.ListObjectsOptions{
|
||||
Prefix: path,
|
||||
Recursive: false,
|
||||
@@ -47,6 +50,7 @@ func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool)
|
||||
|
||||
// getFilesBatch batch func for getting all files in path. Uses "listObjects" dataloader
|
||||
func getFilesBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
log.Debug("getFilesBatch: ", k.Keys())
|
||||
var results []*dataloader.Result
|
||||
|
||||
loader, ok := c.Value("loader").(map[string]*dataloader.Loader)
|
||||
@@ -94,6 +98,7 @@ func getFilesBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
|
||||
// getFileBatch batch func for getting object info
|
||||
func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
log.Debug("getFileBatch: ", k.Keys())
|
||||
var results []*dataloader.Result
|
||||
|
||||
s3Client, ok := c.Value("s3Client").(*minio.Client)
|
||||
@@ -103,6 +108,7 @@ func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
}
|
||||
|
||||
for _, v := range k {
|
||||
log.Debug("S3 call 'StatObject': ", v.String())
|
||||
obj, err := s3Client.StatObject(context.Background(), bucketName, v.String(), minio.StatObjectOptions{})
|
||||
|
||||
if err != nil {
|
||||
@@ -129,6 +135,7 @@ func getFileBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
|
||||
// getDirsBatch batch func for getting dirs in a path
|
||||
func getDirsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
log.Debug("getDirsBatch: ", k.Keys())
|
||||
var results []*dataloader.Result
|
||||
|
||||
loader, ok := c.Value("loader").(map[string]*dataloader.Loader)
|
||||
@@ -172,6 +179,7 @@ func getDirsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result {
|
||||
|
||||
// handleLoaderError helper func when the whole batch failed
|
||||
func handleLoaderError(k dataloader.Keys, err error) []*dataloader.Result {
|
||||
log.Error(err.Error())
|
||||
var results []*dataloader.Result
|
||||
for range k {
|
||||
results = append(results, &dataloader.Result{
|
||||
|
||||
@@ -140,11 +140,7 @@ func graphqlTypes() {
|
||||
return nil, fmt.Errorf("Failed to parse Source for parent resolve")
|
||||
}
|
||||
|
||||
basename := filepath.Dir(source.ID)
|
||||
|
||||
if basename == "." {
|
||||
basename = "/"
|
||||
}
|
||||
basename := getPathFromId(source.ID)
|
||||
|
||||
return Directory{
|
||||
ID: basename,
|
||||
@@ -213,6 +209,10 @@ func loadFile(p graphql.ResolveParams) (*File, error) {
|
||||
thunk := loader["getFile"].Load(p.Context, dataloader.StringKey(source.ID))
|
||||
result, err := thunk()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
file, ok := result.(*File)
|
||||
|
||||
if !ok {
|
||||
|
||||
32
internal/helper.go
Normal file
32
internal/helper.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package s3browser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
)
|
||||
|
||||
func invalidateCache(ctx context.Context, id string) error {
|
||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to get loader from context")
|
||||
}
|
||||
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(getPathFromId(id)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getPathFromId(id string) string {
|
||||
dir := filepath.Dir(id)
|
||||
|
||||
if dir == "." {
|
||||
return "/"
|
||||
}
|
||||
|
||||
return dir + "/"
|
||||
}
|
||||
@@ -8,10 +8,12 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
"github.com/graphql-go/graphql"
|
||||
"github.com/graphql-go/graphql/gqlerrors"
|
||||
"github.com/graphql-go/handler"
|
||||
"github.com/minio/minio-go/v7"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// initHttp setup and start the http server. Blocking
|
||||
@@ -21,6 +23,15 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
|
||||
Pretty: true,
|
||||
GraphiQL: false,
|
||||
Playground: true,
|
||||
FormatErrorFn: func(err error) gqlerrors.FormattedError {
|
||||
switch err := err.(type) {
|
||||
case gqlerrors.FormattedError:
|
||||
log.Error("GQL: ", err.Message)
|
||||
case *gqlerrors.Error:
|
||||
log.Errorf("GQL: '%s' at '%v'", err.Message, err.Path)
|
||||
}
|
||||
return gqlerrors.FormatError(err)
|
||||
},
|
||||
})
|
||||
|
||||
http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) {
|
||||
@@ -45,6 +56,8 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
|
||||
func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||
s3Client := ctx.Value("s3Client").(*minio.Client)
|
||||
id := r.URL.Query().Get("id")
|
||||
|
||||
log.Debug("S3 call 'StatObject': ", id)
|
||||
objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
|
||||
|
||||
if err != nil {
|
||||
@@ -58,6 +71,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("S3 call 'GetObject': ", id)
|
||||
obj, err := s3Client.GetObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
|
||||
|
||||
if err != nil {
|
||||
@@ -80,13 +94,15 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
|
||||
s3Client := ctx.Value("s3Client").(*minio.Client)
|
||||
loader := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
|
||||
id := r.URL.Query().Get("id")
|
||||
|
||||
log.Debug("Upload file: ", id)
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
mimeType, _, _ := mime.ParseMediaType(contentType)
|
||||
|
||||
log.Debug("S3 call 'PutObject': ", id)
|
||||
info, err := s3Client.PutObject(context.Background(), bucketName, id, r.Body, r.ContentLength, minio.PutObjectOptions{
|
||||
ContentType: mimeType,
|
||||
})
|
||||
@@ -97,9 +113,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
|
||||
invalidateCache(ctx, info.Key)
|
||||
|
||||
rw.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
@@ -3,9 +3,7 @@ package s3browser
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
"github.com/minio/minio-go/v7"
|
||||
)
|
||||
|
||||
@@ -25,16 +23,7 @@ func deleteMutation(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
if !ok {
|
||||
return fmt.Errorf("Failed to get loader from context")
|
||||
}
|
||||
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(id))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id)))
|
||||
|
||||
return nil
|
||||
return invalidateCache(ctx, id)
|
||||
}
|
||||
|
||||
func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||
@@ -57,16 +46,8 @@ func copyMutation(ctx context.Context, src, dest string) (*File, error) {
|
||||
}
|
||||
|
||||
// Invalidate cache
|
||||
|
||||
loader, ok := ctx.Value("loader").(map[string]*dataloader.Loader)
|
||||
|
||||
// TODO: Do we want to error when the operation
|
||||
// has succeeded but the cache invalidation has failed ?
|
||||
if ok {
|
||||
loader["getFile"].Clear(ctx, dataloader.StringKey(info.Key))
|
||||
loader["listObjects"].Clear(ctx, dataloader.StringKey(info.Key))
|
||||
loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(info.Key)))
|
||||
}
|
||||
// TODO: check error
|
||||
invalidateCache(ctx, info.Key)
|
||||
|
||||
return &File{
|
||||
ID: info.Key,
|
||||
|
||||
@@ -2,11 +2,12 @@ package s3browser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AppConfig general config
|
||||
@@ -41,50 +42,64 @@ type Directory struct {
|
||||
var bucketName = "dev"
|
||||
|
||||
// setupS3Client connect the s3Client
|
||||
func setupS3Client(config AppConfig) *minio.Client {
|
||||
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 {
|
||||
log.Fatalln(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exists, err := minioClient.BucketExists(context.Background(), config.S3Buket)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
log.Fatalf("Bucket '%s' does not exist", config.S3Buket)
|
||||
} else {
|
||||
log.Print("S3 client connected")
|
||||
return nil, fmt.Errorf("Bucket '%s' does not exist", config.S3Buket)
|
||||
}
|
||||
|
||||
return minioClient
|
||||
return minioClient, nil
|
||||
}
|
||||
|
||||
// Start starts the app
|
||||
func Start(config AppConfig) {
|
||||
s3Client := setupS3Client(config)
|
||||
|
||||
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.Panic(err)
|
||||
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.Printf("Failed to start webserver: %s", err.Error())
|
||||
log.Error("Failed to start webserver: ", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
|
||||
"github.com/graph-gophers/dataloader"
|
||||
"github.com/graphql-go/graphql"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// graphqlSchema generate the schema with its root query and mutation
|
||||
@@ -24,6 +26,9 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
log.Debug("querry 'files': ", path)
|
||||
|
||||
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
||||
thunk := loader["getFiles"].Load(p.Context, dataloader.StringKey(path))
|
||||
return thunk()
|
||||
@@ -42,6 +47,9 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
log.Debug("querry 'directorys': ", path)
|
||||
|
||||
loader := p.Context.Value("loader").(map[string]*dataloader.Loader)
|
||||
thunk := loader["getDirs"].Load(p.Context, dataloader.StringKey(path))
|
||||
return thunk()
|
||||
@@ -60,6 +68,8 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
return nil, fmt.Errorf("Failed to parse args")
|
||||
}
|
||||
|
||||
log.Debug("querry 'file': ", id)
|
||||
|
||||
return File{
|
||||
ID: id,
|
||||
}, nil
|
||||
@@ -81,6 +91,8 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
return nil, fmt.Errorf("Failed to parse args")
|
||||
}
|
||||
|
||||
log.Debug("mutation 'delete': ", id)
|
||||
|
||||
return id, deleteMutation(p.Context, id)
|
||||
},
|
||||
},
|
||||
@@ -104,6 +116,8 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
return nil, fmt.Errorf("Failed to parse args")
|
||||
}
|
||||
|
||||
log.Debug("mutation 'copy': ", src, "-->", dest)
|
||||
|
||||
return copyMutation(p.Context, src, dest)
|
||||
},
|
||||
},
|
||||
@@ -127,6 +141,8 @@ func graphqlSchema() (graphql.Schema, error) {
|
||||
return nil, fmt.Errorf("Failed to parse args")
|
||||
}
|
||||
|
||||
log.Debug("mutation 'move': ", src, "-->", dest)
|
||||
|
||||
return moveMutation(p.Context, src, dest)
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user