feat: echo(get,set file)
This commit is contained in:
parent
4ae56d1726
commit
c8ea9ec9f4
@ -21,14 +21,14 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: system
|
||||
image: loveuer/echo_app:v0.0.1
|
||||
image: loveuer/echo_app:v24.04.05-2
|
||||
imagePullPolicy: IfNotPresent
|
||||
command: ["/app/echo_app"]
|
||||
ports:
|
||||
- containerPort: 80
|
||||
resources:
|
||||
limits:
|
||||
memory: 60Mi
|
||||
memory: 50Mi
|
||||
cpu: 1
|
||||
volumeMounts:
|
||||
- mountPath: /data
|
||||
@ -45,7 +45,7 @@ metadata:
|
||||
namespace: echo-app
|
||||
name: echo-pvc
|
||||
spec:
|
||||
accessModes: ["ReadWriteMany"]
|
||||
accessModes: ["ReadWriteOnce"]
|
||||
storageClassName: "echo-storage"
|
||||
resources:
|
||||
requests:
|
||||
@ -60,14 +60,12 @@ metadata:
|
||||
spec:
|
||||
capacity:
|
||||
storage: 1Gi
|
||||
volumeMode: Filesystem # Filesystem(文件系统) Block(块)
|
||||
accessModes:
|
||||
- ReadWriteMany
|
||||
# persistentVolumeReclaimPolicy: Delete
|
||||
- ReadWriteOnce
|
||||
persistentVolumeReclaimPolicy: Retain
|
||||
hostPath:
|
||||
path: /data/echo-app
|
||||
type: ''
|
||||
type: ""
|
||||
storageClassName: "echo-storage"
|
||||
volumeMode: Filesystem
|
||||
---
|
||||
|
@ -1,4 +1,4 @@
|
||||
FROM golang:1.20.14-alpine3.19 AS builder
|
||||
FROM golang:latest AS builder
|
||||
|
||||
WORKDIR /app/build
|
||||
|
||||
@ -7,6 +7,8 @@ COPY go.sum .
|
||||
COPY service/echo/main.go .
|
||||
|
||||
ENV GOPROXY https://goproxy.io
|
||||
ENV GOOS linux
|
||||
ENV GOARCH amd64
|
||||
|
||||
RUN go mod download && go build -ldflags='-s -w' -o echo_app .
|
||||
|
||||
|
@ -3,7 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
@ -37,6 +39,51 @@ func main() {
|
||||
return c.Next()
|
||||
})
|
||||
|
||||
app.Get("/echo/file", func(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Filename string `query:"filename"`
|
||||
Download bool `query:"download"`
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
req = new(Req)
|
||||
file *os.File
|
||||
bs []byte
|
||||
)
|
||||
|
||||
if err = c.QueryParser(req); err != nil {
|
||||
return resp.Resp400(c, err.Error())
|
||||
}
|
||||
|
||||
if req.Filename == "" {
|
||||
return resp.Resp400(c, req, "empty file name")
|
||||
}
|
||||
|
||||
filename := path.Join(root, path.Base(req.Filename))
|
||||
|
||||
if file, err = os.Open(filename); err != nil {
|
||||
return resp.Resp400(c, nf.Map{"filepath": filename, "err": err.Error()})
|
||||
}
|
||||
|
||||
if bs, err = io.ReadAll(file); err != nil {
|
||||
return resp.Resp400(c, nf.Map{"filepath": filename, "err": err.Error()})
|
||||
}
|
||||
|
||||
ct := ""
|
||||
if len(bs) > 512 {
|
||||
ct = http.DetectContentType(bs[:512])
|
||||
} else {
|
||||
ct = http.DetectContentType(bs)
|
||||
}
|
||||
|
||||
c.SetHeader("Content-Type", ct)
|
||||
|
||||
_, err = c.Write(bs)
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
app.Post("/echo/file", func(c *nf.Ctx) error {
|
||||
type Req struct {
|
||||
Filename string
|
||||
|
Loading…
x
Reference in New Issue
Block a user