This commit is contained in:
Hossin Asaadi
2022-11-10 15:08:27 -05:00
parent 3faf7bb303
commit 7fbd7c24ca
6 changed files with 80 additions and 16 deletions

48
web/controller/api.go Normal file
View File

@@ -0,0 +1,48 @@
package controller
import (
"github.com/gin-gonic/gin"
)
type APIController struct {
BaseController
inboundController *InboundController
settingController *SettingController
}
func NewAPIController(g *gin.RouterGroup) *APIController {
a := &APIController{}
a.initRouter(g)
return a
}
func (a *APIController) initRouter(g *gin.RouterGroup) {
g = g.Group("/xui/API/inbounds")
g.Use(a.checkLogin)
g.GET("/", a.inbounds)
g.GET("/get/:id", a.inbound)
g.POST("/add", a.addInbound)
g.POST("/del/:id", a.delInbound)
g.POST("/update/:id", a.updateInbound)
a.inboundController = NewInboundController(g)
}
func (a *APIController) inbounds(c *gin.Context) {
a.inboundController.getInbounds(c)
}
func (a *APIController) inbound(c *gin.Context) {
a.inboundController.getInbound(c)
}
func (a *APIController) addInbound(c *gin.Context) {
a.inboundController.addInbound(c)
}
func (a *APIController) delInbound(c *gin.Context) {
a.inboundController.delInbound(c)
}
func (a *APIController) updateInbound(c *gin.Context) {
a.inboundController.updateInbound(c)
}

View File

@@ -59,6 +59,19 @@ func (a *InboundController) getInbounds(c *gin.Context) {
}
jsonObj(c, inbounds, nil)
}
func (a *InboundController) getInbound(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
jsonMsg(c, I18n(c , "get"), err)
return
}
inbound, err := a.inboundService.GetInbound(id)
if err != nil {
jsonMsg(c, I18n(c , "pages.inbounds.toasts.obtain"), err)
return
}
jsonObj(c, inbound, nil)
}
func (a *InboundController) addInbound(c *gin.Context) {
inbound := &model.Inbound{}
@@ -71,8 +84,8 @@ func (a *InboundController) addInbound(c *gin.Context) {
inbound.UserId = user.Id
inbound.Enable = true
inbound.Tag = fmt.Sprintf("inbound-%v", inbound.Port)
err = a.inboundService.AddInbound(inbound)
jsonMsg(c, I18n(c , "pages.inbounds.addTo"), err)
inbound, err = a.inboundService.AddInbound(inbound)
jsonMsgObj(c, I18n(c , "pages.inbounds.addTo"), inbound, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
@@ -85,7 +98,7 @@ func (a *InboundController) delInbound(c *gin.Context) {
return
}
err = a.inboundService.DelInbound(id)
jsonMsg(c, I18n(c , "delete"), err)
jsonMsgObj(c, I18n(c , "delete"), id, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}
@@ -105,8 +118,8 @@ func (a *InboundController) updateInbound(c *gin.Context) {
jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
return
}
err = a.inboundService.UpdateInbound(inbound)
jsonMsg(c, I18n(c , "pages.inbounds.revise"), err)
inbound, err = a.inboundService.UpdateInbound(inbound)
jsonMsgObj(c, I18n(c , "pages.inbounds.revise"), inbound, err)
if err == nil {
a.xrayService.SetToNeedRestart()
}