frontend_router.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "github.com/gin-gonic/gin"
  7. "strings"
  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. req_domain := ctx.Request.Host
  34. n := strings.Index(req_domain, ":")
  35. if n > 0 {
  36. req_domain = req_domain[0:n]
  37. }
  38. ctx.Data(200, "application/javascript", []byte(Domain_String_Cache[req_domain]))
  39. }
  40. func (c *FrontendController) GetDomainLogo(ctx *gin.Context) {
  41. go RefreshDomainString(c.apiengine)
  42. req_domain := ctx.Request.Host
  43. n := strings.Index(req_domain, ":")
  44. if n > 0 {
  45. req_domain = req_domain[0:n]
  46. }
  47. ctx.File("static/domain/" + req_domain + "images/sidebar_logo.png")
  48. }
  49. func RefreshDomainString(e *engine.ApiEngine) {
  50. if time.Now().UnixNano()/int64(time.Second) > 10 {
  51. var domain []sysmodel.SysDomain
  52. e.PlatformOrmEngine.SQL("select * from sys_domain where del_flag = 0").Find(&domain)
  53. for _, v := range domain {
  54. domain_js := "window.Domains = {"
  55. domain_js += fmt.Sprintf("'%s': {login: '%s', name: '%s', fullName: '%s', title: '%s', theme: '%s'},",
  56. v.DomainUrl, v.LoginUrl, v.Name, v.FullName, v.FullName, v.Theme)
  57. domain_js += "};"
  58. Domain_String_Cache[v.DomainUrl] = domain_js
  59. }
  60. Refresh_Cache_Time = time.Now().UnixNano() / int64(time.Second)
  61. }
  62. }