feat: echo(get,set file)

This commit is contained in:
loveuer 2024-04-05 12:47:51 +08:00
parent 4ae56d1726
commit c8ea9ec9f4
3 changed files with 55 additions and 8 deletions

View File

@ -21,14 +21,14 @@ spec:
spec: spec:
containers: containers:
- name: system - name: system
image: loveuer/echo_app:v0.0.1 image: loveuer/echo_app:v24.04.05-2
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
command: ["/app/echo_app"] command: ["/app/echo_app"]
ports: ports:
- containerPort: 80 - containerPort: 80
resources: resources:
limits: limits:
memory: 60Mi memory: 50Mi
cpu: 1 cpu: 1
volumeMounts: volumeMounts:
- mountPath: /data - mountPath: /data
@ -45,7 +45,7 @@ metadata:
namespace: echo-app namespace: echo-app
name: echo-pvc name: echo-pvc
spec: spec:
accessModes: ["ReadWriteMany"] accessModes: ["ReadWriteOnce"]
storageClassName: "echo-storage" storageClassName: "echo-storage"
resources: resources:
requests: requests:
@ -60,14 +60,12 @@ metadata:
spec: spec:
capacity: capacity:
storage: 1Gi storage: 1Gi
volumeMode: Filesystem # Filesystem文件系统 Block
accessModes: accessModes:
- ReadWriteMany - ReadWriteOnce
# persistentVolumeReclaimPolicy: Delete
persistentVolumeReclaimPolicy: Retain persistentVolumeReclaimPolicy: Retain
hostPath: hostPath:
path: /data/echo-app path: /data/echo-app
type: '' type: ""
storageClassName: "echo-storage" storageClassName: "echo-storage"
volumeMode: Filesystem volumeMode: Filesystem
--- ---

View File

@ -1,4 +1,4 @@
FROM golang:1.20.14-alpine3.19 AS builder FROM golang:latest AS builder
WORKDIR /app/build WORKDIR /app/build
@ -7,6 +7,8 @@ COPY go.sum .
COPY service/echo/main.go . COPY service/echo/main.go .
ENV GOPROXY https://goproxy.io ENV GOPROXY https://goproxy.io
ENV GOOS linux
ENV GOARCH amd64
RUN go mod download && go build -ldflags='-s -w' -o echo_app . RUN go mod download && go build -ldflags='-s -w' -o echo_app .

View File

@ -3,7 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io"
"log" "log"
"net/http"
"os" "os"
"path" "path"
"time" "time"
@ -37,6 +39,51 @@ func main() {
return c.Next() 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 { app.Post("/echo/file", func(c *nf.Ctx) error {
type Req struct { type Req struct {
Filename string Filename string