diff --git a/web/controller/server.go b/web/controller/server.go index 259062f2..5a333b23 100644 --- a/web/controller/server.go +++ b/web/controller/server.go @@ -27,9 +27,13 @@ type ServerController struct { lastGetVersionsTime time.Time } -func NewServerController(g *gin.RouterGroup) *ServerController { +// 〔中文注释〕: 1. 在函数参数中,增加 serverService service.ServerService,让它可以接收一个服务实例。 +func NewServerController(g *gin.RouterGroup, serverService service.ServerService) *ServerController { a := &ServerController{ lastGetStatusTime: time.Now(), + // 〔中文注释〕: 2. 将传入的 serverService 赋值给 a.serverService。 + // 这样一来,这个 Controller 内部使用的就是我们在 main.go 中创建的那个功能完整的服务了。 + serverService: serverService, } a.initRouter(g) a.startTask() @@ -58,6 +62,8 @@ func (a *ServerController) initRouter(g *gin.RouterGroup) { g.POST("/getNewEchCert", a.getNewEchCert) g.POST("/history/save", a.saveHistory) g.GET("/history/load", a.loadHistory) + g.POST("/install/subconverter", a.installSubconverter) + g.POST("/openPort", a.openPort) } func (a *ServerController) refreshStatus() { @@ -333,3 +339,38 @@ func (a *ServerController) loadHistory(c *gin.Context) { } jsonObj(c, history, nil) } + + +// 〔新增接口〕: 安装 Subconverter +// 〔中文注释〕: 这个函数是暴露给前端的 API 接口,用于处理安装 Subconverter 的请求。 +func (a *ServerController) installSubconverter(c *gin.Context) { + // 〔中文注释〕: 调用服务层中我们刚刚创建的 InstallSubconverter 方法。 + err := a.serverService.InstallSubconverter() + if err != nil { + // 〔中文注释〕: 如果 service 层返回了错误,则向前台返回失败的 JSON 消息。 + jsonMsg(c, "Subconverter 安装指令执行失败", err) + return + } + // 〔中文注释〕: 如果没有错误,则向前台返回成功的 JSON 消息。 + jsonMsg(c, "Subconverter 安装指令已成功发送", nil) +} + +// 【新增接口实现】: 前端放行端口 +func (a *ServerController) openPort(c *gin.Context) { + + // 直接使用 c.PostForm("port") 获取表单数据 + port := c.PostForm("port") + + // 1. 手动进行参数校验 + if port == "" { + jsonMsg(c, "请求端口参数失败", fmt.Errorf("无效的请求参数,请确保端口号存在")) + return + } + + // 【中文注释】: 2. 调用服务层方法,该方法会立即返回,并在后台启动一个协程执行任务。 + a.serverService.OpenPort(port) + + // 【中文注释】: 3. 因为服务层方法是异步的,不再检查它的 error 返回值。 + // 直接向前端返回一个成功的消息,告知用户指令已发送。 + jsonMsg(c, "端口放行指令已成功发送,正在后台执行...", nil) +}