2023-09-12 06:40:24 -04:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2023-09-16 09:46:59 -04:00
|
|
|
"embed"
|
|
|
|
"html/template"
|
2023-09-25 03:43:21 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-13 01:30:45 -04:00
|
|
|
"sub2clash/api/handler"
|
2023-09-20 21:08:02 -04:00
|
|
|
"sub2clash/config"
|
2023-09-13 01:47:22 -04:00
|
|
|
"sub2clash/middleware"
|
2023-11-02 14:35:30 -04:00
|
|
|
|
|
|
|
"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-16 09:46:59 -04:00
|
|
|
|
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-16 09:46:59 -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))
|
|
|
|
},
|
|
|
|
)
|
2023-09-16 09:46:59 -04:00
|
|
|
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]
|
|
|
|
}
|
2023-09-20 21:08:02 -04:00
|
|
|
c.HTML(
|
|
|
|
200, "index.html", gin.H{
|
2023-09-25 03:43:21 -04:00
|
|
|
"Version": version,
|
2023-09-20 21:08:02 -04:00
|
|
|
},
|
|
|
|
)
|
2023-09-16 09:46:59 -04:00
|
|
|
},
|
|
|
|
)
|
2023-09-12 06:40:24 -04:00
|
|
|
r.GET(
|
|
|
|
"/clash", func(c *gin.Context) {
|
2024-03-13 01:30:45 -04:00
|
|
|
handler.SubmodHandler(c)
|
2023-09-12 06:40:24 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
r.GET(
|
|
|
|
"/meta", func(c *gin.Context) {
|
2024-03-13 01:30:45 -04:00
|
|
|
handler.SubHandler(c)
|
2023-09-12 06:40:24 -04:00
|
|
|
},
|
|
|
|
)
|
2024-03-13 04:28:40 -04:00
|
|
|
r.GET(
|
|
|
|
"/s/:hash", func(c *gin.Context) {
|
|
|
|
handler.ShortLinkGetConfigHandler(c)
|
2023-09-17 03:52:37 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
r.GET(
|
2024-03-13 04:28:40 -04:00
|
|
|
"/short", func(c *gin.Context) {
|
|
|
|
handler.ShortLinkGetUrlHandler(c)
|
|
|
|
})
|
|
|
|
r.POST(
|
|
|
|
"/short", func(c *gin.Context) {
|
|
|
|
handler.ShortLinkGenHandler(c)
|
2024-03-13 01:30:45 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
r.PUT(
|
|
|
|
"/short", func(c *gin.Context) {
|
|
|
|
handler.ShortLinkUpdateHandler(c)
|
2023-09-17 03:52:37 -04:00
|
|
|
},
|
|
|
|
)
|
2023-09-12 06:40:24 -04:00
|
|
|
}
|