Skip to content

Commit

Permalink
调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
陈壁浩 committed Jul 18, 2024
1 parent a1036f3 commit e47ade2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 67 deletions.
4 changes: 2 additions & 2 deletions app/http/module/demo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type DemoApi struct {

func Register(r *gin.Engine) error {
api := NewDemoApi()
r.Bind(&demoService.DemoProvider{})
_ = r.Bind(&demoService.DemoProvider{})

r.GET("/demo/demo", api.Demo)
r.GET("/demo/demo2", api.Demo2)
Expand Down Expand Up @@ -72,7 +72,7 @@ func (api *DemoApi) DemoPost(c *gin.Context) {
foo := &Foo{}
err := c.BindJSON(&foo)
if err != nil {
c.AbortWithError(500, err)
_ = c.AbortWithError(500, err)
}
c.JSON(200, nil)
}
42 changes: 1 addition & 41 deletions docs/src/gen_docs/gen_get_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func getProviderSourceCode(filePath string, fset *token.FileSet, d *ast.TypeSpec
return sb.String()
}

func getProviderRemark(filePath string, fset *token.FileSet, astFile *ast.File) (s string) {
func getRemark(filePath string, fset *token.FileSet, astFile *ast.File) (s string) {
objects := astFile.Scope.Objects
var object *ast.Object
for k, o := range objects {
Expand Down Expand Up @@ -118,43 +118,3 @@ func getCommandKey(astFile *ast.File, key string) string {
}
return key
}

func getCommandRemark(filePath string, fset *token.FileSet, astFile *ast.File) (s string) {
objects := astFile.Scope.Objects
var object *ast.Object
for k, o := range objects {
if object == nil && strings.HasSuffix(k, "Key") {
object = o
}
}
if object == nil {
return
}

startLine := 0
endLine := fset.Position(object.Pos()).Line - 1

if len(astFile.Imports) != 0 {
startLine = fset.Position(astFile.Imports[len(astFile.Imports)-1].End()).Line + 1
} else {
startLine = fset.Position(astFile.Package).Line + 1
}

f, _ := os.OpenFile(filePath, os.O_RDONLY, 0666)
defer f.Close()

buf := make([]byte, 1024*4)
f.Read(buf)
reader := bufio.NewReader(bytes.NewReader(buf))
sb := strings.Builder{}
for i := 1; i < endLine; i++ {
line, _, _ := reader.ReadLine()
if i > startLine {
sb.Write(line)
if i != endLine {
sb.Write([]byte("\n"))
}
}
}
return sb.String()
}
4 changes: 2 additions & 2 deletions docs/src/gen_docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func genProviderDocs(targetPath, genPath string, selectFiles []string) {
var key, remark, code string

// 获取说明
remark = getProviderRemark(filePath, fset, astFile)
remark = getRemark(filePath, fset, astFile)
remark = strings.Trim(remark, "/*\n")
remark = strings.Trim(remark, "\n*/")

Expand Down Expand Up @@ -148,7 +148,7 @@ func genCommandDocs(targetPath, genPath string, genCommandSub map[string]string,
var key, remark string

// 获取说明
remark = getCommandRemark(filePath, fset, astFile)
remark = getRemark(filePath, fset, astFile)
remark = strings.Trim(remark, "/*\n")
remark = strings.Trim(remark, "\n*/")

Expand Down
4 changes: 2 additions & 2 deletions framework/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (container *GobContainer) Bind(provider ServiceProvider) error {
container.lock.Unlock()

// if provider is not defer
if provider.IsDefer() == false {
if !provider.IsDefer() {
if err := provider.Boot(container); err != nil {
return err
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func (container *GobContainer) make(key string, params []interface{}, forceNew b

// NameList 列出容器中所有服务提供者的字符串凭证
func (container *GobContainer) NameList() []string {
ret := []string{}
var ret []string
for _, provider := range container.providers {
name := provider.Name()
ret = append(ret, name)
Expand Down
5 changes: 1 addition & 4 deletions framework/provider/config/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ func NewConfigService(params ...interface{}) (interface{}, error) {
for {
select {
case ev := <-watch.Events:
// 判断事件发生的类型
// Create 创建
// Write 写入
// Remove 删除
// 判断事件发生的类型 Create 创建 Write 写入 Remove 删除
path, _ := filepath.Abs(ev.Name)
index := strings.LastIndex(path, string(os.PathSeparator))
folder := path[:index]
Expand Down
2 changes: 2 additions & 0 deletions framework/provider/log/formatter/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func Prefix(level contract.LogLevel) string {
prefix = "[Debug]"
case contract.TraceLevel:
prefix = "[Trace]"
default:
prefix = "[UnknownLevel]"
}
return prefix
}
2 changes: 1 addition & 1 deletion framework/provider/log/services/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ConsoleLogService struct {
LogService
}

// NewConsoleLog 实例化 ConsoleLog
// NewConsoleLogService 实例化 ConsoleLog
func NewConsoleLogService(params ...interface{}) (interface{}, error) {
c := params[0].(framework.Container)
level := params[1].(contract.LogLevel)
Expand Down
30 changes: 15 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ func main() {
args := os.Args
if len(args) > 1 && args[1] == "new" {
container := framework.NewGobContainer()
console.RunRootCommand(container, true)
_ = console.RunRootCommand(container, true)
return
}

// 初始化服务容器
container := framework.NewGobContainer()
container.Bind(&app.AppProvider{}) // 绑定 App 服务提供者
container.Bind(&env.EnvProvider{}) // 绑定 环境变量 服务提供者
container.Bind(&distributed.LocalDistributedProvider{}) // 绑定 分布式本地锁 服务提供者
container.Bind(&config.ConfigProvider{}) // 绑定 配置 服务提供者
container.Bind(&log.LogProvider{}) // 绑定 日志 服务提供者
container.Bind(&id.IDProvider{}) // 绑定 ID 服务提供者
container.Bind(&trace.TraceProvider{}) // 绑定 全链路支持 服务提供者
container.Bind(&orm.GormProvider{}) // 绑定 orm 服务提供者
container.Bind(&redis.RedisProvider{}) // 绑定 redis 服务提供者
container.Bind(&cache.CacheProvider{}) // 绑定 缓存 服务提供者
container.Bind(&ssh.SSHProvider{}) // 绑定 ssh 服务提供者
container.Bind(&sls.SLSProvider{}) // 绑定 sls 服务提供者
_ = container.Bind(&app.AppProvider{}) // 绑定 App 服务提供者
_ = container.Bind(&env.EnvProvider{}) // 绑定 环境变量 服务提供者
_ = container.Bind(&distributed.LocalDistributedProvider{}) // 绑定 分布式本地锁 服务提供者
_ = container.Bind(&config.ConfigProvider{}) // 绑定 配置 服务提供者
_ = container.Bind(&log.LogProvider{}) // 绑定 日志 服务提供者
_ = container.Bind(&id.IDProvider{}) // 绑定 ID 服务提供者
_ = container.Bind(&trace.TraceProvider{}) // 绑定 全链路支持 服务提供者
_ = container.Bind(&orm.GormProvider{}) // 绑定 orm 服务提供者
_ = container.Bind(&redis.RedisProvider{}) // 绑定 redis 服务提供者
_ = container.Bind(&cache.CacheProvider{}) // 绑定 缓存 服务提供者
_ = container.Bind(&ssh.SSHProvider{}) // 绑定 ssh 服务提供者
_ = container.Bind(&sls.SLSProvider{}) // 绑定 sls 服务提供者

// 将 HTTP 引擎初始化,并且作为服务提供者绑定到服务容器中
if engine, err := http.NewHttpEngine(container); err == nil {
// 绑定 Kernel 服务提供者
container.Bind(&kernel.KernelProvider{HttpEngine: engine})
_ = container.Bind(&kernel.KernelProvider{HttpEngine: engine})
}
// 运行root命令
console.RunRootCommand(container, false)
_ = console.RunRootCommand(container, false)
}

0 comments on commit e47ade2

Please sign in to comment.