代码拉取完成,页面将自动刷新
package main
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"strings"
)
//go:embed template.go.tpl
var tpl string
type service struct {
Name string // Greeter
FullName string // helloworld.Greeter
FilePath string // api/helloworld/helloworld.proto
Methods []*method
MethodSet map[string]*method
}
func (s *service) execute() string {
if s.MethodSet == nil {
s.MethodSet = map[string]*method{}
for _, m := range s.Methods {
m := m
s.MethodSet[m.Name] = m
}
}
buf := new(bytes.Buffer)
tmpl, err := template.New("http").Parse(strings.TrimSpace(tpl))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return buf.String()
}
// InterfaceName service interface name
func (s *service) InterfaceName() string {
return s.Name + "HTTPServer"
}
type method struct {
Name string // SayHello
Num int // 一个 rpc 方法可以对应多个 http 请求
Request string // SayHelloReq
Reply string // SayHelloResp
// http_rule
Path string // 路由
Method string // HTTP Method
Body string
ResponseBody string
}
// HandlerName for gin handler name
func (m *method) HandlerName() string {
return fmt.Sprintf("%s_%d", m.Name, m.Num)
}
// HasPathParams 是否包含路由参数
func (m *method) HasPathParams() bool {
paths := strings.Split(m.Path, "/")
for _, p := range paths {
if len(p) > 0 && (p[0] == '{' && p[len(p)-1] == '}' || p[0] == ':') {
return true
}
}
return false
}
// initPathParams 转换参数路由 {xx} --> :xx
func (m *method) initPathParams() {
paths := strings.Split(m.Path, "/")
for i, p := range paths {
if len(p) > 0 && (p[0] == '{' && p[len(p)-1] == '}' || p[0] == ':') {
paths[i] = ":" + p[1:len(p)-1]
}
}
m.Path = strings.Join(paths, "/")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。