diff --git a/makefile b/makefile index 822e612..b09339a 100644 --- a/makefile +++ b/makefile @@ -26,3 +26,6 @@ gen-struct: .PHONY: gen-protobuf gen-protobuf: @python3 ./shell/gen_protobuf.py + +gen-middleware: + @python3 ./shell/gen_middleware.py diff --git a/middleware/config/middle.gen.go b/middleware/config/middle.gen.go new file mode 100644 index 0000000..83496f4 --- /dev/null +++ b/middleware/config/middle.gen.go @@ -0,0 +1,21 @@ +// Code generated by gen-middleware. DO NOT EDIT. +// make gen-middleware generated + +package config + +import ( + "gitee.com/timedb/wheatCache/middleware" + + logMiddle "gitee.com/timedb/wheatCache/middleware/log-middle" + mapKey "gitee.com/timedb/wheatCache/middleware/map-key" +) + + +func GetMiddlewareMap() map[string]middleware.MiddlewareInterface { + + return map[string]middleware.MiddlewareInterface{ + + "logMiddle":logMiddle.NewMiddleware(), + "mapKey":mapKey.NewMiddleware(), + } +} \ No newline at end of file diff --git a/middleware/config/middle.template b/middleware/config/middle.template new file mode 100644 index 0000000..4f8986f --- /dev/null +++ b/middleware/config/middle.template @@ -0,0 +1,21 @@ +// Code generated by gen-middleware. DO NOT EDIT. +// make gen-middleware generated + +package config + +import ( + "gitee.com/timedb/wheatCache/middleware" + {%for dir in dirs %} + {{dir[0]}} "gitee.com/timedb/wheatCache/middleware/{{dir[1]}}" + {%- endfor%} +) + + +func GetMiddlewareMap() map[string]middleware.MiddlewareInterface { + + return map[string]middleware.MiddlewareInterface{ + {%for dir in dirs %} + "{{dir[0]}}":{{dir[0]}}.NewMiddleware(), + {%- endfor%} + } +} \ No newline at end of file diff --git a/middleware/config/middle_conf.go b/middleware/config/middle_conf.go deleted file mode 100644 index 3d78198..0000000 --- a/middleware/config/middle_conf.go +++ /dev/null @@ -1,14 +0,0 @@ -package config - -import ( - "gitee.com/timedb/wheatCache/middleware" - logMiddle "gitee.com/timedb/wheatCache/middleware/log-middle" -) - -func GetMiddlewareMap() map[string]middleware.MiddlewareInterface { - logMiddlewareCase := logMiddle.NewMiddleware() - - return map[string]middleware.MiddlewareInterface{ - "logMiddlewareCase": logMiddlewareCase, - } -} diff --git a/middleware/log-middle/kbl.go b/middleware/log-middle/kbl.go new file mode 100644 index 0000000..4f95f3c --- /dev/null +++ b/middleware/log-middle/kbl.go @@ -0,0 +1,5 @@ +package log_middle + +func wait() { + +} diff --git a/middleware/log-middle/middleware.go b/middleware/log-middle/middleware.go index a2d4cd0..55478bc 100644 --- a/middleware/log-middle/middleware.go +++ b/middleware/log-middle/middleware.go @@ -2,6 +2,7 @@ package log_middle import ( "fmt" + "gitee.com/timedb/wheatCache/middleware" ) @@ -9,7 +10,6 @@ type A struct { } func (i *A) Init() { - } func (i *A) Exat(interface{}) (interface{}, error) { diff --git a/middleware/map-key/middleware.go b/middleware/map-key/middleware.go new file mode 100644 index 0000000..1fffda6 --- /dev/null +++ b/middleware/map-key/middleware.go @@ -0,0 +1,22 @@ +package log_middle + +import ( + "fmt" + + "gitee.com/timedb/wheatCache/middleware" +) + +type ABB struct { +} + +func (i *ABB) Init() { +} + +func (i *ABB) Exat(interface{}) (interface{}, error) { + fmt.Println(1) + return nil, nil +} + +func NewMiddleware() middleware.MiddlewareInterface { + return &ABB{} +} diff --git a/shell/gen_middleware.py b/shell/gen_middleware.py new file mode 100644 index 0000000..976e312 --- /dev/null +++ b/shell/gen_middleware.py @@ -0,0 +1,67 @@ +import os +from jinja2 import Template + + +sysPath = os.getcwd() +tempPath = f"{sysPath}/middleware/config" +structurePath = f"{sysPath}/pkg/structure" +protobufPath = f"{sysPath}/protobuf" +storagePath = f"{sysPath}/storage" +middlePath = f"{sysPath}/middleware" + +def go_fmt(path: str): + os.system(f"go fmt {path}") + + +def to_camel(val: str) -> str: + camel ="" + val = val.split('_') + for k in range(0,len(val),1): + if k == 0: + camel = camel+str(val[k]) + else: + camel = camel+str(val[k].capitalize()) + return camel + +# 得到各个中间件的文件名 +def getMiddleName(path:str)->list: + dic = [] + dataname = os.listdir(path) + + for i in dataname : + if i != 'config' and i != 'define.go': + dic.append(i) + return dic + +#读取模板 +def load_template(name: str) -> str: + with open(f"{tempPath}/{name}", "r", encoding="utf-8") as fp: + return fp.read() + +# 获取各个中间件的目录 +def getMiddleDir(keys:list)->list: + dirs = [] + middleware = [] + for middlename in keys: + middleware=[] + name = to_camel(middlename.replace("-","_")) + dir = middlename + middleware.append(name) + middleware.append(dir) + dirs.append(middleware) + return dirs + + + +if __name__ == "__main__": + + tem_text = load_template("middle.template") + temp = Template(tem_text) + keys = getMiddleName(middlePath) + dirs = getMiddleDir(keys) + text = temp.render(dirs=dirs,) + temp_path = f"{middlePath}/config/middle.gen.go" + with open(temp_path,'w',encoding='utf-8') as f: + f.write(text) + +