frontend_router.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package custom_routers
  2. import (
  3. "fmt"
  4. "git.qianqiusoft.com/qianqiusoft/light-apiengine/engine"
  5. sysmodel "git.qianqiusoft.com/qianqiusoft/light-apiengine/models"
  6. sysutils "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  7. "github.com/gin-gonic/gin"
  8. "time"
  9. )
  10. var Domain_String_Cache map[string]string
  11. var Refresh_Cache_Time int64 = 0
  12. func Register(e *engine.ApiEngine) {
  13. ctrl := NewFrontendController(e)
  14. e.GinEngine.StaticFile("/", "web/index.html")
  15. e.GinEngine.StaticFile("/index.html", "web/index.html")
  16. //e.GinEngine.StaticFile("/favicon.ico", "web/favicon.ico")
  17. e.GinEngine.StaticFile("/config.js", "web/config.js")
  18. e.GinEngine.GET("/domains.js", ctrl.GetDomainsJs)
  19. e.GinEngine.GET("/favicon.ico", ctrl.GetDomainLogo)
  20. e.GinEngine.Static("/static", "web/static")
  21. Domain_String_Cache = make(map[string]string)
  22. go RefreshDomainString(e)
  23. }
  24. type FrontendController struct {
  25. apiengine *engine.ApiEngine
  26. }
  27. func NewFrontendController(e *engine.ApiEngine) *FrontendController {
  28. controller := &FrontendController{e}
  29. return controller
  30. }
  31. func (c *FrontendController) GetDomainsJs(ctx *gin.Context) {
  32. go RefreshDomainString(c.apiengine)
  33. hostnames := sysutils.GetHostnames(ctx)
  34. for i := range hostnames {
  35. hostname := hostnames[i]
  36. if v, ok := Domain_String_Cache[hostname]; ok {
  37. ctx.Data(200, "application/javascript", []byte(v))
  38. return
  39. }
  40. }
  41. ctx.Data(200, "application/javascript", []byte(`window.Domains = {}`))
  42. }
  43. func (c *FrontendController) GetDomainLogo(ctx *gin.Context) {
  44. go RefreshDomainString(c.apiengine)
  45. hostnames := sysutils.GetHostnames(ctx)
  46. for i := range hostnames{
  47. logoFile := "web/static/domain/" + hostnames[i] + "/images/sidebar_logo.png"
  48. if sysutils.Exists(logoFile){
  49. ctx.File(logoFile)
  50. return
  51. }
  52. }
  53. ctx.File("web/static/domain/default/images/sidebar_logo.png")
  54. }
  55. func RefreshDomainString(e *engine.ApiEngine) {
  56. if time.Now().UnixNano()/int64(time.Second) > 10 {
  57. var domain []sysmodel.SysDomain
  58. e.PlatformOrmEngine.SQL("select * from sys_domain where del_flag = 0").Find(&domain)
  59. for _, v := range domain {
  60. domain_js := "window.Domains = {"
  61. domain_js += fmt.Sprintf("'%s': {login: '%s', name: '%s', fullName: '%s', title: '%s', theme: '%s'},",
  62. v.DomainUrl, v.LoginUrl, v.Name, v.FullName, v.FullName, v.Theme)
  63. domain_js += "};"
  64. Domain_String_Cache[v.DomainUrl] = domain_js
  65. }
  66. Refresh_Cache_Time = time.Now().UnixNano() / int64(time.Second)
  67. }
  68. }