1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 01:24:42 -05:00
sub2clash/api/route.go

52 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-09-12 06:40:24 -04:00
package api
import (
"embed"
"html/template"
2023-09-25 03:43:21 -04:00
"log"
"net/http"
"sub2clash/api/handler"
"sub2clash/constant"
2023-09-13 01:47:22 -04:00
"sub2clash/middleware"
"github.com/gin-gonic/gin"
2023-09-12 06:40:24 -04:00
)
2023-09-25 03:43:21 -04:00
//go:embed static
var staticFiles embed.FS
2023-09-12 06:40:24 -04:00
func SetRoute(r *gin.Engine) {
2023-09-13 01:47:22 -04:00
r.Use(middleware.ZapLogger())
2023-09-25 03:43:21 -04:00
// 使用内嵌的模板文件
2023-09-25 03:43:21 -04:00
tpl, err := template.ParseFS(staticFiles, "static/*")
if err != nil {
log.Fatalf("Error parsing templates: %v", err)
}
r.SetHTMLTemplate(tpl)
r.GET(
"/static/*filepath", func(c *gin.Context) {
c.FileFromFS("static/"+c.Param("filepath"), http.FS(staticFiles))
},
)
r.GET(
"/", func(c *gin.Context) {
version := constant.Version
if len(constant.Version) > 7 {
version = constant.Version[:7]
2023-09-25 03:43:21 -04:00
}
c.HTML(
200, "index.html", gin.H{
2023-09-25 03:43:21 -04:00
"Version": version,
},
)
},
)
r.GET("/clash", handler.SubmodHandler)
r.GET("/meta", handler.SubHandler)
r.GET("/s/:hash", handler.GetRawConfHandler)
r.POST("/short", handler.GenerateLinkHandler)
r.PUT("/short", handler.UpdateLinkHandler)
2023-09-12 06:40:24 -04:00
}