github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/service/gen/service_generator.go (about) 1 package gen 2 3 import ( 4 "go/build" 5 "path" 6 7 "github.com/johnnyeven/libtools/codegen" 8 ) 9 10 type ServiceGenerator struct { 11 PathName string 12 ServiceName string 13 DatabaseName string 14 Root string 15 } 16 17 func (g *ServiceGenerator) Load(cwd string) { 18 } 19 20 func (g *ServiceGenerator) Pick() { 21 } 22 23 func (g *ServiceGenerator) Output(cwd string) codegen.Outputs { 24 outputs := codegen.Outputs{} 25 26 codegen.NewGenFile("main", path.Join(g.ServiceName, "doc.go")). 27 WithData(g). 28 OutputTo(outputs) 29 30 outputs.WriteFiles() 31 32 pkg, _ := build.ImportDir(path.Join(cwd, g.ServiceName), build.ImportComment) 33 34 g.Root = pkg.ImportPath 35 36 if g.DatabaseName != "" { 37 codegen.NewGenFile("database", path.Join(g.ServiceName, "database/db.go")). 38 WithData(g). 39 Block(` 40 var DB{{ .ExposeVar .Data.DatabaseName }} = {{ ( .PureUse "github.com/johnnyeven/libtools/sqlx" )}}.NewDatabase("{{ .Data.DatabaseName }}") 41 `, 42 ). 43 OutputTo(outputs) 44 45 outputs.WriteFiles() 46 } 47 48 codegen.NewGenFile("global", path.Join(g.ServiceName, "global/config.go")). 49 WithData(g). 50 Block(` 51 func init() { 52 {{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.SetServiceName("{{ .Data.ServiceName }}") 53 {{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.ConfP(&Config) 54 55 {{ if .Data.DatabaseName }} 56 {{ .PureUse .Data.Root "database" }}.DB{{ .ExposeVar .Data.DatabaseName }}.MustMigrateTo(Config.MasterDB.Get(), !{{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.AutoMigrate) 57 {{ end }} 58 } 59 60 var Config = struct { 61 Log *{{ ( .PureUse "github.com/johnnyeven/libtools/log" ) }}.Log 62 Server {{ ( .PureUse "github.com/johnnyeven/libtools/courier/transport_http" ) }}.ServeHTTP 63 {{ if .Data.DatabaseName }} 64 MasterDB *{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL 65 SlaveDB *{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL 66 {{ end }} 67 }{ 68 Log: &{{ ( .PureUse "github.com/johnnyeven/libtools/log" ) }}.Log{ 69 Level: "DEBUG", 70 }, 71 Server: {{ ( .PureUse "github.com/johnnyeven/libtools/courier/transport_http" ) }}.ServeHTTP{ 72 WithCORS: true, 73 Port: 8000, 74 }, 75 {{ if .Data.DatabaseName }} 76 MasterDB: &{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL{ 77 Name: "{{ .Data.DatabaseName }}", 78 Port: 33306, 79 User: "root", 80 Password: "root", 81 Host: "....", 82 }, 83 SlaveDB: &{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL{ 84 Name: "{{ .Data.DatabaseName }}-readonly", 85 Port: 33306, 86 User: "root", 87 Password: "root", 88 Host: "....", 89 }, 90 {{ end }} 91 } 92 `, 93 ).OutputTo(outputs) 94 95 codegen.NewGenFile("types", path.Join(g.ServiceName, "constants/types/doc.go")).WithData(g).Block(` 96 // Defined enum types here 97 `).OutputTo(outputs) 98 99 codegen.NewGenFile("modules", path.Join(g.ServiceName, "modules/doc.go")).WithData(g).Block(` 100 // Defined sub modules here 101 `).OutputTo(outputs) 102 103 codegen.NewGenFile("errors", path.Join(g.ServiceName, "constants/errors/status_err_codes.go")). 104 WithData(g). 105 Block(` 106 //go:generate libtools gen error 107 const ServiceStatusErrorCode = 0 * 1e3 // todo rename this 108 109 const ( 110 // 请求参数错误 111 BadRequest {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusBadRequest*1e6 + ServiceStatusErrorCode + iota 112 ) 113 114 const ( 115 // 未找到 116 NotFound {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusNotFound*1e6 + ServiceStatusErrorCode + iota 117 ) 118 119 const ( 120 // @errTalk 未授权 121 Unauthorized {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusUnauthorized*1e6 + ServiceStatusErrorCode + iota 122 ) 123 124 const ( 125 // @errTalk 操作冲突 126 Conflict {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusConflict*1e6 + ServiceStatusErrorCode + iota 127 ) 128 129 const ( 130 // @errTalk 不允许操作 131 Forbidden {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusForbidden*1e6 + ServiceStatusErrorCode + iota 132 ) 133 134 const ( 135 // 内部处理错误 136 InternalError {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusInternalServerError*1e6 + ServiceStatusErrorCode + iota 137 ) 138 `, 139 ). 140 OutputTo(outputs) 141 142 codegen.NewGenFile("routes", path.Join(g.ServiceName, "routes/root.go")). 143 WithData(g). 144 Block(` 145 var RootRouter = {{ .PureUse "github.com/johnnyeven/libtools/courier" }}.NewRouter(GroupRoot{}) 146 147 func init() { 148 RootRouter.Register({{ .PureUse "github.com/johnnyeven/libtools/courier/swagger" }}.SwaggerRouter) 149 } 150 151 type GroupRoot struct { 152 courier.EmptyOperator 153 } 154 155 func (root GroupRoot) Path() string { 156 return "/{{ .Data.PathName }}" 157 } 158 `, 159 ). 160 OutputTo(outputs) 161 162 outputs.WriteFiles() 163 164 codegen.NewGenFile("main", path.Join(g.ServiceName, "main.go")). 165 WithData(g). 166 Block(` 167 func main() { 168 {{( .PureUse "github.com/johnnyeven/libtools/servicex" )}}.Execute() 169 {{( .PureUse .Data.Root "global" )}}.Config.Server.Serve({{ ( .PureUse .Data.Root "routes" ) }}.RootRouter) 170 } 171 `, 172 ). 173 OutputTo(outputs) 174 175 return outputs 176 }