Skip to content

Commit

Permalink
go:embed assets in program
Browse files Browse the repository at this point in the history
  • Loading branch information
six-ddc committed Jun 27, 2021
1 parent 137c59b commit 6e6c452
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 71 deletions.
81 changes: 44 additions & 37 deletions charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"embed"
"encoding/json"
"fmt"
"net"
Expand All @@ -12,6 +13,7 @@ import (
"text/template"
"time"

_ "embed"
cors "github.com/AdhityaRamadhanus/fasthttpcors"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/components"
Expand All @@ -20,9 +22,13 @@ import (
"github.com/valyala/fasthttp"
)

//go:embed echarts.min.js
//go:embed jquery.min.js
var assetsFS embed.FS

var (
assertsPath = "/echarts/statics/"
apiPath = "/data"
assetsPath = "/echarts/statics/"
apiPath = "/data/"
latencyView = "latency"
rpsView = "rps"
timeFormat = "15:04:05"
Expand All @@ -35,7 +41,7 @@ $(function () { setInterval({{ .ViewID }}_sync, {{ .Interval }}); });
function {{ .ViewID }}_sync() {
$.ajax({
type: "GET",
url: "{{ .APIPath }}/{{ .Route }}",
url: "{{ .APIPath }}{{ .Route }}",
dataType: "json",
success: function (result) {
let opt = goecharts_{{ .ViewID }}.getOption();
Expand Down Expand Up @@ -151,7 +157,7 @@ func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Ch
c := &Charts{ln: ln, dataFunc: dataFunc}
c.page = components.NewPage()
c.page.PageTitle = "plow"
c.page.AssetsHost = assertsPath
c.page.AssetsHost = assetsPath
c.page.Assets.JSAssets.Add("jquery.min.js")
c.page.AddCharts(c.newLatencyView(), c.newRPSView())

Expand All @@ -160,43 +166,44 @@ func NewCharts(ln net.Listener, dataFunc func() *ChartsReport, desc string) (*Ch

func (c *Charts) Handler(ctx *fasthttp.RequestCtx) {
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) {
view := path[len(apiPath)+1:]
var values []interface{}
reportData := c.dataFunc()
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)
}
if strings.HasPrefix(path, apiPath) {
view := path[len(apiPath):]
var values []interface{}
reportData := c.dataFunc()
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)
}
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
case rpsView:
if reportData != nil {
values = append(values, reportData.RPS)
} else {
values = append(values, nil)
}
_ = json.NewEncoder(ctx).Encode(metrics)
}
metrics := &Metrics{
Time: time.Now().Format(timeFormat),
Values: values,
}
_ = 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.Error("NotFound", fasthttp.StatusNotFound)
ctx.SetBodyStream(f, -1)
}
} else {
ctx.Error("NotFound", fasthttp.StatusNotFound)
}
}

Expand Down
45 changes: 45 additions & 0 deletions echarts.min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions jquery.min.js

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions statics.go

This file was deleted.

0 comments on commit 6e6c452

Please sign in to comment.