Browse Source

conf add AdminBasePath. serve ui

pull/395/head
Goxiaoy 4 years ago
parent
commit
c74438a754
  1. 1
      admin/src/utils/request.ts
  2. 1
      conf.sample.yml
  3. 1
      dtmsvr/config/config.go
  4. 26
      main.go

1
admin/src/utils/request.ts

@ -1,7 +1,6 @@
import axios from 'axios'
const request = axios.create({
baseURL: window.basePath || '',
timeout: 60000
})

1
conf.sample.yml

@ -63,6 +63,7 @@
### advanced options
# UpdateBranchAsyncGoroutineNum: 1 # num of async goroutine to update branch status
# TimeZoneOffset: '' #default '' using system default. '+8': Asia/Shanghai; '0': GMT
# AdminBasePath: '' #default '' set admin access base path
# ConfigUpdateInterval: 10 # the interval to update configuration in memory such as topics map... (seconds)
# TimeZoneOffset: '' # default '' using system default. '+8': Asia/Shanghai; '0': GMT

1
dtmsvr/config/config.go

@ -101,6 +101,7 @@ type Type struct {
ConfigUpdateInterval int64 `yaml:"ConfigUpdateInterval" default:"3"`
AlertRetryLimit int64 `yaml:"AlertRetryLimit" default:"3"`
AlertWebHook string `yaml:"AlertWebHook"`
AdminBasePath string `yaml:"AdminBasePath"`
}
// Config config

26
main.go

@ -58,23 +58,35 @@ func addAdmin(app *gin.Engine, conf *config.Type) {
// for testing users, proxy admin to target because the build output has not been embed
dist := getSub(admin, "admin", "dist")
index, err := dist.Open("index.html")
var router gin.IRoutes = app
if len(conf.AdminBasePath) > 0 {
router = app.Group(conf.AdminBasePath)
}
if err == nil {
cont, err := ioutil.ReadAll(index)
logger.FatalIfError(err)
_ = index.Close()
sfile := string(cont)
//replace base path
sfile = strings.Replace(sfile, "\"assets/", "\""+conf.AdminBasePath+"/assets/", -1)
sfile = strings.Replace(sfile, "PUBLIC-PATH-VARIABLE", conf.AdminBasePath, -1)
renderIndex := func(c *gin.Context) {
c.Header("content-type", "text/html;charset=utf-8")
c.String(200, sfile)
}
app.StaticFS("/assets", http.FS(getSub(dist, "assets")))
app.GET("/admin/*name", renderIndex)
app.GET("/", renderIndex)
router.StaticFS("/assets", http.FS(getSub(dist, "assets")))
router.GET("/admin/*name", renderIndex)
router.GET("/", renderIndex)
router.GET("/favicon.ico", func(ctx *gin.Context) {
http.StripPrefix(conf.AdminBasePath, http.FileServer(http.FS(dist))).ServeHTTP(ctx.Writer, ctx.Request)
})
logger.Infof("admin is served from dir 'admin/dist/'")
} else {
app.GET("/", proxyAdmin)
app.GET("/assets/*name", proxyAdmin)
app.GET("/admin/*name", proxyAdmin)
router.GET("/", proxyAdmin)
router.GET("/assets/*name", proxyAdmin)
router.GET("/admin/*name", proxyAdmin)
lang := os.Getenv("LANG")
if strings.HasPrefix(lang, "zh_CN") {
target = "cn-admin.dtm.pub"
@ -83,7 +95,7 @@ func addAdmin(app *gin.Engine, conf *config.Type) {
}
logger.Infof("admin is proxied to %s", target)
}
logger.Infof("admin is running at: http://localhost:%d", conf.HTTPPort)
logger.Infof("admin is running at: http://localhost:%d%s", conf.HTTPPort, conf.AdminBasePath)
}
func proxyAdmin(c *gin.Context) {

Loading…
Cancel
Save