Browse Source

Add cache

Florian Hoss 2 years ago
parent
commit
52f16c9966
4 changed files with 17 additions and 13 deletions
  1. 7 0
      server/middlewares.go
  2. 8 10
      server/router.go
  3. 2 1
      templates/_base.gohtml
  4. 0 2
      templates/robots.txt

+ 7 - 0
server/middlewares.go

@@ -1,9 +1,16 @@
 package server
 package server
 
 
 import (
 import (
+	"context"
+	"github.com/cloudwego/hertz/pkg/app"
 	"github.com/hertz-contrib/gzip"
 	"github.com/hertz-contrib/gzip"
 )
 )
 
 
+func CacheMiddleware() app.HandlerFunc {
+	return func(ctx context.Context, c *app.RequestContext) {
+		c.Header("Cache-Control", "public, max-age=604800, immutable")
+	}
+}
 func (server *Server) setupMiddlewares() {
 func (server *Server) setupMiddlewares() {
 	server.Router.Use(gzip.Gzip(gzip.DefaultCompression))
 	server.Router.Use(gzip.Gzip(gzip.DefaultCompression))
 }
 }

+ 8 - 10
server/router.go

@@ -10,20 +10,18 @@ func (server *Server) setupRouter() {
 	server.Router.GET("/", server.goDash)
 	server.Router.GET("/", server.goDash)
 	server.Router.GET("/ws", webSocket)
 	server.Router.GET("/ws", webSocket)
 
 
-	server.serveStatic("static")
-	server.serveStatic("storage/icons")
-
-	server.Router.GET("/robots.txt", func(c context.Context, ctx *app.RequestContext) {
-		ctx.File(TemplatesFolder + "/robots.txt")
-	})
-	server.Router.GET("/favicon.ico", func(c context.Context, ctx *app.RequestContext) {
-		ctx.File("static/favicon/favicon.ico")
-	})
-
 	server.Router.NoMethod(func(c context.Context, ctx *app.RequestContext) {
 	server.Router.NoMethod(func(c context.Context, ctx *app.RequestContext) {
 		ctx.Redirect(consts.StatusPermanentRedirect, []byte("/"))
 		ctx.Redirect(consts.StatusPermanentRedirect, []byte("/"))
 	})
 	})
 	server.Router.NoRoute(func(c context.Context, ctx *app.RequestContext) {
 	server.Router.NoRoute(func(c context.Context, ctx *app.RequestContext) {
 		ctx.Redirect(consts.StatusPermanentRedirect, []byte("/"))
 		ctx.Redirect(consts.StatusPermanentRedirect, []byte("/"))
 	})
 	})
+
+	server.Router.Use(CacheMiddleware())
+	server.serveStatic("static")
+	server.serveStatic("storage/icons")
+
+	server.Router.GET("/robots.txt", func(c context.Context, ctx *app.RequestContext) {
+		ctx.String(consts.StatusOK, "User-agent: *\nDisallow: /")
+	})
 }
 }

+ 2 - 1
templates/_base.gohtml

@@ -7,9 +7,10 @@
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <meta name="description" content="A blazing fast start-page for services written in Go " />
       <meta name="description" content="A blazing fast start-page for services written in Go " />
 
 
-      <link rel="apple-touch-icon" sizes="180x180" href="/static/favicon/apple-touch-icon.png" />
+      <link rel="icon" type="image/x-icon" href="/static/favicon/favicon.ico" />
       <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon/favicon-32x32.png" />
       <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon/favicon-32x32.png" />
       <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon/favicon-16x16.png" />
       <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon/favicon-16x16.png" />
+      <link rel="apple-touch-icon" sizes="180x180" href="/static/favicon/apple-touch-icon.png" />
       <link rel="manifest" href="/static/favicon/site.webmanifest" />
       <link rel="manifest" href="/static/favicon/site.webmanifest" />
       <style>
       <style>
         .bookmark-link:hover .img {
         .bookmark-link:hover .img {

+ 0 - 2
templates/robots.txt

@@ -1,2 +0,0 @@
-User-agent: *
-Disallow: /