Browse Source

embed init ok

pull/288/head
yedf2 4 years ago
parent
commit
fa6c4c2737
  1. 1
      dashboard/.env.development
  2. 4
      dashboard/src/api/api_dtm.ts
  3. 2
      dashboard/src/layout/components/header.vue
  4. 38
      dashboard/src/router/index.ts
  5. 4
      dtmsvr/config/config.go
  6. 2
      dtmsvr/config/config_utils.go
  7. 8
      dtmsvr/entry/main.go
  8. 48
      main.go
  9. 6
      test/api_test.go

1
dashboard/.env.development

@ -1,3 +1,2 @@
VITE_APP_API_BASE_URL="/api"
VITE_PROXY=[["/api", "http://localhost:36789"]] VITE_PROXY=[["/api", "http://localhost:36789"]]
VITE_DASHBOARD_VERSION="v0.0.0-dev" VITE_DASHBOARD_VERSION="v0.0.0-dev"

4
dashboard/src/api/api_dtm.ts

@ -8,7 +8,7 @@ export interface IListAllTransactionsReq {
export function listAllTransactions<T>(payload: IListAllTransactionsReq): Promise<AxiosResponse<T>> { export function listAllTransactions<T>(payload: IListAllTransactionsReq): Promise<AxiosResponse<T>> {
return request({ return request({
url: '/dtmsvr/all', url: '/api/dtmsvr/all',
method: 'get', method: 'get',
params: payload params: payload
}) })
@ -16,7 +16,7 @@ export function listAllTransactions<T>(payload: IListAllTransactionsReq): Promis
export function getDtmVersion(): Promise<AxiosResponse<any>> { export function getDtmVersion(): Promise<AxiosResponse<any>> {
return request({ return request({
url: '/dtmsvr/version', url: '/api/dtmsvr/version',
method: 'get', method: 'get',
}) })
} }

2
dashboard/src/layout/components/header.vue

@ -3,7 +3,7 @@
<a-layout-header class="header flex"> <a-layout-header class="header flex">
<div class="flex items-center logo h-16"> <div class="flex items-center logo h-16">
<svg-icon style="width: 36px; height: 36px; margin-right: 84px;" icon-class="svg-logo" /> <svg-icon style="width: 36px; height: 36px; margin-right: 84px;" icon-class="svg-logo" />
<span class="text-gray-400 text-lg">DTM dashbaord {{ version }}</span> <span class="text-gray-400 text-lg">DTM dashboard {{ version }}</span>
</div> </div>
<a-menu <a-menu
v-model:selectedKeys="activeMenu" v-model:selectedKeys="activeMenu"

38
dashboard/src/router/index.ts

@ -8,10 +8,10 @@ const Components: IObject<() => Promise<typeof import('*.vue')>> = Object.assign
}) })
export const allowRouter: Array<IMenubarList> = [ export const allowRouter: Array<IMenubarList> = [
{ {
name: 'Dashboard', name: 'Dashboard',
path: '/', path: '/',
redirect: '/dashboard/nodes/living', redirect: '/dashboard/global-transactions/all',
component: Components['LayoutHeader'], component: Components['LayoutHeader'],
meta: { title: 'Dashboard', activeMenu: '/dashboard' }, meta: { title: 'Dashboard', activeMenu: '/dashboard' },
children: [ children: [
@ -21,12 +21,12 @@ export const allowRouter: Array<IMenubarList> = [
component: Components['LayoutMain'], component: Components['LayoutMain'],
meta: { title: 'Nodes' }, meta: { title: 'Nodes' },
children: [ children: [
{ {
name: 'LivingNodes', name: 'LivingNodes',
path: '/dashboard/nodes/living', path: '/dashboard/nodes/living',
component: Components['LivingNodes'], component: Components['LivingNodes'],
meta: { title: 'Living Nodes' }, meta: { title: 'Living Nodes' },
} }
] ]
}, { }, {
name: 'GlobalTransactions', name: 'GlobalTransactions',
@ -34,17 +34,17 @@ export const allowRouter: Array<IMenubarList> = [
component: Components['LayoutMain'], component: Components['LayoutMain'],
meta: { title: 'Global Transactions' }, meta: { title: 'Global Transactions' },
children: [ children: [
{ {
name: 'AllTransactions', name: 'AllTransactions',
path: '/dashboard/global-transactions/all', path: '/dashboard/global-transactions/all',
component: Components['AllTransactions'], component: Components['AllTransactions'],
meta: { title: 'All Transactions' }, meta: { title: 'All Transactions' },
}, { }, {
name: 'UnfinishedTransactions', name: 'UnfinishedTransactions',
path: '/dashboard/global-transactions/unfinished', path: '/dashboard/global-transactions/unfinished',
component: Components['UnfinishedTransactions'], component: Components['UnfinishedTransactions'],
meta: { title: 'Unfinished Transactions' }, meta: { title: 'Unfinished Transactions' },
} }
] ]
} }
] ]

4
dtmsvr/config/config.go

@ -79,7 +79,7 @@ func (s *Store) GetDBConf() dtmcli.DBConf {
} }
} }
type configType struct { type ConfigType struct {
Store Store `yaml:"Store"` Store Store `yaml:"Store"`
TransCronInterval int64 `yaml:"TransCronInterval" default:"3"` TransCronInterval int64 `yaml:"TransCronInterval" default:"3"`
TimeoutToFail int64 `yaml:"TimeoutToFail" default:"35"` TimeoutToFail int64 `yaml:"TimeoutToFail" default:"35"`
@ -97,7 +97,7 @@ type configType struct {
} }
// Config config // Config config
var Config = configType{} var Config = ConfigType{}
// MustLoadConfig load config from env and file // MustLoadConfig load config from env and file
func MustLoadConfig(confFile string) { func MustLoadConfig(confFile string) {

2
dtmsvr/config/config_utils.go

@ -58,7 +58,7 @@ func toUnderscoreUpper(key string) string {
return strings.ToUpper(s2) return strings.ToUpper(s2)
} }
func checkConfig(conf *configType) error { func checkConfig(conf *ConfigType) error {
if conf.RetryInterval < 10 { if conf.RetryInterval < 10 {
return errors.New("RetryInterval should not be less than 10") return errors.New("RetryInterval should not be less than 10")
} }

8
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.") var confFile = flag.String("c", "", "Path to the server configuration file.")
// Main is the entry point of dtm server. // Main is the entry point of dtm server.
func Main(version *string) *gin.Engine { func Main(version *string) (*gin.Engine, *config.ConfigType) {
flag.Parse() flag.Parse()
if *version == "" { if *version == "" {
*version = "v0.0.0-dev" *version = "v0.0.0-dev"
@ -36,10 +36,10 @@ func Main(version *string) *gin.Engine {
dtmsvr.Version = *version dtmsvr.Version = *version
if flag.NArg() > 0 || *isHelp { if flag.NArg() > 0 || *isHelp {
usage() usage()
return nil return nil, nil
} else if *isVersion { } else if *isVersion {
fmt.Printf("dtm version: %s\n", *version) fmt.Printf("dtm version: %s\n", *version)
return nil return nil, nil
} }
logger.Infof("dtm version is: %s", *version) logger.Infof("dtm version is: %s", *version)
config.MustLoadConfig(*confFile) config.MustLoadConfig(*confFile)
@ -55,5 +55,5 @@ func Main(version *string) *gin.Engine {
registry.WaitStoreUp() registry.WaitStoreUp()
app := dtmsvr.StartSvr() // start dtmsvr api app := dtmsvr.StartSvr() // start dtmsvr api
go dtmsvr.CronExpiredTrans(-1) // start dtmsvr cron job go dtmsvr.CronExpiredTrans(-1) // start dtmsvr cron job
return app return app, &config.Config
} }

48
main.go

@ -7,12 +7,15 @@
package main package main
import ( import (
"embed"
"fmt" "fmt"
"io/fs"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"github.com/dtm-labs/dtm/dtmcli/logger" "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/entry"
_ "github.com/dtm-labs/dtm/dtmsvr/microservices" _ "github.com/dtm-labs/dtm/dtmsvr/microservices"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -22,23 +25,50 @@ import (
var Version string var Version string
func main() { func main() {
app := entry.Main(&Version) app, conf := entry.Main(&Version)
if app != nil { if app != nil {
addDashboard(app) addDashboard(app, conf)
select {} select {}
} }
} }
func addDashboard(app *gin.Engine) {
app.GET("/dashboard/*name", proxyDashboard) //go:embed dashboard/dist
app.GET("/@vite/*name", proxyDashboard) var dashboard embed.FS
app.GET("/node_modules/*name", proxyDashboard)
app.GET("/src/*name", proxyDashboard) //go:embed dashboard/dist/index.html
app.GET("/@id/*name", proxyDashboard) 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) { func proxyDashboard(c *gin.Context) {
target := "127.0.0.1:5000"
u := &url.URL{} u := &url.URL{}
u.Scheme = "http" u.Scheme = "http"
u.Host = target u.Host = target

6
test/api_test.go

@ -18,6 +18,12 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestAPIQuery(t *testing.T) {
gid := dtmimp.GetFuncName() gid := dtmimp.GetFuncName()
err := genMsg(gid).Submit() err := genMsg(gid).Submit()

Loading…
Cancel
Save