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

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

@ -3,7 +3,7 @@
<a-layout-header class="header flex">
<div class="flex items-center logo h-16">
<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>
<a-menu
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> = [
{
{
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<IMenubarList> = [
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<IMenubarList> = [
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' },
}
]
}
]

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"`
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) {

2
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")
}

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.")
// 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
}

48
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

6
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()

Loading…
Cancel
Save