github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/service/gen/service_generator.go (about)

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