diff --git a/internal/httpServer.go b/internal/httpServer.go index 33d9ca3..0637376 100644 --- a/internal/httpServer.go +++ b/internal/httpServer.go @@ -8,6 +8,7 @@ import ( "net/http" "path/filepath" + "github.com/graph-gophers/dataloader" "github.com/graphql-go/graphql" "github.com/graphql-go/handler" "github.com/minio/minio-go/v7" @@ -22,20 +23,18 @@ func initHttp(schema graphql.Schema, resolveContext context.Context) { Playground: true, }) - s3Client := resolveContext.Value("s3Client").(*minio.Client) - http.HandleFunc("/graphql", func(rw http.ResponseWriter, r *http.Request) { h.ContextHandler(resolveContext, rw, r) }) http.HandleFunc("/api/file", func(rw http.ResponseWriter, r *http.Request) { if r.Method == "GET" { - httpGetFile(rw, r, s3Client) + httpGetFile(resolveContext, rw, r) return } if r.Method == "POST" { - httpPostFile(rw, r, s3Client) + httpPostFile(resolveContext, rw, r) return } }) @@ -43,7 +42,8 @@ func initHttp(schema graphql.Schema, resolveContext context.Context) { http.ListenAndServe(":8080", nil) } -func httpGetFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client) { +func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { + s3Client := ctx.Value("s3Client").(*minio.Client) id := r.URL.Query().Get("id") objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{}) @@ -73,15 +73,22 @@ func httpGetFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client io.Copy(rw, obj) } -func httpPostFile(rw http.ResponseWriter, r *http.Request, s3Client *minio.Client) { +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) - filename := r.URL.Query().Get("id") + id := r.URL.Query().Get("id") contentType := r.Header.Get("Content-Type") mimeType, _, _ := mime.ParseMediaType(contentType) - s3Client.PutObject(context.Background(), bucketName, filename, r.Body, r.ContentLength, minio.PutObjectOptions{ + s3Client.PutObject(context.Background(), bucketName, id, r.Body, r.ContentLength, minio.PutObjectOptions{ ContentType: mimeType, }) + // Invalidate cache + loader["getFile"].Clear(ctx, dataloader.StringKey(id)) + loader["listObjects"].Clear(ctx, dataloader.StringKey(id)) + loader["getFiles"].Clear(ctx, dataloader.StringKey(filepath.Dir(id))) + }