李伟乐 1 year ago
parent 51ca38703e
commit dea8e274e9
  1. 13
      Makefile
  2. 2
      api/user_http.pb.go
  3. 29
      cmd/gin-kit/main.go
  4. 3
      protogen/protogen.go

@ -1,15 +1,16 @@
kit:
go build -o ~/go/bin/protoc-gen-kit cmd/kit/main.go
gin-kit:
go build -o ~/go/bin/protoc-gen-gin-kit cmd/gin-kit/main.go
ts:
go build -o ~/go/bin/protoc-gen-ts cmd/ts/main.go
all:
make auth
make kit
make gin-kit
make ts
make gen
kit:
go build -o ~/go/bin/protoc-gen-kit cmd/kit/main.go
gin-kit:
go build -o ~/go/bin/protoc-gen-gin-kit cmd/gin-kit/main.go
ts:
go build -o ~/go/bin/protoc-gen-ts cmd/ts/main.go
testts:
make ts
gen:

@ -2,8 +2,8 @@ package user
import (
"context"
"git.diulo.com/mogfee/kit/middleware/jwt"
"git.diulo.com/mogfee/kit/transport/http"
"git.diulo.com/mogfee/kit/middleware/jwt"
)
type UserHTTPServer interface {

@ -79,7 +79,8 @@ func (u *Kit) Generate(plugin *protogen.Plugin) error {
u.genPost(serverName, t, m)
case protogen2.METHOD_DELETE:
u.genDelete(serverName, t, m)
case protogen2.METHOD_PUT:
u.genPut(serverName, t, m)
}
}
}
@ -162,3 +163,29 @@ func (u *Kit) genDelete(serverName string, t *protogen.GeneratedFile, m *protoge
}`)
t.P("}")
}
func (u *Kit) genPut(serverName string, t *protogen.GeneratedFile, m *protogen.Method) {
t.P("func http", m.GoName, "Handler(srv ", serverName, "Server,m ...middleware.Middleware)func(c *gin.Context){")
t.P(`return func(c *gin.Context) {
var post `, m.Input.GoIdent.GoName, `
resp := response.New(c)
if err := resp.BindJSON(&post); err != nil {
resp.Error(err)
return
}
h := func(ctx context.Context, a any) (any, error) {
return srv.`, m.GoName, `(ctx, a.(*`, m.Input.GoIdent.GoName, `))
}
out, err := middleware.HttpMiddleware(c, h, m...)(c, &post)
if err != nil {
resp.Error(err)
} else {
if v, ok := out.(*`, m.Output.GoIdent.GoName, `); ok {
resp.Success(v)
} else {
resp.Error(errors.InternalServer("RESULT_TYPE_ERROR", "`, m.Output.GoIdent.GoName, `"))
}
}
}`)
t.P("}")
}

@ -12,6 +12,7 @@ const (
METHOD_GET = "GET"
METHOD_POST = "POST"
METHOD_DELETE = "DELETE"
METHOD_PUT = "PUT"
)
func GetProtoMethod(m *protogen.Method) (method string, path string) {
@ -26,6 +27,8 @@ func GetProtoMethod(m *protogen.Method) (method string, path string) {
return METHOD_POST, vvv.Post
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Delete); ok {
return METHOD_DELETE, vvv.Delete
} else if vvv, ok := vv.Pattern.(*annotations.HttpRule_Put); ok {
return METHOD_PUT, vvv.Put
}
}
}

Loading…
Cancel
Save