inobox
使用的开源项目
使用
控制台程序
go install github.com/inoth/inobox/cmd/inobox@version
$ inobox help
inobox 的一些工具整合
Usage:
inobox [command]
Available Commands:
help Help about any command
init init a project (not yet realized)
rand gen random string
version
Flags:
-h, --help help for inobox
import
go get -u github.com/inoth/inobox
示例
详细内容查看:example
import (
"github.com/gin-gonic/gin"
"github.com/inoth/inobox"
"github.com/inoth/inobox/component/logger"
logzap "github.com/inoth/inobox/component/logger/log_zap"
"github.com/inoth/inobox/register/config"
"github.com/inoth/inobox/server/httpsvr"
"github.com/inoth/inobox/server/httpsvr/middleware"
"github.com/inoth/inobox/server/httpsvr/res"
"github.com/inoth/inobox/server/wssvr"
"github.com/inoth/inobox/utils"
)
func main() {
reg := inobox.New(
config.ConfigWithLocal("config"),
logzap.LoggerWithZap,
wssvr.WebsocketServer(wssvr.SetWsConfig()),
httpsvr.HttpWithGin(
func(hgs *httpsvr.HttpGinServer) {
router := hgs.Engine()
router.GET("", func(c *gin.Context) {
res.OK(c, "ok", map[string]interface{}{
"server_name": hgs.Name(),
"app_name": config.GetString("app_name"),
})
})
router.GET("ws", func(c *gin.Context) {
uid := utils.RandString(16)
client, err := wssvr.NewClient(uid, "", wssvr.WsSvc, c.Writer, c.Request)
if err != nil {
res.ErrBadRequest(c)
return
}
client.Start()
logger.Infof("client:%s connect server.", client.ClientId)
})
router.GET("join", func(c *gin.Context) {
uid := c.Query("uid")
rid := c.Query("rid")
wssvr.WsSvc.JoinRoom(uid, rid)
res.OK(c, "ok")
})
router.GET("exit", func(c *gin.Context) {
uid := c.Query("uid")
rid := c.Query("rid")
wssvr.WsSvc.ExitRoom(uid, rid)
res.OK(c, "ok")
})
},
),
httpsvr.Http2WithGin(
httpsvr.SetHttp2Name("gin2"),
func(hgs *httpsvr.Http2GinServer) {
router := hgs.Engine()
router.Use(
middleware.Cors(),
middleware.Recovery(),
middleware.LoggerAndTrace(),
)
},
func(hgs *httpsvr.Http2GinServer) {
router := hgs.Engine()
router.GET("", func(c *gin.Context) {
res.OK(c, "ok", map[string]interface{}{
"server_name": hgs.Name(),
"app_name": config.GetString("app_name"),
})
})
},
),
)
reg.Run()
}
component
// 配置,default path: ./config
config.ConfigWithLocal()
// logger && zap
logzap.LoggerWithZap
// redis
redis.CacheWithRedis
// mysql && gorm
mysql.DBWithGormMysql
server
// http && gin
httpsvr.HttpWithGin()
// http2 && gin
httpsvr.Http2WithGin()
// udp
udpsvr.UdpWithUdp()
// websocket
wssvr.WebsocketServer(wssvr.SetWsConfig())
获取启动
// 创建注册器
inobox.New()
// 使用默认注册器
inobox.Default()