ft/adds docker-compose volumes prepares fileupload

- Files should be saved into subdirs
This commit is contained in:
itsscb 2023-10-12 00:30:50 +02:00
parent d7b4f66e38
commit 3d6d87854e
3 changed files with 37 additions and 3 deletions

View File

@ -7,6 +7,7 @@ RUN go build -o main main.go
# Run stage # Run stage
FROM alpine:3.18 FROM alpine:3.18
WORKDIR /app WORKDIR /app
RUN mkdir files
COPY --from=builder /app/main . COPY --from=builder /app/main .
COPY app.env . COPY app.env .
ENV DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable ENV DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable

View File

@ -8,8 +8,8 @@ services:
- POSTGRES_DB=df - POSTGRES_DB=df
ports: ports:
- "5432:5432" - "5432:5432"
# volumes: volumes:
# - ./db/migration:/docker-entrypoint-initdb.d - data-volume:/var/lib/postgresql/data
api: api:
build: build:
context: . context: .
@ -17,8 +17,13 @@ services:
ports: ports:
- "8080:8080" - "8080:8080"
- "9090:9090" - "9090:9090"
volumes:
- ./files:/app/files/:Z
environment: environment:
- DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable - DB_SOURCE=postgresql://root:secret@postgres:5432/df?sslmode=disable
depends_on: depends_on:
- postgres - postgres
command: [ "/app/main" ] command: [ "/app/main" ]
volumes:
data-volume:

View File

@ -11,6 +11,11 @@ import (
"github.com/gin-gonic/gin" "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) { func (server *Server) UploadDocument(ctx *gin.Context) {
authHeader := ctx.GetHeader("authorization") authHeader := ctx.GetHeader("authorization")
@ -35,13 +40,36 @@ func (server *Server) UploadDocument(ctx *gin.Context) {
return 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") file, err := ctx.FormFile("file")
if err != nil { if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(errors.New("could not parse file"))) ctx.JSON(http.StatusInternalServerError, errorResponse(errors.New("could not parse file")))
return 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 { if _, err := os.Stat(p); err != nil {
err = ctx.SaveUploadedFile(file, p) err = ctx.SaveUploadedFile(file, p)