Skip to content

Commit

Permalink
embed index.html
Browse files Browse the repository at this point in the history
add Dockerfile
  • Loading branch information
grj1046 committed Apr 1, 2021
1 parent a3a9db1 commit a066fef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.16.2-alpine3.13

ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct

WORKDIR /hotspot
COPY . .

RUN go get -d -v ./...
RUN go install -v ./...

EXPOSE 80
CMD ["hotspot-online"]
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#### 02.处理并返回数据 (可通过环境变量`HOTSPOT_HTTP_PORT`设置端口,默认值`80`)
接口 `/hotspot` 会将本地json文件读取并按照需求返回为json格式接口
返回格式如下:
```json

```
[{
FileName: '',
Content: [{
Expand All @@ -29,4 +30,17 @@
```

#### 03.前端展示
前端采用Bootstrap4 来展示,用`jquery.getJSON`从远程接口获取数据,来渲染页面。
前端采用Bootstrap4 来展示,用`jquery.getJSON`从远程接口获取数据,来渲染页面。

### 04.添加对Docker的支持
使用 golang alpine 生成的镜像文件也不小,我记得有一个在同一个Dockerfile中指定编译镜像和部署镜像的写法,找不到那种写法了。
可以使用scratch镜像指定一个空的文件夹

项目中的Dockerfile生成的镜像有点大 366MB

如果用Scratch作镜像,大小只有7MB,需要研究一下


### 其它有用无用的东西
https://golang.org/pkg/embed/
https://harsimranmaan.medium.com/embedding-static-files-in-a-go-binary-using-go-embed-bac505f3cb9a
20 changes: 13 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
Expand All @@ -18,6 +19,9 @@ import (
"golang.org/x/text/encoding/simplifiedchinese"
)

//go:embed index.html
var embedFile embed.FS

const jsonPath = "json"

type JsonModel struct {
Expand Down Expand Up @@ -147,13 +151,13 @@ func parse_zhihu_rb() {
fileName := "zhihu.json"
byteBody, err := getHttpBody(weburl)
if err != nil {
log.Println("getHttpBody failed", err)
log.Println("getHttpBody failed: ", err)
return
}
result := make([]JsonModel, 0)
var zhihuhot ZhihuHot
if err := json.Unmarshal(byteBody, &zhihuhot); err != nil {
log.Println("Unmarshal failed", err)
log.Println("Unmarshal failed: ", err)
return
}
for _, item := range zhihuhot.Data {
Expand All @@ -170,15 +174,15 @@ func parse_zhihu_rb() {
}
err = WriteFile(result, fileName)
if err != nil {
log.Println("WriteFile failed ", err)
log.Println("WriteFile failed: ", err)
return
}
}

func parse_website(model HotSiteModel) {
doc, err := getGoquryDocument(model.WebURL)
if err != nil {
log.Println("getGoquryDocument failed", err)
log.Println("getGoquryDocument failed: ", err)
return
}
result := make([]JsonModel, 0)
Expand All @@ -200,7 +204,7 @@ func parse_website(model HotSiteModel) {
})
err = WriteFile(result, model.FileName)
if err != nil {
log.Println("WriteFile failed ", err)
log.Println("WriteFile failed: ", err)
return
}
}
Expand Down Expand Up @@ -240,7 +244,7 @@ func GetHotspot() {

/************** Http **************/
func handlerHome(w http.ResponseWriter, r *http.Request) {
byteHtml, err := ioutil.ReadFile("index.html")
byteHtml, err := embedFile.ReadFile("index.html")
if err != nil {
fmt.Fprintf(w, "get index.html failed. "+err.Error())
return
Expand Down Expand Up @@ -292,6 +296,8 @@ func main() {
http.HandleFunc("/", handlerHome)
err = http.ListenAndServe(httpPort, nil)
if err != nil {
log.Fatal("ListenAndServe failed", err)
log.Fatal("ListenAndServe failed: ", err)
return
}
fmt.Println("ListenAndServe: ", httpPort)
}

0 comments on commit a066fef

Please sign in to comment.