Register a new static resource which generated by ui directory
(r *gin.Engine, baseURLPath string)
| 71 | |
| 72 | // Register a new static resource which generated by ui directory |
| 73 | func (a *UIRouter) Register(r *gin.Engine, baseURLPath string) { |
| 74 | staticPath := os.Getenv("ANSWER_STATIC_PATH") |
| 75 | |
| 76 | // if ANSWER_STATIC_PATH is set and not empty, ignore embed resource |
| 77 | if staticPath != "" { |
| 78 | info, err := os.Stat(staticPath) |
| 79 | |
| 80 | if err != nil || !info.IsDir() { |
| 81 | log.Error(err) |
| 82 | } else { |
| 83 | log.Debugf("registering static path %s", staticPath) |
| 84 | |
| 85 | r.LoadHTMLGlob(staticPath + "/*.html") |
| 86 | r.Static(baseURLPath+"/static", staticPath+"/static") |
| 87 | r.NoRoute(func(c *gin.Context) { |
| 88 | c.HTML(http.StatusOK, "index.html", gin.H{}) |
| 89 | }) |
| 90 | |
| 91 | // return immediately if the static path is set |
| 92 | return |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // handle the static file by default ui static files |
| 97 | r.StaticFS(baseURLPath+"/static", http.FS(&_resource{ |
| 98 | fs: ui.Build, |
| 99 | })) |
| 100 | |
| 101 | // specify the not router for default routes and redirect |
| 102 | r.NoRoute(func(c *gin.Context) { |
| 103 | urlPath := c.Request.URL.Path |
| 104 | if len(baseURLPath) > 0 { |
| 105 | urlPath = strings.TrimPrefix(urlPath, baseURLPath) |
| 106 | } |
| 107 | filePath := "" |
| 108 | switch urlPath { |
| 109 | case "/favicon.ico": |
| 110 | branding, err := a.siteInfoService.GetSiteBranding(c) |
| 111 | if err != nil { |
| 112 | log.Error(err) |
| 113 | } |
| 114 | if branding != nil && branding.Favicon != "" { |
| 115 | c.String(http.StatusOK, htmltext.GetPicByUrl(branding.Favicon)) |
| 116 | return |
| 117 | } else if branding != nil && branding.SquareIcon != "" { |
| 118 | c.String(http.StatusOK, htmltext.GetPicByUrl(branding.SquareIcon)) |
| 119 | return |
| 120 | } else { |
| 121 | c.Header("content-type", "image/vnd.microsoft.icon") |
| 122 | filePath = UIRootFilePath + urlPath |
| 123 | } |
| 124 | case "/manifest.json": |
| 125 | a.siteInfoController.GetManifestJson(c) |
| 126 | return |
| 127 | case "/install": |
| 128 | // if answer is running by run command user can not access install page. |
| 129 | c.Redirect(http.StatusFound, "/") |
| 130 | return |