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,43 +166,44 @@ 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 { if strings.HasPrefix(path, apiPath) {
case assertsPath + "echarts.min.js": view := path[len(apiPath):]
_, _ = ctx.WriteString(EchartJS) var values []interface{}
case assertsPath + "jquery.min.js": reportData := c.dataFunc()
_, _ = ctx.WriteString(JqueryJS) switch view {
case "/": case latencyView:
if reportData != nil {
values = append(values, reportData.Latency.min/1e6)
values = append(values, reportData.Latency.Mean()/1e6)
values = append(values, reportData.Latency.max/1e6)
} else {
values = append(values, nil, nil, nil)
}
case rpsView:
if reportData != nil {
values = append(values, reportData.RPS)
} else {
values = append(values, nil)
}
}
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
}
_ = json.NewEncoder(ctx).Encode(metrics)
} else if path == "/" {
ctx.SetContentType("text/html") ctx.SetContentType("text/html")
_ = c.page.Render(ctx) _ = c.page.Render(ctx)
default: } else if strings.HasPrefix(path, assetsPath) {
if strings.HasPrefix(path, apiPath) { ap := path[len(assetsPath):]
view := path[len(apiPath)+1:] f, err := assetsFS.Open(ap)
var values []interface{} if err != nil {
reportData := c.dataFunc() ctx.Error(err.Error(), 404)
switch view {
case latencyView:
if reportData != nil {
values = append(values, reportData.Latency.min/1e6)
values = append(values, reportData.Latency.Mean()/1e6)
values = append(values, reportData.Latency.max/1e6)
} else {
values = append(values, nil, nil, nil)
}
case rpsView:
if reportData != nil {
values = append(values, reportData.RPS)
} else {
values = append(values, nil)
}
}
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
}
_ = json.NewEncoder(ctx).Encode(metrics)
} else { } else {
ctx.Error("NotFound", fasthttp.StatusNotFound) ctx.SetBodyStream(f, -1)
} }
} else {
ctx.Error("NotFound", fasthttp.StatusNotFound)
} }
} }

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