From 3d6d87854ed88808e9ff9524ed8e082dc4a2f7e9 Mon Sep 17 00:00:00 2001 From: itsscb Date: Thu, 12 Oct 2023 00:30:50 +0200 Subject: [PATCH] ft/adds docker-compose volumes prepares fileupload - Files should be saved into subdirs --- bff/Dockerfile | 1 + bff/docker-compose.yaml | 9 +++++++-- bff/gw/document.go | 30 +++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/bff/Dockerfile b/bff/Dockerfile index c5c01b3..618139f 100644 --- a/bff/Dockerfile +++ b/bff/Dockerfile @@ -7,6 +7,7 @@ RUN go build -o main main.go # Run stage FROM alpine:3.18 WORKDIR /app +RUN mkdir files COPY --from=builder /app/main . COPY app.env . ENV DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable diff --git a/bff/docker-compose.yaml b/bff/docker-compose.yaml index 9a09f69..9f6b7a2 100644 --- a/bff/docker-compose.yaml +++ b/bff/docker-compose.yaml @@ -8,8 +8,8 @@ services: - POSTGRES_DB=df ports: - "5432:5432" - # volumes: - # - ./db/migration:/docker-entrypoint-initdb.d + volumes: + - data-volume:/var/lib/postgresql/data api: build: context: . @@ -17,8 +17,13 @@ services: ports: - "8080:8080" - "9090:9090" + volumes: + - ./files:/app/files/:Z environment: - DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable depends_on: - postgres command: [ "/app/main" ] + +volumes: + data-volume: \ No newline at end of file diff --git a/bff/gw/document.go b/bff/gw/document.go index ef04f7b..0b9697d 100644 --- a/bff/gw/document.go +++ b/bff/gw/document.go @@ -11,6 +11,11 @@ import ( "github.com/gin-gonic/gin" ) +// type uploadDocumentRequest struct { +// PersonID uint64 `json:"person_id"` +// MailID uint64 `json:"mail_id"` +// } + func (server *Server) UploadDocument(ctx *gin.Context) { authHeader := ctx.GetHeader("authorization") @@ -35,13 +40,36 @@ func (server *Server) UploadDocument(ctx *gin.Context) { return } + // TODO: FileUpload with POST-Values + // bodyData, _ := io.ReadAll(ctx.Request.Body) + // slog.Info("Document", slog.String("body", fmt.Sprintf("%#v", string(bodyData)))) + file, err := ctx.FormFile("file") if err != nil { ctx.JSON(http.StatusInternalServerError, errorResponse(errors.New("could not parse file"))) return } - p := filepath.Join("./files", fmt.Sprintf("%d", account.ID), file.Filename) + targetDir := filepath.Join("./files", fmt.Sprintf("%d", account.ID)) + + // var req *uploadDocumentRequest + // _ = ctx.ShouldBindJSON(&req) + + // if req != nil { + // if req.MailID <= 0 && req.PersonID <= 0 { + // ctx.JSON(http.StatusBadRequest, errorResponse(errors.New("document can't be assigned to both person_id AND mail_id"))) + // return + // } + + // if req.MailID > 0 { + // targetDir = filepath.Join(targetDir, "mail", fmt.Sprintf("%d", req.MailID)) + // } + // if req.PersonID > 0 { + // targetDir = filepath.Join(targetDir, "person", fmt.Sprintf("%d", req.PersonID)) + // } + // } + + p := filepath.Join(targetDir, file.Filename) if _, err := os.Stat(p); err != nil { err = ctx.SaveUploadedFile(file, p)