6 Star 12 Fork 2

保卫战 / dragon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
control.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
freeman 提交于 2021-12-02 20:20 . init
package goplugin
import (
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func Serve(port int, initFunc, runFunc interface{}) {
rand.Seed(time.Now().Unix())
engine := gin.Default()
SetCall(INIT, initFunc)
SetCall(RUN, runFunc)
engine.Any(PathRoot, Home)
engine.GET(PathInit, InitService)
engine.POST(PathRunService, RunService)
s := []string{COLON, strconv.Itoa(port)}
engine.Run(strings.Join(s, EMPTY))
}
func InitService(context *gin.Context) {
var s []string
x := GetXCall(INIT)
f := x.Interface().(func() map[string]interface{})
m := f()
for k := range m {
s = append(s, k)
}
fmt.Println(">>>> 已初始化RPC服务列表:", s)
context.JSON(http.StatusOK, s)
}
func RunService(context *gin.Context) {
s := context.Param(ParamService)
r, e := context.GetRawData()
if e != nil {
fmt.Println("RunService failed", e)
context.String(http.StatusBadRequest, e.Error())
return
}
x := GetXCall(RUN)
f := x.Interface().(func(name string, b []byte) []byte)
b := f(s, r)
context.Data(http.StatusOK, CTJSON, b)
}
func Home(context *gin.Context) {
context.String(http.StatusOK, OK)
}
Go
1
https://gitee.com/freemanfeng/dragon.git
git@gitee.com:freemanfeng/dragon.git
freemanfeng
dragon
dragon
v1.0.0

搜索帮助

53164aa7 5694891 3bd8fe86 5694891