feat: using golang1.16 embed (#40)

This commit is contained in:
jeessy2 2021-02-23 18:15:45 +08:00 committed by GitHub
parent 7aa76c73c0
commit c3bbb8dd98
12 changed files with 21 additions and 700 deletions

View File

@ -16,7 +16,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15
go-version: ^1.16
id: go
- name: Check out code into the Go module directory

View File

@ -1,5 +1,5 @@
# build stage
FROM golang:1.15 AS builder
FROM golang:1.16 AS builder
WORKDIR /app
COPY . .

View File

@ -17,22 +17,12 @@ build: $(DIR_SRC)/main.go
build_docker_image:
@$(DOCKER_CMD) build -f ./Dockerfile -t ddns-go:$(VERSION) .
init:
@go get -u github.com/go-bindata/go-bindata/...
test:
@$(GO) test ./...
test-race:
@$(GO) test -race ./...
bindata:
@go-bindata -pkg util -o util/staticPages.go static/pages/...
@go-bindata -pkg asserts -o asserts/html.go -fs -prefix "static/" static/
dev:
@go-bindata -debug -pkg util -o util/staticPages.go static/pages/...
# clean all build result
clean:
@$(GO) clean ./...

View File

@ -106,10 +106,6 @@
## 开发&自行编译
- 如果喜欢从源代码编译自己的版本,可以使用本项目提供的 Makefile 构建
- 开发:
- 首先使用 `make init` 安装 `bindata`
- 使用 `make dev` 动态加载修改后的 `writing.html`
- 编译:
- 如修改了html, 务必使用 `make bindata` 生成编译需要的静态文件
- 使用 `make build` 生成本地编译后的 `ddns-go` 可执行文件
- 使用 `make build_docker_image` 自行编译 Docker 镜像
- 开发环境 golang 1.16
- 使用 `make build` 生成本地编译后的 `ddns-go` 可执行文件
- 使用 `make build_docker_image` 自行编译 Docker 镜像

File diff suppressed because one or more lines are too long

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

2
go.mod
View File

@ -1,6 +1,6 @@
module ddns-go
go 1.15
go 1.16
require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.439

13
main.go
View File

@ -1,11 +1,11 @@
package main
import (
static "ddns-go/asserts"
"ddns-go/config"
"ddns-go/dns"
"ddns-go/util"
"ddns-go/web"
"embed"
"flag"
"fmt"
"log"
@ -15,13 +15,20 @@ import (
"time"
)
//go:embed static
var staticEmbededFiles embed.FS
//go:embed favicon.ico
var faviconEmbededFile embed.FS
func main() {
listen := flag.String("l", ":9876", "web server listen address")
every := flag.String("f", "300", "dns update frequency in second")
flag.Parse()
// 启动静态文件服务
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(static.AssetFile())))
http.Handle("/favicon.ico", http.StripPrefix("/", http.FileServer(static.AssetFile())))
http.Handle("/static/", http.FileServer(http.FS(staticEmbededFiles)))
http.Handle("/favicon.ico", http.FileServer(http.FS(faviconEmbededFile)))
http.HandleFunc("/", config.BasicAuth(web.Writing))
http.HandleFunc("/save", config.BasicAuth(web.Save))

File diff suppressed because one or more lines are too long

View File

@ -1,20 +0,0 @@
package util
import (
"io/ioutil"
"log"
"os"
"strings"
)
// GetStaticResourcePath 获得静态资源文件路径
func GetStaticResourcePath(orgPath string) (temPath string, err error) {
data, err := Asset(orgPath)
if err != nil {
log.Println("Asset was not found.")
return "", err
}
tempFile := os.TempDir() + string(os.PathSeparator) + strings.ReplaceAll(orgPath, "/", "_")
err = ioutil.WriteFile(tempFile, data, 0600)
return tempFile, err
}

View File

@ -2,8 +2,7 @@ package web
import (
"ddns-go/config"
"ddns-go/util"
"log"
"embed"
"strings"
"fmt"
@ -11,14 +10,12 @@ import (
"net/http"
)
//go:embed writing.html
var writingEmbedFile embed.FS
// Writing 填写信息
func Writing(writer http.ResponseWriter, request *http.Request) {
tempPath, err := util.GetStaticResourcePath("static/pages/writing.html")
if err != nil {
log.Println("Asset was not found.")
return
}
tmpl, err := template.ParseFiles(tempPath)
tmpl, err := template.ParseFS(writingEmbedFile, "writing.html")
if err != nil {
fmt.Println("Error happened..")
fmt.Println(err)