go:embed assets in program

This commit is contained in:
six-ddc 2021-06-27 11:10:06 +08:00
parent 137c59bb64
commit 6e6c4523b3
4 changed files with 91 additions and 71 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"embed"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net" "net"
@ -12,6 +13,7 @@ import (
"text/template" "text/template"
"time" "time"
_ "embed"
cors "github.com/AdhityaRamadhanus/fasthttpcors" cors "github.com/AdhityaRamadhanus/fasthttpcors"
"github.com/go-echarts/go-echarts/v2/charts" "github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components" "github.com/go-echarts/go-echarts/v2/components"
@ -20,9 +22,13 @@ import (
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
//go:embed echarts.min.js
//go:embed jquery.min.js
var assetsFS embed.FS
var ( var (
assertsPath = "/echarts/statics/" assetsPath = "/echarts/statics/"
apiPath = "/data" apiPath = "/data/"
latencyView = "latency" latencyView = "latency"
rpsView = "rps" rpsView = "rps"
timeFormat = "15:04:05" timeFormat = "15:04:05"
@ -35,7 +41,7 @@ $(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); });
function {{ .ViewID }}_sync() { function {{ .ViewID }}_sync() {
$.ajax({ $.ajax({
type: "GET", type: "GET",
url: "{{ .APIPath }}/{{ .Route }}", url: "{{ .APIPath }}{{ .Route }}",
dataType: "json", dataType: "json",
success: function (result) { success: function (result) {
let opt = goecharts_{{ .ViewID }}.getOption(); let opt = goecharts_{{ .ViewID }}.getOption();
@ -151,7 +157,7 @@ func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Ch
c := &Charts{ln: ln, dataFunc: dataFunc} c := &Charts{ln: ln, dataFunc: dataFunc}
c.page = components.NewPage() c.page = components.NewPage()
c.page.PageTitle = "plow" c.page.PageTitle = "plow"
c.page.AssetsHost = assertsPath c.page.AssetsHost = assetsPath
c.page.Assets.JSAssets.Add("jquery.min.js") c.page.Assets.JSAssets.Add("jquery.min.js")
c.page.AddCharts(c.newLatencyView(), c.newRPSView()) c.page.AddCharts(c.newLatencyView(), c.newRPSView())
@ -160,17 +166,8 @@ func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Ch
func (c *Charts) Handler(ctx *fasthttp.RequestCtx) { func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path()) path := string(ctx.Path())
switch path {
case assertsPath + "echarts.min.js":
_, _ = ctx.WriteString(EchartJS)
case assertsPath + "jquery.min.js":
_, _ = ctx.WriteString(JqueryJS)
case "/":
ctx.SetContentType("text/html")
_ = c.page.Render(ctx)
default:
if strings.HasPrefix(path, apiPath) { if strings.HasPrefix(path, apiPath) {
view := path[len(apiPath)+1:] view := path[len(apiPath):]
var values []interface{} var values []interface{}
reportData := c.dataFunc() reportData := c.dataFunc()
switch view { switch view {
@ -194,11 +191,21 @@ func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
Values: values, Values: values,
} }
_ = json.NewEncoder(ctx).Encode(metrics) _ = json.NewEncoder(ctx).Encode(metrics)
} else if path == "/" {
ctx.SetContentType("text/html")
_ = c.page.Render(ctx)
} else if strings.HasPrefix(path, assetsPath) {
ap := path[len(assetsPath):]
f, err := assetsFS.Open(ap)
if err != nil {
ctx.Error(err.Error(), 404)
} else {
ctx.SetBodyStream(f, -1)
}
} else { } else {
ctx.Error("NotFound", fasthttp.StatusNotFound) ctx.Error("NotFound", fasthttp.StatusNotFound)
} }
} }
}
func (c *Charts) Serve(open bool) { func (c *Charts) Serve(open bool) {
server := fasthttp.Server{ server := fasthttp.Server{

45
echarts.min.js vendored Normal file

File diff suppressed because one or more lines are too long

2
jquery.min.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long