1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 14:04:41 -05:00
sub2sing-box/api/server.go

50 lines
813 B
Go
Raw Normal View History

2024-03-11 13:34:08 -04:00
package api
import (
2024-03-19 01:29:40 -04:00
"embed"
2024-03-11 13:34:08 -04:00
"fmt"
2024-03-19 01:29:40 -04:00
"html/template"
"net/http"
2024-03-11 13:34:08 -04:00
"strconv"
2024-09-19 06:12:24 -04:00
"github.com/nitezs/sub2sing-box/api/handler"
2024-03-11 13:34:08 -04:00
"github.com/gin-gonic/gin"
)
2024-03-19 01:29:40 -04:00
//go:embed static
var staticFiles embed.FS
2024-03-11 13:34:08 -04:00
func RunServer(port uint16) {
2024-03-19 01:29:40 -04:00
tpl, err := template.ParseFS(staticFiles, "static/*")
if err != nil {
println(err.Error())
}
2024-03-11 13:34:08 -04:00
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
2024-03-19 01:29:40 -04:00
r.SetHTMLTemplate(tpl)
r.GET(
"/static/*path", func(c *gin.Context) {
c.FileFromFS("static/"+c.Param("path"), http.FS(staticFiles))
},
)
r.GET(
"/", func(c *gin.Context) {
c.HTML(
200, "index.html", nil,
)
},
)
2024-03-11 13:34:08 -04:00
r.GET("/convert", handler.Convert)
2024-03-12 14:02:17 -04:00
fmt.Println("Server is running on port", port)
2024-03-19 01:29:40 -04:00
err = r.Run(":" + strconv.Itoa(int(port)))
2024-03-11 13:34:08 -04:00
if err != nil {
fmt.Println("Run server failed: ", err)
}
}