开箱即用的GO后台管理系统 Kratos Admin - 代码生成工具集

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,182元/月
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
云原生网关 MSE Higress,422元/月
简介: Kratos Admin 是一款开箱即用的 Go 后台管理系统,配套代码生成工具集(cfgexp、sql2orm、sql2proto、sql2kratos),支持配置导出、数据库转 ORM、Protobuf 及 Kratos 微服务代码生成,助力高效开发。

开箱即用的GO后台管理系统 Kratos Admin - 代码生成工具集

我们为go-kratos-admin这个项目打造了一个代码生成工具集。

  • cfgexp 用于将服务本地配置导入到远程配置系统,支持:Etd、Consul、Nacos……
  • sql2orm 用于把数据库的表结构导入,并且生成为ORM代码,支持:ENT、GORM……
  • sql2proto 用于把数据的表结构导入,并且生成gRPC、REST的Protobuf代码。
  • sql2kratos 用于把数据的表结构导入,并且生成一整套的Kratos服务代码。

项目代码地址:

cfgexp

该工具将本地配置文件导出到Consul或Etcd等远程配置系统,从而更轻松地管理分布式系统中的配置。

现在已经支持的远程配置系统有:

  • Etcd
  • Consul
  • Nacos

如何安装

go install github.com/tx7do/kratos-cli/config-exporter/cmd/cfgexp@latest

如何使用

Config Exporter is a tool to export configuration from remote services like Consul or Etcd to local files.

Usage:
  cfgexp [flags]

Flags:
  -a, --addr string    remote config service address (default "127.0.0.1:8500")
  -e, --env string     environment name, like dev, test, prod, etc. (default "dev")
  -g, --group string   group name, this name is used to key prefix in remote config service (default "DEFAULT_GROUP")
  -h, --help           help for cfgexp
  -n, --ns string      namespace ID, used for Nacos (default "public")
  -p, --proj string    project name, this name is used to key prefix in remote config service
  -r, --root string    project root dir (default "./")
  -t, --type string    remote config service name (consul, etcd, etc.) (default "consul")

示例

Etcd

cfgexp \
    -t "etcd" \
    -a "localhost:2379" \
    -p "kratos_admin"

consul

cfgexp \
    -t "consul" \
    -a "localhost:8500" \
    -p "kratos_admin"

Nacos

cfgexp \
    -t "nacos" \
    -a "localhost:8848" \
    -p "kratos_admin" \
    -n "public" \
    -e "dev" \
    -g "DEFAULT_GROUP"

sql2orm

该工具导入 SQL 数据库的表结构,并生成用于 Kratos 微服务的 ORM代码。

支持的ORM:

  • ent
  • GORM

如何安装

go install github.com/tx7do/kratos-cli/sql-orm/cmd/sql2orm@latest

如何使用

sql2orm is a tool to generate ORM code from SQL database schemas.

Usage:
  sql2orm [flags]

Flags:
  -d, --dao-path string          output path for DAO code (for gorm) (default "./daos/")
  -v, --drv string               Database driver name to use (mysql, postgres, sqlite...) (default "mysql")
  -n, --dsn string               Data source name (connection information), for example:
                                 "mysql://user:pass@tcp(localhost:3306)/dbname"
                                 "postgres://user:pass@host:port/dbname"
  -e, --exclude-tables strings   comma-separated list of tables to exclude
  -h, --help                     help for sql2orm
  -o, --orm string               ORM type to use (ent, gorm) (default "ent")
  -s, --schema-path string       output path for schema (default "./ent/schema/")
  -t, --tables strings           comma-separated list of tables to inspect (all if empty)

示例

Ent

sql2orm \
  --orm "ent" \
  --dsn "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  --schema-path "./ent/schema"

GORM

sql2orm \
  --orm "gorm" \
  --drv "postgres" \
  --dsn "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  --schema-path "./daos/models" \
  --dao-path "./daos/"

sql2proto

该工具导入 SQL 数据库的表结构,并生成用于 Kratos 微服务的 Protobuf 代码。

如何安装

go install github.com/tx7do/kratos-cli/sql-proto/cmd/sql2proto@latest

如何使用

sql2proto is a tool to import SQL database schema and generate Protobuf code.

Usage:
  sql2proto [flags]

Flags:
  -n, --dsn string          Data source name (connection information), for example:
                            "mysql://user:pass@tcp(localhost:3306)/dbname"
                            "postgres://user:pass@host:port/dbname"
  -e, --excludes strings    comma-separated list of tables to exclude
  -h, --help                help for sql2proto
  -i, --includes strings    comma-separated list of tables to inspect (all if empty)
  -m, --module string       module name for the generated code, e.g., 'admin' (default "admin")
  -o, --output string       output path for protobuf schema files (default "./api/protos/")
  -s, --src-module string   Source module name, for REST service generate, e.g., "admin" (default "user")
  -t, --type string         generate RPC service type, "rest" for REST service, "grpc" for gRPC service (default "grpc")
  -v, --version string      Version of the module, e.g., 'v1' (default "v1")

