ETag!
This commit is contained in:
parent
2a2895228f
commit
272055df38
1 changed files with 22 additions and 0 deletions
22
main.go
22
main.go
|
@ -7,6 +7,8 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
@ -58,10 +60,30 @@ func main() {
|
|||
}))
|
||||
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
|
||||
|
||||
// Generate the deployment time when the application starts
|
||||
deploymentTime := fmt.Sprintf("%d", time.Now().UnixNano())
|
||||
|
||||
// Static server
|
||||
fs := http.FS(PublicFS)
|
||||
e.GET("/public/*", func(c echo.Context) error {
|
||||
// Generate an ETag based on the deployment time
|
||||
eTag := fmt.Sprintf(`W/"%s"`, deploymentTime)
|
||||
|
||||
// Set the ETag header
|
||||
c.Response().Header().Set("ETag", eTag)
|
||||
|
||||
// Set the Cache-Control header
|
||||
c.Response().Header().Set("Cache-Control", "public, max-age=3600")
|
||||
|
||||
// Check the file extension and set the Content-Type header accordingly
|
||||
ext := filepath.Ext(c.Param("*"))
|
||||
switch ext {
|
||||
case ".css":
|
||||
c.Response().Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||
case ".js":
|
||||
c.Response().Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||
}
|
||||
|
||||
return echo.WrapHandler(http.FileServer(fs))(c)
|
||||
})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue