Stay away from tedious CRUD and cherish life π.
sparrow is library to generate out-of-box http project and provide tools to generate sql and api, which saves your life πΌ.
- api generation
- swagger api doc generation
- sql generation
- auto migrate
- jwt authorization
- validation
- log rotation
- dependency injection
- i18n
- prometheus & grafana
go install github.com/rickywei/sparrow/cmd/sparrow@latest
cd dir
sparrow new yourProjectName
yourProjectName
could be like github.com/rickywei/sparrow
or sparrow
.
Both of these two format will generate the project folder sparrow in dir(i.e. dir/sparrow).
But they have different module path in go.mod and import prefix corresponding.
Your Project will looks like following.
βββ Dockerfile
βββ Makefile
βββ README.md
βββ active.en.toml
βββ active.zh.toml
βββ api
β βββ api.go
β βββ v1.go
βββ app
β βββ app.go
β βββ wire.go
β βββ wire_gen.go
βββ cache
β βββ cache.go
βββ cmd
β βββ main.go
βββ conf
β βββ conf.go
βββ conf.yaml
βββ dao
β βββ dao.go
β βββ gen.go
β βββ user.gen.go
βββ docker-compose.yaml
βββ docs
β βββ docs.go
β βββ swagger.json
β βββ swagger.yaml
βββ go.mod
βββ go.sum
βββ handler
β βββ errno.go
β βββ handler.go
β βββ resp.go
β βββ user.go
βββ logger
β βββ logger.go
βββ middleware
β βββ auth.go
β βββ i18n.go
β βββ logger.go
β βββ recover.go
βββ po
β βββ sql
β β βββ db.sql
β β βββ table.sql
β βββ user.gen.go
βββ tool
β βββ apigen.go
β βββ gormgen.go
βββ translate.zh.toml
βββ vo
βββ user.go
cd yourProjectName
docker-compose up
The app should be started and you can check api here http://localhost:8000/swagger/index.html
Uncomment the todo table in ./po/sql/table.sql(also you can define your own model) then run following.
cd yourProjectName
make gorm
# generates Todo struct in po/todo.gen.go and sql operations in dao/todo.gen.go
Add Todo to auto migrate.
// dao/dao.go
var (
Q *Query
db *gorm.DB
models = []any{po.User{}, po.Todo{}} // TODO
)
cd yourProjectName
make api name=todo
# generates TodoHandler in handler/todo.go
# generates Todo in vo/todo.go. You should modify this to adapt your handler, reference vo/user.go
Then you should add the handler to api and routes like userHandler.
// api/api.go
type API struct {
engine *gin.Engine
srv *http.Server
ctx context.Context
cancel context.CancelFunc
userHandler *handler.UserHandler
todo *handler.todoHandler // TODO
}
func NewApi(userHandler *handler.UserHandler, todo *handler.TodoHandler/*TODO*/) *API {
// ...
api := &API{
engine: engine,
srv: srv,
ctx: ctx,
cancel: cancel,
userHandler: userHandler,
todoHandler: todoHandler, // TODO
}
}
// api/v1.go
func init() {
routes = append(routes, func(api *API) {
v1 := api.engine.Group("api").Group("v1")
{
v1.POST("user", api.userHandler.Create)
// ...
}
// TODO
{
v1.POST("todo", api.todoHandler.Create)
// ...
}
})
}
// handler/handler.go
// ...
var (
ProviderSet = wire.NewSet(NewUserHandler, NewTodoHandler/*TODO*/)
)
You need to register handler in api and define your routes. (this part may be automatic in the feature)
cd yourProjectName
make swag
cd yourProjectName
make wire
make wrie
will handle the dependency of each handler with api and do DI for you in app/wire_gen.go
- keep previous code when generating vo and handler rather than override
- modify api and routes automatically after api generation
- support prometheus and grafna
- i18n example
- zh README.md
- Fork the project
- Fix open issues or request new features
Give a βοΈ if this project helped you!
This project is MIT licensed.
Copyright (c) 2023 RickyWei