From fa6c4c2737f61635c092eadd40869dabc942e857 Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Sun, 15 May 2022 18:32:28 +0800 Subject: [PATCH] embed init ok --- dashboard/.env.development | 1 - dashboard/src/api/api_dtm.ts | 4 +- dashboard/src/layout/components/header.vue | 2 +- dashboard/src/router/index.ts | 38 ++++++++--------- dtmsvr/config/config.go | 4 +- dtmsvr/config/config_utils.go | 2 +- dtmsvr/entry/main.go | 8 ++-- main.go | 48 ++++++++++++++++++---- test/api_test.go | 6 +++ 9 files changed, 74 insertions(+), 39 deletions(-) diff --git a/dashboard/.env.development b/dashboard/.env.development index 4360528..50507c4 100644 --- a/dashboard/.env.development +++ b/dashboard/.env.development @@ -1,3 +1,2 @@ -VITE_APP_API_BASE_URL="/api" VITE_PROXY=[["/api", "http://localhost:36789"]] VITE_DASHBOARD_VERSION="v0.0.0-dev" diff --git a/dashboard/src/api/api_dtm.ts b/dashboard/src/api/api_dtm.ts index e232a4b..b0b52d6 100644 --- a/dashboard/src/api/api_dtm.ts +++ b/dashboard/src/api/api_dtm.ts @@ -8,7 +8,7 @@ export interface IListAllTransactionsReq { export function listAllTransactions(payload: IListAllTransactionsReq): Promise> { return request({ - url: '/dtmsvr/all', + url: '/api/dtmsvr/all', method: 'get', params: payload }) @@ -16,7 +16,7 @@ export function listAllTransactions(payload: IListAllTransactionsReq): Promis export function getDtmVersion(): Promise> { return request({ - url: '/dtmsvr/version', + url: '/api/dtmsvr/version', method: 'get', }) } diff --git a/dashboard/src/layout/components/header.vue b/dashboard/src/layout/components/header.vue index 38723e7..28b2148 100644 --- a/dashboard/src/layout/components/header.vue +++ b/dashboard/src/layout/components/header.vue @@ -3,7 +3,7 @@ Promise> = Object.assign }) export const allowRouter: Array = [ - { + { name: 'Dashboard', path: '/', - redirect: '/dashboard/nodes/living', + redirect: '/dashboard/global-transactions/all', component: Components['LayoutHeader'], meta: { title: 'Dashboard', activeMenu: '/dashboard' }, children: [ @@ -21,12 +21,12 @@ export const allowRouter: Array = [ component: Components['LayoutMain'], meta: { title: 'Nodes' }, children: [ - { - name: 'LivingNodes', - path: '/dashboard/nodes/living', - component: Components['LivingNodes'], - meta: { title: 'Living Nodes' }, - } + { + name: 'LivingNodes', + path: '/dashboard/nodes/living', + component: Components['LivingNodes'], + meta: { title: 'Living Nodes' }, + } ] }, { name: 'GlobalTransactions', @@ -34,17 +34,17 @@ export const allowRouter: Array = [ component: Components['LayoutMain'], meta: { title: 'Global Transactions' }, children: [ - { - name: 'AllTransactions', - path: '/dashboard/global-transactions/all', - component: Components['AllTransactions'], - meta: { title: 'All Transactions' }, - }, { - name: 'UnfinishedTransactions', - path: '/dashboard/global-transactions/unfinished', - component: Components['UnfinishedTransactions'], - meta: { title: 'Unfinished Transactions' }, - } + { + name: 'AllTransactions', + path: '/dashboard/global-transactions/all', + component: Components['AllTransactions'], + meta: { title: 'All Transactions' }, + }, { + name: 'UnfinishedTransactions', + path: '/dashboard/global-transactions/unfinished', + component: Components['UnfinishedTransactions'], + meta: { title: 'Unfinished Transactions' }, + } ] } ] diff --git a/dtmsvr/config/config.go b/dtmsvr/config/config.go index 0cb3aca..3c47e0d 100644 --- a/dtmsvr/config/config.go +++ b/dtmsvr/config/config.go @@ -79,7 +79,7 @@ func (s *Store) GetDBConf() dtmcli.DBConf { } } -type configType struct { +type ConfigType struct { Store Store `yaml:"Store"` TransCronInterval int64 `yaml:"TransCronInterval" default:"3"` TimeoutToFail int64 `yaml:"TimeoutToFail" default:"35"` @@ -97,7 +97,7 @@ type configType struct { } // Config config -var Config = configType{} +var Config = ConfigType{} // MustLoadConfig load config from env and file func MustLoadConfig(confFile string) { diff --git a/dtmsvr/config/config_utils.go b/dtmsvr/config/config_utils.go index f588eee..b27b684 100644 --- a/dtmsvr/config/config_utils.go +++ b/dtmsvr/config/config_utils.go @@ -58,7 +58,7 @@ func toUnderscoreUpper(key string) string { return strings.ToUpper(s2) } -func checkConfig(conf *configType) error { +func checkConfig(conf *ConfigType) error { if conf.RetryInterval < 10 { return errors.New("RetryInterval should not be less than 10") } diff --git a/dtmsvr/entry/main.go b/dtmsvr/entry/main.go index 9592879..e7b5c0e 100644 --- a/dtmsvr/entry/main.go +++ b/dtmsvr/entry/main.go @@ -28,7 +28,7 @@ var isReset = flag.Bool("r", false, "Reset dtm server data.") var confFile = flag.String("c", "", "Path to the server configuration file.") // Main is the entry point of dtm server. -func Main(version *string) *gin.Engine { +func Main(version *string) (*gin.Engine, *config.ConfigType) { flag.Parse() if *version == "" { *version = "v0.0.0-dev" @@ -36,10 +36,10 @@ func Main(version *string) *gin.Engine { dtmsvr.Version = *version if flag.NArg() > 0 || *isHelp { usage() - return nil + return nil, nil } else if *isVersion { fmt.Printf("dtm version: %s\n", *version) - return nil + return nil, nil } logger.Infof("dtm version is: %s", *version) config.MustLoadConfig(*confFile) @@ -55,5 +55,5 @@ func Main(version *string) *gin.Engine { registry.WaitStoreUp() app := dtmsvr.StartSvr() // start dtmsvr api go dtmsvr.CronExpiredTrans(-1) // start dtmsvr cron job - return app + return app, &config.Config } diff --git a/main.go b/main.go index 8b8a134..1be5ba1 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,15 @@ package main import ( + "embed" "fmt" + "io/fs" "net/http" "net/http/httputil" "net/url" "github.com/dtm-labs/dtm/dtmcli/logger" + "github.com/dtm-labs/dtm/dtmsvr/config" "github.com/dtm-labs/dtm/dtmsvr/entry" _ "github.com/dtm-labs/dtm/dtmsvr/microservices" "github.com/gin-gonic/gin" @@ -22,23 +25,50 @@ import ( var Version string func main() { - app := entry.Main(&Version) + app, conf := entry.Main(&Version) if app != nil { - addDashboard(app) + addDashboard(app, conf) select {} } } -func addDashboard(app *gin.Engine) { - app.GET("/dashboard/*name", proxyDashboard) - app.GET("/@vite/*name", proxyDashboard) - app.GET("/node_modules/*name", proxyDashboard) - app.GET("/src/*name", proxyDashboard) - app.GET("/@id/*name", proxyDashboard) + +//go:embed dashboard/dist +var dashboard embed.FS + +//go:embed dashboard/dist/index.html +var indexFile string + +var target = "127.0.0.1:5000" + +func getSub(f1 fs.FS, sub string) fs.FS { + f2, err := fs.Sub(f1, sub) + logger.FatalIfError(err) + return f2 +} +func addDashboard(app *gin.Engine, conf *config.ConfigType) { + dist := getSub(dashboard, "dashboard/dist") + _, err := dist.Open("index.html") + if err == nil { + app.StaticFS("/assets", http.FS(getSub(dist, "assets"))) + app.GET("/dashboard/*name", func(c *gin.Context) { + c.Header("content-type", "text/html;charset=utf-8") + c.String(200, indexFile) + }) + logger.Infof("dashboard is served from dir 'dashboard/dist/'") + } else { + app.GET("/", proxyDashboard) + app.GET("/dashboard/*name", proxyDashboard) + app.GET("/@vite/*name", proxyDashboard) + app.GET("/node_modules/*name", proxyDashboard) + app.GET("/src/*name", proxyDashboard) + app.GET("/@id/*name", proxyDashboard) + logger.Infof("dashboard is proxied to %s", target) + } + logger.Infof("Dashboard is running at: http://localhost:%d", conf.HTTPPort) } func proxyDashboard(c *gin.Context) { - target := "127.0.0.1:5000" u := &url.URL{} u.Scheme = "http" u.Host = target diff --git a/test/api_test.go b/test/api_test.go index ff3d5d6..ca195b4 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -18,6 +18,12 @@ import ( "github.com/stretchr/testify/assert" ) +func TestAPIVersion(t *testing.T) { + resp, err := dtmimp.RestyClient.R().Get(dtmutil.DefaultHTTPServer + "/query") + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode()) +} + func TestAPIQuery(t *testing.T) { gid := dtmimp.GetFuncName() err := genMsg(gid).Submit()