From 6d76a350056b77b26bde7286bae2820f80d27113 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 7 Jul 2022 16:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/static.go | 33 --------------------------------- main.go | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 33 insertions(+), 39 deletions(-) delete mode 100644 dist/static.go diff --git a/dist/static.go b/dist/static.go deleted file mode 100644 index 681f8dd..0000000 --- a/dist/static.go +++ /dev/null @@ -1,33 +0,0 @@ -package dist - -import ( - "embed" - "errors" - "io/fs" - "path/filepath" - "strings" -) - -type Dist struct { - embed.FS -} - -//go:embed css js favicon.ico index.html -var Static embed.FS - -func (r Dist) Open(name string) (fs.File, error) { - if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) { - return nil, errors.New("http: invalid character in file path") - } - fullName := strings.TrimLeft(name, "/") - prifix := strings.Split(fullName, ".") - l := len(prifix) - p := prifix[l-1] - if p == "js" || p == "css" { - fullName = p + "/" + fullName - } else if p == "map" { - fullName = "js/" + fullName - } - file, err := r.FS.Open(fullName) - return file, err -} diff --git a/main.go b/main.go index c26cfe2..4e80b32 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,17 @@ package main import ( + "embed" + "errors" "github.com/PuerkitoBio/goquery" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github/fthvgb1/newsfetch/dist" + "io/fs" "io/ioutil" "log" "net/http" "os/exec" + "path/filepath" "regexp" "runtime" "strconv" @@ -59,6 +62,32 @@ type message struct { Data interface{} } +type dist struct { + embed.FS + path string +} + +//go:embed dist/* +var st embed.FS + +func (r dist) Open(name string) (fs.File, error) { + if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) { + return nil, errors.New("http: invalid character in file path") + } + fullName := strings.TrimLeft(name, "/") + prifix := strings.Split(fullName, ".") + l := len(prifix) + p := prifix[l-1] + if p == "js" || p == "css" { + fullName = p + "/" + fullName + } else if p == "map" { + fullName = "js/" + fullName + } + fullName = r.path + "/" + fullName + file, err := r.FS.Open(fullName) + return file, err +} + func setMap[T mapT](obj *mapXS[T], key string, v T) { obj.Lock() (*obj.mapX)[key] = v @@ -267,14 +296,12 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) { func main() { h := newFetchHandler("https://www.baidu.com/s?rtt=1&bsst=1&cl=2&tn=news&rsv_dl=ns_pc&word=") router := gin.Default() - static := dist.Dist{ - FS: dist.Static, + static := dist{ + FS: st, + path: "dist", } - //router.Static("/css", "./dist/css") - //router.Static("/js", "./dist/js") router.StaticFS("/js", http.FS(static)) router.StaticFS("/css", http.FS(static)) - //router.StaticFile("/favicon.ico", "dist/favicon.ico") var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true