1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-26 01:10:53 -05:00
sub2clash/api/controller/clash.go

31 lines
659 B
Go
Raw Permalink Normal View History

2023-09-12 06:40:24 -04:00
package controller
import (
2023-09-13 01:47:22 -04:00
"github.com/gin-gonic/gin"
"gopkg.in/yaml.v3"
2023-09-12 06:40:24 -04:00
"net/http"
2023-09-12 12:46:17 -04:00
"sub2clash/config"
"sub2clash/validator"
2023-09-12 06:40:24 -04:00
)
func SubmodHandler(c *gin.Context) {
// 从请求中获取参数
2023-09-13 01:47:22 -04:00
query, err := validator.ParseQuery(c)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
2023-09-12 06:40:24 -04:00
return
}
2023-09-13 01:47:22 -04:00
sub, err := BuildSub(query, config.Default.ClashTemplate)
2023-09-12 06:40:24 -04:00
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
// 输出
2023-09-13 01:47:22 -04:00
marshal, err := yaml.Marshal(sub)
2023-09-12 06:40:24 -04:00
if err != nil {
2023-09-13 01:47:22 -04:00
c.String(http.StatusInternalServerError, "YAML序列化失败: "+err.Error())
2023-09-12 06:40:24 -04:00
return
}
2023-09-13 01:47:22 -04:00
c.String(http.StatusOK, string(marshal))
2023-09-12 06:40:24 -04:00
}