From 7df01251dfd55a8de4ae2fd7eb2f6b182f7d82b8 Mon Sep 17 00:00:00 2001 From: Lyric-c Date: Mon, 7 Aug 2023 13:38:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=9D=99=E6=80=81=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=B5=8C=E5=85=A5=E5=88=B0=E5=8F=AF=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E6=96=87=E4=BB=B6=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 390df0b..f1b9dfb 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "embed" "encoding/json" "fmt" "io/ioutil" @@ -25,6 +26,13 @@ var ( rssUrls Config upgrader = websocket.Upgrader{} connMu sync.Mutex // 定义互斥锁 + + //go:embed static + dirStatic embed.FS + //go:embed index.html + fileIndex embed.FS + + htmlContent []byte ) func init() { @@ -38,6 +46,11 @@ func init() { if err != nil { panic(err) } + // 读取 index.html 内容 + htmlContent, err = fileIndex.ReadFile("index.html") + if err != nil { + panic(err) + } initDB() } @@ -58,13 +71,14 @@ func main() { http.HandleFunc("/", serveHome) //加载静态文件 - fs := http.FileServer(http.Dir("static")) - http.Handle("/static/", http.StripPrefix("/static/", fs)) + fs := http.FileServer(http.FS(dirStatic)) + http.Handle("/static/", fs) log.Fatal(http.ListenAndServe(":8080", nil)) } func serveHome(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "index.html") + w.Header().Add("Content-Type", "text/html; charset=utf-8") + w.Write(htmlContent) } func wsHandler(w http.ResponseWriter, r *http.Request) { From 3f809e933746d150379bf4ee4e984bac21bddcb8 Mon Sep 17 00:00:00 2001 From: Lyric-c Date: Mon, 7 Aug 2023 14:33:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index bc9b798..3a3eb1c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,13 +3,15 @@ FROM golang:1.20.4-alpine3.18 AS builder COPY . /src WORKDIR /src +#国内服务器可以取消以下注释 +#RUN go env -w GO111MODULE=on && \ +# go env -w GOPROXY=https://goproxy.cn,direct + RUN go build -ldflags "-s -w" -o ./bin/ . FROM alpine COPY --from=builder /src/bin /app -COPY --from=builder /src/index.html /app/index.html -COPY --from=builder /src/static /app/static COPY --from=builder /src/config.json /app/config.json WORKDIR /app From 3fd1030b63ed0fb8135da7ae707de964eac43c5f Mon Sep 17 00:00:00 2001 From: Lyric-c Date: Mon, 7 Aug 2023 14:33:51 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 3e743d1..cf65485 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,12 @@ git clone https://github.com/srcrs/rss-reader docker-compose up -d ``` +国内服务器将Dockerfile中取消下面注释使用 go mod 镜像 +```dockerfile +#RUN go env -w GO111MODULE=on && \ +# go env -w GOPROXY=https://goproxy.cn,direct +``` + 部署成功后,通过ip+端口号访问 # nginx反代