| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package custom_routers
- import (
- "fmt"
- "git.qianqiusoft.com/qianqiusoft/light-apiengine/engine"
- sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
- sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
- "github.com/gin-gonic/gin"
- "time"
- )
- var Domain_String_Cache map[string]string
- var Refresh_Cache_Time int64 = 0
- func Register(e *engine.ApiEngine) {
- ctrl := NewFrontendController(e)
- e.GinEngine.StaticFile("/", "web/index.html")
- e.GinEngine.StaticFile("/index.html", "web/index.html")
- //e.GinEngine.StaticFile("/favicon.ico", "web/favicon.ico")
- e.GinEngine.StaticFile("/config.js", "web/config.js")
- e.GinEngine.GET("/domains.js", ctrl.GetDomainsJs)
- e.GinEngine.GET("/favicon.ico", ctrl.GetDomainLogo)
- e.GinEngine.Static("/static", "web/static")
- Domain_String_Cache = make(map[string]string)
- go RefreshDomainString(e)
- }
- type FrontendController struct {
- apiengine *engine.ApiEngine
- }
- func NewFrontendController(e *engine.ApiEngine) *FrontendController {
- controller := &FrontendController{e}
- return controller
- }
- func (c *FrontendController) GetDomainsJs(ctx *gin.Context) {
- go RefreshDomainString(c.apiengine)
- hostnames := sysutils.GetHostnames(ctx)
- for i := range hostnames {
- hostname := hostnames[i]
- if v, ok := Domain_String_Cache[hostname]; ok {
- ctx.Data(200, "application/javascript", []byte(v))
- return
- }
- }
- ctx.Data(200, "application/javascript", []byte(`window.Domains = {}`))
- }
- func (c *FrontendController) GetDomainLogo(ctx *gin.Context) {
- go RefreshDomainString(c.apiengine)
- hostnames := sysutils.GetHostnames(ctx)
- for i := range hostnames{
- logoFile := "web/static/domain/" + hostnames[i] + "/images/sidebar_logo.png"
- if sysutils.Exists(logoFile){
- ctx.File(logoFile)
- return
- }
- }
- ctx.File("web/static/domain/default/images/sidebar_logo.png")
- }
- func RefreshDomainString(e *engine.ApiEngine) {
- if time.Now().UnixNano()/int64(time.Second) > 10 {
- var domain []sysmodel.SysDomain
- e.PlatformOrmEngine.SQL("select * from sys_domain where del_flag = 0").Find(&domain)
- for _, v := range domain {
- domain_js := "window.Domains = {"
- domain_js += fmt.Sprintf("'%s': {login: '%s', name: '%s', fullName: '%s', title: '%s', theme: '%s'},",
- v.DomainUrl, v.LoginUrl, v.Name, v.FullName, v.FullName, v.Theme)
- domain_js += "};"
- Domain_String_Cache[v.DomainUrl] = domain_js
- }
- Refresh_Cache_Time = time.Now().UnixNano() / int64(time.Second)
- }
- }
|