Update tailwind.config.js and .air.toml, remove stylegen/package.json and modify base.css and main.go
This commit is contained in:
55
main.go
55
main.go
@ -1,6 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
@ -16,6 +25,9 @@ func main() {
|
||||
// Initialize Echo router
|
||||
e := echo.New()
|
||||
|
||||
// Generate a unique version identifier
|
||||
version := time.Now().Format(time.RFC3339)
|
||||
|
||||
// Middleware
|
||||
e.Use(middleware.Logger())
|
||||
e.Use(middleware.Recover())
|
||||
@ -27,6 +39,40 @@ func main() {
|
||||
Level: 5,
|
||||
}))
|
||||
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(50)))
|
||||
// Use middleware to set ETag and Cache-Control headers
|
||||
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// Get the path of the requested resource
|
||||
path := c.Request().URL.Path
|
||||
|
||||
// If the requested resource is a CSS file
|
||||
if strings.HasSuffix(path, ".css") {
|
||||
log.Println(path)
|
||||
// Read the CSS file
|
||||
data, err := os.ReadFile(filepath.Join("public", strings.TrimPrefix(path, "/public")))
|
||||
if err != nil {
|
||||
// Log the error and return a 500 status
|
||||
log.Println(err)
|
||||
return c.NoContent(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Compute the hash of the CSS file contents
|
||||
hash := sha256.Sum256(data)
|
||||
|
||||
// Set the ETag to the hash
|
||||
c.Response().Header().Set("ETag", hex.EncodeToString(hash[:]))
|
||||
|
||||
// Set the Content-Type to text/css
|
||||
c.Response().Header().Set("Content-Type", "text/css")
|
||||
} else {
|
||||
// For other resources, set the ETag to the server start time
|
||||
c.Response().Header().Set("ETag", version)
|
||||
}
|
||||
|
||||
c.Response().Header().Set("Cache-Control", "public, no-cache")
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
|
||||
// Static server
|
||||
e.Static("/public", "public")
|
||||
@ -43,6 +89,11 @@ func main() {
|
||||
apiGroup.GET("/sse", api.SSE)
|
||||
apiGroup.POST("/sendsse", api.SSEDemoSend)
|
||||
|
||||
// Start server
|
||||
e.Logger.Fatal(e.Start(":3000"))
|
||||
// Start server with HTTP/2 support
|
||||
s := &http.Server{
|
||||
Addr: ":3000",
|
||||
Handler: e,
|
||||
}
|
||||
e.Logger.Fatal(e.StartServer(s))
|
||||
log.Println("Server started on port 3000")
|
||||
}
|
||||
|
Reference in New Issue
Block a user