示例

生成gRPC服务的Proto:

sql2proto \
  -n "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  -o "./api/protos" \
  -t "grpc" \
  -m "user"

生成REST服务的Proto:

sql2proto \
  -n "mysql://root:pass@localhost:3306/test" \
  -o "./api/protos" \
  -t "rest" \
  -m "admin" \
  -s "user"

sql2kratos

该工具导入 SQL 数据库的表结构,并生成 Kratos 微服务代码。

如何安装

go install github.com/tx7do/kratos-cli/sql-kratos/cmd/sql2kratos@latest

如何使用

sql2kratos imports the SQL database schemas and generates Kratos microservice code.

Usage:
  sql2kratos [flags]

Flags:
  -n, --dsn string          Data source name (connection information), for example:
                            "mysql://user:pass@tcp(localhost:3306)/dbname"
                            "postgres://user:pass@host:port/dbname"
  -e, --excludes strings    comma-separated list of tables to exclude
  -l, --gen-data            enable generate data package code (default true)
  -k, --gen-main            enable generate main package code (default true)
  -z, --gen-orm             enable generate ORM code (default true)
  -q, --gen-proto           enable generate protobuf schema files (default true)
  -w, --gen-srv             enable generate server package code (default true)
  -a, --gen-svc             enable generate service package code (default true)
  -h, --help                help for sql2kratos
  -i, --includes strings    comma-separated list of tables to inspect (all if empty)
  -m, --module string       Target module name for the generated code, e.g., 'admin' (default "admin")
  -r, --orm string          ORM type to use (ent, gorm) (default "ent")
  -o, --output string       output path for protobuf schema files (default "./api/protos/")
  -p, --project string      Project name for the generated code, e.g., 'kratos-admin' (default "kratos-admin")
  -x, --repo                use repository pattern (default true)
  -g, --servers strings     comma-separated list of servers to generate, e.g., "grpc,rest" (default [grpc])
  -c, --service string      Service name for the generated code, e.g., 'user' (default "user")
  -s, --src-module string   Source module name, for REST service generate, e.g., "admin" (default "user")
  -v, --version string      Version of the module, e.g., 'v1' (default "v1")

示例

gRPC服务代码:

sql2kratos \
  -p "kratos-admin" \
  -n "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  -r "ent" \
  -o "." \
  -m "user" \
  -c "user" \
  -g "grpc"

REST服务代码:

sql2kratos \
  -p "kratos-admin" \
  -n "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  -r "ent" \
  -o "." \
  -s "user" \
  -m "admin" \
  -c "admin" \
  -g "rest" \
  -x=false \
  -z=false \
  -l=false

项目代码

目录
相关文章
|
关系型数据库 MySQL Go
Go语言微服务框架 - 8.Gormer迭代-定制专属的ORM代码生成工具
我们对比一下GORM库提供的`gorm.Model`,它在新增、修改时,会自动修改对应的时间,这个可以帮我们减少很多重复性的代码编写。这里,我就针对现有的gormer工具做一个示例性的迭代。
206 0
|
2月前
|
监控 前端开发 数据可视化
Github 12.3kstar, 3分钟起步做中后台?Go+Vue 脚手架,把权限、代码生成、RBAC 都封装好了
Go-admin 是基于 Gin + Vue 的中后台脚手架,集成 Casbin RBAC 权限、JWT 鉴权、GORM 数据库操作与 Swagger 文档,内置用户、角色、菜单等管理模块。提供代码生成器与表单构建器,支持多租户与多前端框架(Element UI/Arco/Ant Design),3 分钟快速搭建企业级后台,助力高效交付。
192 4
|
关系型数据库 MySQL Go
Go - 代码生成工具
Go - 代码生成工具
119 3
|
JSON 编解码 中间件
go-zero代码生成器助你高效开发
go-zero代码生成器助你高效开发
|
Kubernetes Shell Go
Go编程模式 - 7-代码生成
良好的命名能体现出其价值。尤其是在错误码的处理上,无需再去查询错误码对应的错误内容,直接可以通过命名了解。
156 0
|
Go API 微服务
go-zero微服务框架代码生成神器goctl原理分析(一)
go-zero微服务框架代码生成神器goctl原理分析(一)
|
1月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
130 1
|
3月前
|
Cloud Native 安全 Java
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
278 1
|
3月前
|
Cloud Native Go API
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
348 0
|
3月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
222 0

热门文章

最新文章