1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gin_starter.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-09-17 16:31 . feat: rebuild log
package gin_plugin
import (
"fmt"
"gitee.com/kristas/booting-go/framework/common/util/lang"
"gitee.com/kristas/booting-go/framework/core/bean"
"gitee.com/kristas/booting-go/framework/core/ioc/bean_repository"
"gitee.com/kristas/booting-go/framework/core/log"
"gitee.com/kristas/booting-go/framework/web"
"github.com/gin-gonic/gin"
"net/http"
"regexp"
)
type Starter struct {
bean.Component
app *gin.Engine
Log log.Logger
}
func NewStarter() *Starter {
return &Starter{}
}
func (s *Starter) Beans() (beans bean.Components) {
s.app = gin.Default()
bean_repository.RegisterSingletonBeanWithName("app", s.app)
return
}
func (s *Starter) RegisterEndpoint(endpoint web.Endpoint) {
info := endpoint.GetApiInfo()
s.app.Handle(info.GetHttpMethod(), pathVariableParser(info.GetURL()), func(ctx *gin.Context) {
endpoint.Handler(newMapperAdapter(ctx), func(result interface{}) error {
ctx.JSON(http.StatusOK, result)
return nil
})
})
}
func (s *Starter) FsHandler(prefix, dir string) {
s.app.Static(prefix, dir)
}
func (s *Starter) ListenAndServe(port int) {
s.Log.Fatal(s.app.Run(fmt.Sprintf(":%d", port)).Error())
}
func pathVariableParser(url string) string {
compile := regexp.MustCompile("{\\w+}")
vars := compile.FindAllString(url, -1)
t := lang.NewString(url)
for _, v := range vars {
pv := lang.NewString(v).RemovePrefix("{").RemoveSuffix("}").String()
t = t.Replace(v, ":"+pv, 1)
}
return t.String()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.7

搜索帮助