Оптимизация страниц. Начало реализации мультиплеера

This commit is contained in:
2025-11-03 21:23:41 +03:00
parent d6bba525d3
commit 16fe1342ed
5 changed files with 92 additions and 36 deletions

View File

@@ -4,35 +4,64 @@ import (
// "flag"
// "fmt"
"embed"
"fmt"
"net/http"
"strings"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
//go:embed static
//go:embed toe
var staticFiles embed.FS
func SetCustomContentType() gin.HandlerFunc {
return func(c *gin.Context) {
return func(c *gin.Context) {
requestPath := c.Request.URL.Path
if strings.HasPrefix(requestPath, "/l/static") {
c.Header("Content-Type", "text/html; charset=UTF-8")
if strings.Contains(requestPath, ".") {
c.Header("Cache-Control", "public, max-age=31536000, immutable")
}
c.Next()
}
if strings.HasPrefix(requestPath, "/tic/tac/toe") && !strings.Contains(requestPath, ".") {
c.Header("Content-Type", "text/html; charset=UTF-8")
} else if strings.HasSuffix(requestPath, ".ico") {
c.Header("Content-Type", "image/svg+xml")
} else if strings.HasSuffix(requestPath, ".js") {
c.Header("Content-Type", "application/javascript")
} else if strings.HasSuffix(requestPath, ".css") {
c.Header("Content-Type", "text/css")
}
c.Next()
}
}
var favicon = "<svg width=\"32\" height=\"32\" viewBox=\"0 0 32 32\" xmlns=\"http://www.w3.org/2000/svg\"><path fill=\"none\" stroke=\"#000\" d=\"M10 10h12v12H10z\"/><path d=\"M22 22a8 8 0 0 0-12-12\" fill=\"none\" stroke=\"#f30\" stroke-width=\"2\"/><path stroke=\"#07f\" stroke-width=\"2\" d=\"m8 8 16 16M8 24l8-8\"/><path stroke=\"#000\" d=\"m8.5 7.5 16 16\"/></svg>"
func main() {
embeddedFilesSystem := http.FS(staticFiles)
router := gin.Default()
router.Use(SetCustomContentType())
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
router.StaticFS("/l", embeddedFilesSystem)
router.Run(":8080")
}
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/tic/tac/toe/index")
})
router.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusOK, favicon)
})
router.GET("/multiplayer", multiplayer)
router.StaticFS("/tic/tac", embeddedFilesSystem)
router.Run(":8080")
}
func multiplayer(c *gin.Context) {
if c.GetHeader("Accept") == "*/*" {
c.String(http.StatusTeapot, "Preload not allowed")
}
u, err := uuid.NewV7()
if err != nil {
fmt.Println("Ошибка:", err)
return
}
c.String(http.StatusOK, fmt.Sprintf("ToDo\nUUID7: %s", u.String()))
}