package server import ( "embed" "io/fs" "log" "net/http" "strings" "github.com/gin-gonic/gin" "github.com/zggsong/FileSync/config" c "github.com/zggsong/FileSync/server/controller" "github.com/zggsong/FileSync/server/ws" ) //go:embed frontend/dist/* var FS embed.FS func Run() { hub := ws.NewHub() go hub.Run() gin.SetMode(gin.ReleaseMode) router := gin.Default() router.POST("/api/v1/files", c.FilesController) router.POST("/api/v1/texts", c.TextsController) router.GET("/api/v1/qrcodes", c.QrcodesController) router.GET("/uploads/:path", c.UploadsController) router.GET("/api/v1/addresses", c.AddressesController) router.GET("/ws", func(c *gin.Context) { ws.HttpController(c, hub) }) staticFiles, _ := fs.Sub(FS, "frontend/dist") router.StaticFS("/static", http.FS(staticFiles)) router.NoRoute(func(c *gin.Context) { path := c.Request.URL.Path if strings.HasPrefix(path, "/static/") { reader, err := staticFiles.Open("index.html") if err != nil { log.Fatal(err) } defer reader.Close() stat, err := reader.Stat() if err != nil { log.Fatal(err) } c.DataFromReader(http.StatusOK, stat.Size(), "text/html", reader, nil) } else { c.Status(http.StatusNotFound) } }) router.Run(":" + config.GetPort()) }