| 37 | } |
| 38 | |
| 39 | func (m *UIMiddleware) Middleware() gin.HandlerFunc { |
| 40 | return func(c *gin.Context) { |
| 41 | path := strings.TrimPrefix(c.Request.URL.Path, "/") |
| 42 | |
| 43 | tlog.App.Debug().Str("path", path).Msg("path") |
| 44 | |
| 45 | switch strings.SplitN(path, "/", 2)[0] { |
| 46 | case "api", "resources", ".well-known": |
| 47 | c.Next() |
| 48 | return |
| 49 | case "robots.txt": |
| 50 | c.Writer.Header().Set("Content-Type", "text/plain") |
| 51 | c.Writer.WriteHeader(http.StatusOK) |
| 52 | c.Writer.Write([]byte("User-agent: *\nDisallow: /\n")) |
| 53 | return |
| 54 | default: |
| 55 | _, err := fs.Stat(m.uiFs, path) |
| 56 | |
| 57 | // Enough for one authentication flow |
| 58 | maxAge := 15 * time.Minute |
| 59 | |
| 60 | if os.IsNotExist(err) { |
| 61 | c.Request.URL.Path = "/" |
| 62 | } else if strings.HasPrefix(path, "assets/") { |
| 63 | // assets are named with a hash and can be cached for a long time |
| 64 | maxAge = 30 * 24 * time.Hour |
| 65 | } |
| 66 | |
| 67 | c.Writer.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int(maxAge.Seconds()))) |
| 68 | m.uiFileServer.ServeHTTP(c.Writer, c.Request) |
| 69 | c.Abort() |
| 70 | return |
| 71 | } |
| 72 | } |
| 73 | } |