Skip to content

Commit d1a8d87

Browse files
authored
✨ refator and ready for develop usage (#15)
* 🗃️ improve rdb and repo accessibility * ♻️ optimize repo and http generate module * remove useless code
1 parent 3e19743 commit d1a8d87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+580
-271
lines changed

config/template.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ const (
3939
RepoUpdateTmpl = "repo_update.tmpl"
4040
RepoInsertTmpl = "repo_insert.tmpl"
4141

42-
HttpHandlerTmpl = "http_handler.tmpl"
43-
HttpHandlerRegisterTmpl = "http_handler_register.tmpl"
44-
HttpProtocolTmpl = "http_protocol.tmpl"
45-
HttpProtocolMethodTmpl = "http_protocol_method.tmpl"
46-
HttpProtocolTierTmpl = "http_protocol_tier.tmpl"
47-
HttpParamImplTmpl = "http_param_impl.tmpl"
42+
ApiTmpl = "api.tmpl"
43+
ApiCustomTmpl = "api_custom.tmpl"
44+
ApiMethodTmpl = "api_method.tmpl"
45+
HandlersTmpl = "handlers.tmpl"
46+
HandlersCustomTmpl = "handlers_custom.tmpl"
47+
HandlersRouterTmpl = "handlers_router.tmpl"
4848
)
4949

5050
var (
@@ -83,12 +83,12 @@ var templateNames = []string{
8383
RdbUpdateTmpl,
8484
RdbInsertTmpl,
8585

86-
HttpHandlerTmpl,
87-
HttpHandlerRegisterTmpl,
88-
HttpProtocolTmpl,
89-
HttpProtocolMethodTmpl,
90-
HttpProtocolTierTmpl,
91-
HttpParamImplTmpl,
86+
ApiTmpl,
87+
ApiCustomTmpl,
88+
ApiMethodTmpl,
89+
HandlersTmpl,
90+
HandlersCustomTmpl,
91+
HandlersRouterTmpl,
9292
}
9393

9494
func init() {

config/templates/api.tmpl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Code generated by protoc-gen-cato. DO NOT EDIT.
2+
3+
package {{.ServicePackage}}
4+
5+
import (
6+
{{- range .ImportPackages}}
7+
{{.}}
8+
{{- end}}
9+
)
10+
11+
type {{ .ServiceName }}Service interface {
12+
{{- range .Methods}}
13+
{{.}}
14+
{{- end}}
15+
}
16+
17+
var (
18+
apiService = New{{ .ServiceName }}Service()
19+
)
20+
21+
22+
func New{{ .ServiceName }}Service() {{ .ServiceName }}Service {
23+
ss := new({{ .ServiceNameInner }}Service)
24+
ss.init()
25+
return ss
26+
}

config/templates/api_custom.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package {{.ServicePackage}}
2+
3+
import (
4+
{{- range .ImportPackages}}
5+
{{.}}
6+
{{- end}}
7+
)
8+
9+
type {{ .ServiceNameInner }}Service struct {}
10+
11+
func (si *{{.ServiceNameInner}}Service) init() {
12+
13+
}
14+
15+
{{- range .Methods}}
16+
func (si *{{ $.ServiceNameInner }}Service ) {{.}} { panic("impl me")}
17+
{{- end}}
File renamed without changes.

config/templates/handlers.tmpl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Code generated by protoc-gen-cato. DO NOT EDIT.
2+
3+
package {{.ServicePackage}}
4+
5+
import (
6+
"net/http"
7+
8+
"github.com/ncuhome/cato/core/httpc"
9+
)
10+
11+
12+
type {{ .ServiceName }}HttpService interface {
13+
httpc.HttpService
14+
{{- range .HttpMethods}}
15+
{{.}}() http.HandlerFunc
16+
{{- end}}
17+
}
18+
19+
var (
20+
httpService {{ .ServiceName }}HttpService = New{{ .ServiceName }}HttpService()
21+
)
22+
23+
24+
func New{{ .ServiceName }}HttpService() {{ .ServiceName }}HttpService {
25+
hs := new({{ .ServiceNameInner }}HttpService)
26+
hs.init()
27+
{{- range .HttpRouters}}
28+
hs.register({{.}})
29+
{{- end}}
30+
return hs
31+
}
32+
33+
34+
type {{ .ServiceNameInner }}HttpService struct {
35+
funcs map[string]map[string]http.HandlerFunc
36+
}
37+
38+
func (hs *{{ .ServiceNameInner }}HttpService) BaseUrl() string {
39+
return "{{.RouterBasePath}}"
40+
}
41+
42+
func (hs *{{ .ServiceNameInner}}HttpService) Urls() map[string][]string {
43+
mm := make(map[string][]string)
44+
for method, data := range hs.funcs {
45+
for uri := range data {
46+
mm[method] = append(mm[method], uri)
47+
}
48+
}
49+
return mm
50+
}
51+
52+
func (hs *{{ .ServiceNameInner}}HttpService) Handlers(method, uri string) (http.HandlerFunc, bool) {
53+
collections, ok := hs.funcs[method]
54+
if !ok {
55+
return nil, false
56+
}
57+
f, ok := collections[uri]
58+
return f, ok
59+
}
60+
61+
62+
func (hs *{{ .ServiceNameInner}}HttpService) register(method, uri string, handlerFunc http.HandlerFunc) {
63+
if hs.funcs == nil {
64+
hs.funcs = make(map[string]map[string]http.HandlerFunc)
65+
}
66+
_, ok := hs.funcs[method]
67+
if !ok {
68+
hs.funcs[method] = make(map[string]http.HandlerFunc)
69+
}
70+
hs.funcs[method][uri] = handlerFunc
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package {{.ServicePackage}}
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/ncuhome/cato/core/httpc"
8+
)
9+
10+
func (hs *{{ .ServiceNameInner }}HttpService) init() {
11+
12+
}
13+
14+
15+
{{- range .HttpMethods}}
16+
func (hs *{{ $.ServiceNameInner }}HttpService) {{.}}() http.HandlerFunc { panic("impl name") }
17+
{{- end}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{.Func}}() http.HandlerFunc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"{{.Method}}", "{{.Uri}}", hs.{{.Func}}()

config/templates/http_handler.tmpl

Lines changed: 0 additions & 41 deletions
This file was deleted.

config/templates/http_handler_register.tmpl

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)