2 Star 0 Fork 338

hxchjm / go-zero

forked from kevwan / go-zero 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gen.go 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
kingxt 提交于 2021-01-11 15:10 . feature: refactor api parse to g4 (#365)
package apigen
import (
"errors"
"fmt"
"path/filepath"
"strings"
"text/template"
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/urfave/cli"
)
const apiTemplate = `
syntax = "v1"
info(
title: // TODO: add title
desc: // TODO: add description
author: "{{.gitUser}}"
email: "{{.gitEmail}}"
)
type request {
// TODO: add members here and delete this comment
}
type response {
// TODO: add members here and delete this comment
}
service {{.serviceName}} {
@handler GetUser // TODO: set handler name and delete this comment
get /users/id/:userId(request) returns(response)
@handler CreateUser // TODO: set handler name and delete this comment
post /users/create(request)
}
`
func ApiCommand(c *cli.Context) error {
apiFile := c.String("o")
if len(apiFile) == 0 {
return errors.New("missing -o")
}
fp, err := util.CreateIfNotExist(apiFile)
if err != nil {
return err
}
defer fp.Close()
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
baseName = baseName[:len(baseName)-4]
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {
baseName = baseName[:len(baseName)-3]
}
t := template.Must(template.New("etcTemplate").Parse(apiTemplate))
if err := t.Execute(fp, map[string]string{
"gitUser": getGitName(),
"gitEmail": getGitEmail(),
"serviceName": baseName + "-api",
}); err != nil {
return err
}
fmt.Println(aurora.Green("Done."))
return nil
}
Go
1
https://gitee.com/hxchjm/go-zero.git
git@gitee.com:hxchjm/go-zero.git
hxchjm
go-zero
go-zero
3eb88c4e4bf1

搜索帮助