1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 02:04:41 -05:00
sub2clash/api/route.go

67 lines
1.1 KiB
Go
Raw 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"
2023-09-12 12:46:17 -04:00
"sub2clash/api/controller"
"sub2clash/config"
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) {
2023-09-25 03:43:21 -04:00
version := config.Version
if len(config.Version) > 7 {
version = config.Version[:7]
}
c.HTML(
200, "index.html", gin.H{
2023-09-25 03:43:21 -04:00
"Version": version,
},
)
},
)
2023-09-12 06:40:24 -04:00
r.GET(
"/clash", func(c *gin.Context) {
controller.SubmodHandler(c)
},
)
r.GET(
"/meta", func(c *gin.Context) {
controller.SubHandler(c)
},
)
2023-09-17 03:52:37 -04:00
r.POST(
"/short", func(c *gin.Context) {
controller.ShortLinkGenHandler(c)
},
)
r.GET(
"/s/:hash", func(c *gin.Context) {
controller.ShortLinkGetHandler(c)
},
)
2023-09-12 06:40:24 -04:00
}