s3browser-backend/internal/httpServer.go

95 lines
2.4 KiB
Go
Raw Normal View History

2021-08-03 21:10:23 +00:00
package s3browser
import (
"context"
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
2021-08-06 12:13:08 +00:00
"github.com/graph-gophers/dataloader"
2021-08-03 21:10:23 +00:00
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
"github.com/minio/minio-go/v7"
)
// initHttp setup and start the http server. Blocking
func initHttp(schema graphql.Schema, resolveContext context.Context) {
h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: false,
Playground: true,
})
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" {
2021-08-06 12:13:08 +00:00
httpGetFile(resolveContext, rw, r)
2021-08-03 21:10:23 +00:00
return
}
if r.Method == "POST" {
2021-08-06 12:13:08 +00:00
httpPostFile(resolveContext, rw, r)
2021-08-03 21:10:23 +00:00
return
}
})
http.ListenAndServe(":8080", nil)
}
2021-08-06 12:13:08 +00:00
func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
s3Client := ctx.Value("s3Client").(*minio.Client)
2021-08-03 21:10:23 +00:00
id := r.URL.Query().Get("id")
objInfo, err := s3Client.StatObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
if err != nil {
rw.WriteHeader(500)
return
}
reqEtag := r.Header.Get("If-None-Match")
if reqEtag == objInfo.ETag {
rw.WriteHeader(304)
return
}
obj, err := s3Client.GetObject(context.Background(), bucketName, id, minio.GetObjectOptions{})
if err != nil {
rw.WriteHeader(500)
return
}
rw.Header().Set("Cache-Control", "must-revalidate")
rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filepath.Base((objInfo.Key))))
rw.Header().Set("Content-Type", objInfo.ContentType)
rw.Header().Set("ETag", objInfo.ETag)
io.Copy(rw, obj)
}
2021-08-06 12:13:08 +00:00
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)
2021-08-03 21:10:23 +00:00
2021-08-06 12:13:08 +00:00
id := r.URL.Query().Get("id")
2021-08-06 11:49:00 +00:00
2021-08-03 21:10:23 +00:00
contentType := r.Header.Get("Content-Type")
2021-08-06 11:49:00 +00:00
mimeType, _, _ := mime.ParseMediaType(contentType)
2021-08-03 21:10:23 +00:00
2021-08-06 12:13:08 +00:00
s3Client.PutObject(context.Background(), bucketName, id, r.Body, r.ContentLength, minio.PutObjectOptions{
2021-08-06 11:49:00 +00:00
ContentType: mimeType,
})
2021-08-03 21:10:23 +00:00
2021-08-06 12:13:08 +00:00
// 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)))
2021-08-03 21:10:23 +00:00
}