init commit

This commit is contained in:
flswld
2022-11-20 15:38:00 +08:00
parent eda2b643b9
commit 3efed3defe
5834 changed files with 636508 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package controller
import (
"air/service"
"flswld.com/common/config"
"github.com/gin-gonic/gin"
"strconv"
)
type Controller struct {
service *service.Service
}
func NewController(service *service.Service) (r *Controller) {
r = new(Controller)
r.service = service
go r.registerRouter()
return r
}
func (c *Controller) registerRouter() {
if config.CONF.Logger.Level == "DEBUG" {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.Default()
// HTTP
engine.GET("/http/fetch", c.fetchHttpService)
engine.GET("/http/fetch/all", c.fetchAllHttpService)
engine.POST("/http/reg", c.registerHttpService)
engine.POST("/http/cancel", c.cancelHttpService)
engine.POST("/http/ka", c.httpKeepalive)
// RPC
engine.GET("/rpc/fetch", c.fetchRpcService)
engine.GET("/rpc/fetch/all", c.fetchAllRpcService)
engine.POST("/rpc/reg", c.registerRpcService)
engine.POST("/rpc/cancel", c.cancelRpcService)
engine.POST("/rpc/ka", c.rpcKeepalive)
// 长轮询
engine.GET("/poll/http", c.pollHttpService)
engine.GET("/poll/http/all", c.pollAllHttpService)
engine.GET("/poll/rpc", c.pollRpcService)
engine.GET("/poll/rpc/all", c.pollAllRpcService)
port := strconv.FormatInt(int64(config.CONF.HttpPort), 10)
portStr := ":" + port
_ = engine.Run(portStr)
}

View File

@@ -0,0 +1,39 @@
package controller
import "github.com/gin-gonic/gin"
// 获取HTTP服务
func (c *Controller) fetchHttpService(context *gin.Context) {
inst := c.service.FetchHttpService(context.Query("name"))
context.JSON(200, gin.H{
"code": 0,
"instance": inst,
})
}
// 获取所有HTTP服务
func (c *Controller) fetchAllHttpService(context *gin.Context) {
svc := c.service.FetchAllHttpService()
context.JSON(200, gin.H{
"code": 0,
"service": svc,
})
}
// 获取RPC服务
func (c *Controller) fetchRpcService(context *gin.Context) {
inst := c.service.FetchRpcService(context.Query("name"))
context.JSON(200, gin.H{
"code": 0,
"instance": inst,
})
}
// 获取所有RPC服务
func (c *Controller) fetchAllRpcService(context *gin.Context) {
svc := c.service.FetchAllRpcService()
context.JSON(200, gin.H{
"code": 0,
"service": svc,
})
}

View File

@@ -0,0 +1,35 @@
package controller
import (
"air/entity"
"flswld.com/logger"
"github.com/gin-gonic/gin"
)
// HTTP心跳
func (c *Controller) httpKeepalive(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.HttpKeepalive(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}
// RPC心跳
func (c *Controller) rpcKeepalive(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.RpcKeepalive(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}

View File

@@ -0,0 +1,85 @@
package controller
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func (c *Controller) pollHttpService(context *gin.Context) {
serviceName := context.Query("name")
recvrNtfr := c.service.RegistryHttpNotifyReceiver(serviceName)
timeout := time.NewTicker(time.Second * 30)
select {
case inst := <-recvrNtfr.NotifyChannel:
c.service.CancelHttpNotifyReceiver(serviceName, recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"instance": inst,
})
case <-timeout.C:
c.service.CancelHttpNotifyReceiver(serviceName, recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"instance": nil,
})
}
}
func (c *Controller) pollAllHttpService(context *gin.Context) {
recvrNtfr := c.service.RegistryAllHttpNotifyReceiver()
timeout := time.NewTicker(time.Second * 30)
select {
case svc := <-recvrNtfr.NotifyChannel:
c.service.CancelAllHttpNotifyReceiver(recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"service": svc,
})
case <-timeout.C:
c.service.CancelAllHttpNotifyReceiver(recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"service": nil,
})
}
}
func (c *Controller) pollRpcService(context *gin.Context) {
serviceName := context.Query("name")
recvrNtfr := c.service.RegistryRpcNotifyReceiver(serviceName)
timeout := time.NewTicker(time.Second * 30)
select {
case inst := <-recvrNtfr.NotifyChannel:
c.service.CancelRpcNotifyReceiver(serviceName, recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"instance": inst,
})
case <-timeout.C:
c.service.CancelRpcNotifyReceiver(serviceName, recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"instance": nil,
})
}
}
func (c *Controller) pollAllRpcService(context *gin.Context) {
recvrNtfr := c.service.RegistryAllRpcNotifyReceiver()
timeout := time.NewTicker(time.Second * 30)
select {
case svc := <-recvrNtfr.NotifyChannel:
c.service.CancelAllRpcNotifyReceiver(recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"service": svc,
})
case <-timeout.C:
c.service.CancelAllRpcNotifyReceiver(recvrNtfr.Id)
context.JSON(http.StatusOK, gin.H{
"code": 0,
"service": nil,
})
}
}

View File

@@ -0,0 +1,63 @@
package controller
import (
"air/entity"
"flswld.com/logger"
"github.com/gin-gonic/gin"
)
// 注册HTTP服务
func (c *Controller) registerHttpService(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.RegisterHttpService(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}
// 取消注册HTTP服务
func (c *Controller) cancelHttpService(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.CancelHttpService(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}
// 注册RPC服务
func (c *Controller) registerRpcService(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.RegisterRpcService(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}
// 取消注册RPC服务
func (c *Controller) cancelRpcService(context *gin.Context) {
inst := new(entity.Instance)
err := context.ShouldBindJSON(inst)
if err != nil {
logger.LOG.Error("parse json error: %v", err)
return
}
c.service.CancelRpcService(*inst)
context.JSON(200, gin.H{
"code": 0,
})
}