(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc))
| 176 | } |
| 177 | |
| 178 | func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) { |
| 179 | utils.Log.Debug("Setting up static routes...") |
| 180 | siteConfig := getSiteConfig() |
| 181 | initStatic() |
| 182 | initIndex(siteConfig) |
| 183 | folders := []string{"assets", "images", "streamer", "static"} |
| 184 | |
| 185 | if conf.Conf.Cdn == "" { |
| 186 | utils.Log.Debug("Setting up static file serving...") |
| 187 | r.Use(func(c *gin.Context) { |
| 188 | for _, folder := range folders { |
| 189 | if strings.HasPrefix(c.Request.RequestURI, fmt.Sprintf("/%s/", folder)) { |
| 190 | c.Header("Cache-Control", "public, max-age=15552000") |
| 191 | } |
| 192 | } |
| 193 | }) |
| 194 | for _, folder := range folders { |
| 195 | sub, err := fs.Sub(static, folder) |
| 196 | if err != nil { |
| 197 | utils.Log.Fatalf("can't find folder: %s", folder) |
| 198 | } |
| 199 | utils.Log.Debugf("Setting up route for folder: %s", folder) |
| 200 | r.StaticFS(fmt.Sprintf("/%s/", folder), http.FS(sub)) |
| 201 | } |
| 202 | } else { |
| 203 | // Ensure static file redirected to CDN |
| 204 | for _, folder := range folders { |
| 205 | r.GET(fmt.Sprintf("/%s/*filepath", folder), func(c *gin.Context) { |
| 206 | filepath := c.Param("filepath") |
| 207 | c.Redirect(http.StatusFound, fmt.Sprintf("%s/%s%s", siteConfig.Cdn, folder, filepath)) |
| 208 | }) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | utils.Log.Debug("Setting up catch-all route...") |
| 213 | noRoute(func(c *gin.Context) { |
| 214 | if c.Request.Method != "GET" && c.Request.Method != "POST" { |
| 215 | c.Status(405) |
| 216 | return |
| 217 | } |
| 218 | c.Header("Content-Type", "text/html") |
| 219 | c.Status(200) |
| 220 | if strings.HasPrefix(c.Request.URL.Path, "/@manage") { |
| 221 | _, _ = c.Writer.WriteString(conf.ManageHtml) |
| 222 | } else { |
| 223 | _, _ = c.Writer.WriteString(conf.IndexHtml) |
| 224 | } |
| 225 | c.Writer.Flush() |
| 226 | c.Writer.WriteHeaderNow() |
| 227 | }) |
| 228 | } |
no test coverage detected