MCPcopy Index your code
hub / github.com/apache/answer / Register

Method Register

internal/router/ui.go:73–168  ·  view source on GitHub ↗

Register a new static resource which generated by ui directory

(r *gin.Engine, baseURLPath string)

Source from the content-addressed store, hash-verified

71
72// Register a new static resource which generated by ui directory
73func (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

Callers 4

RegisterFuncsMethod · 0.45
NewHTTPServerFunction · 0.45
initFunction · 0.45

Calls 4

GetPicByUrlFunction · 0.92
GetManifestJsonMethod · 0.80
GetStaticPrefixMethod · 0.80
GetSiteBrandingMethod · 0.65

Tested by 1