diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..569e6d2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode +.idea +.DS_Store \ No newline at end of file diff --git a/deployment/echo.yaml b/deployment/echo.yaml new file mode 100644 index 0000000..e9abb6b --- /dev/null +++ b/deployment/echo.yaml @@ -0,0 +1,113 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: echo-app + +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + namespace: echo-app + name: echo +spec: + replicas: 1 + selector: + matchLabels: + app: echo + template: + metadata: + labels: + app: echo + spec: + containers: + - name: system + image: loveuer/echo_app:v0.0.1 + imagePullPolicy: IfNotPresent + command: ["/app/echo_app"] + ports: + - containerPort: 80 + resources: + limits: + memory: 60Mi + cpu: 1 + volumeMounts: + - mountPath: /data + name: echo-data + volumes: + - name: echo-data + persistentVolumeClaim: + claimName: echo-pvc + +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + namespace: echo-app + name: echo-pvc +spec: + accessModes: ["ReadWriteMany"] + storageClassName: "echo-storage" + resources: + requests: + storage: 1Gi + +--- +apiVersion: v1 +kind: PersistentVolume +metadata: + namespace: echo-app + name: echo-pv +spec: + capacity: + storage: 1Gi + volumeMode: Filesystem # Filesystem(文件系统) Block(块) + accessModes: + - ReadWriteMany + # persistentVolumeReclaimPolicy: Delete + persistentVolumeReclaimPolicy: Retain + hostPath: + path: /data/echo-app + type: '' + storageClassName: "echo-storage" + volumeMode: Filesystem +--- +apiVersion: v1 +kind: Service +metadata: + namespace: echo-app + name: echo + # annotations: + # metallb.universe.tf/loadBalancerIPs: 10.220.10.48 +spec: + selector: + app: echo + # type: LoadBalancer + type: ClusterIP + ports: + - name: echo-http + port: 80 + targetPort: 80 + # nodePort: 31997 + +--- +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ing-echo + namespace: echo-app + annotations: + nginx.ingress.kubernetes.io/ssl-redirect: "false" + nginx.ingress.kubernetes.io/use-regex: "true" +spec: + ingressClassName: nginx + rules: + - host: "echo.zyp.dev.com" + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: echo + port: + number: 80 diff --git a/deployment/start-rancher.sh b/deployment/start-rancher.sh index 8c9d7a4..2ec9fe7 100755 --- a/deployment/start-rancher.sh +++ b/deployment/start-rancher.sh @@ -7,6 +7,7 @@ docker run \ --restart=always \ -e SSL_CERT_DIR=/apps/ssl \ --add-host raw.githubusercontent.com:185.199.109.133 \ + -v ./data:/data \ -p 20443:443 \ -p 80:30080 \ -p 443:30443 \ diff --git a/service/echo/Dockerfile b/service/echo/Dockerfile new file mode 100644 index 0000000..7420c7d --- /dev/null +++ b/service/echo/Dockerfile @@ -0,0 +1,23 @@ +FROM golang:1.20.14-alpine3.19 AS builder + +WORKDIR /app/build + +COPY go.mod . +COPY go.sum . +COPY service/echo/main.go . + +ENV GOPROXY https://goproxy.io + +RUN go mod download && go build -ldflags='-s -w' -o echo_app . + +FROM alpine + +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && apk add curl + +ENV TZ Asia/Shanghai + +WORKDIR /app + +COPY --from=builder /app/build/echo_app . + +CMD [ "/app/echo_app" ] \ No newline at end of file diff --git a/service/echo/main.go b/service/echo/main.go new file mode 100644 index 0000000..9fb4bed --- /dev/null +++ b/service/echo/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "flag" + "fmt" + "log" + "os" + "path" + "time" + + "github.com/loveuer/nf" + "github.com/loveuer/nf/nft/resp" +) + +var ( + addr string + hostname string + root string +) + +func init() { + flag.StringVar(&addr, "addr", ":80", "") + flag.StringVar(&root, "root", "/data", "save file root dir") + flag.Parse() + + hostname = os.Getenv("HOSTNAME") + if hostname == "" { + hostname = fmt.Sprintf("unknown-%d", time.Now().UnixNano()) + } +} + +func main() { + app := nf.New() + + app.Use(func(c *nf.Ctx) error { + c.SetHeader("U-HOSTNAME", hostname) + return c.Next() + }) + + app.Post("/echo/file", func(c *nf.Ctx) error { + type Req struct { + Filename string + Content string + } + + var ( + err error + req = new(Req) + file *os.File + ) + + if err = c.BodyParser(req); err != nil { + return resp.Resp400(c, err.Error()) + } + + filename := path.Join(root, path.Base(req.Filename)) + + if file, err = os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644); err != nil { + return resp.Resp400(c, err.Error()) + } + + defer file.Close() + + if _, err = file.WriteString(req.Content); err != nil { + return resp.Resp500(c, err.Error()) + } + + return resp.Resp200(c, nf.Map{"filename": filename}) + }) + + log.Fatal(app.Run(addr)) +} diff --git a/Dockerfile b/service/hello/Dockerfile similarity index 93% rename from Dockerfile rename to service/hello/Dockerfile index 97235a1..262c092 100644 --- a/Dockerfile +++ b/service/hello/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /app/build COPY go.mod . COPY go.sum . -COPY main.go . +COPY service/hello/main.go . ENV GOPROXY https://goproxy.io diff --git a/main.go b/service/hello/main.go similarity index 100% rename from main.go rename to service/hello/main.go