Skip to content

🐦sparrow is library to generate out-of-box http project and provide tools to generate sql and api, which saves your life πŸ‘Ό

License

Notifications You must be signed in to change notification settings

rickywei/sparrow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐦SPARROW

License Go Version Go report

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 πŸ‘Ό.

πŸ’₯Features

  • api generation
  • swagger api doc generation
  • sql generation
  • auto migrate
  • jwt authorization
  • validation
  • log rotation
  • dependency injection
  • i18n
  • prometheus & grafana

πŸš€Install

go install github.com/rickywei/sparrow/cmd/sparrow@latest

πŸ“šTutorial

πŸƒQuick Start

Generate a new project by sparrow

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

Start app by docker-compose

cd yourProjectName
docker-compose up

The app should be started and you can check api here http://localhost:8000/swagger/index.html

swagger

πŸ’‘Usage

Generate db operation and struct

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
)

Generate api

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)

generate swagger doc

cd yourProjectName
make swag

dependency inject

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

🎯TODO

  • 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

🀝 Contributing

🦸Contributors

Contributors

πŸ‘Show your support

Give a ⭐️ if this project helped you!

πŸ“ License

This project is MIT licensed.

Copyright (c) 2023 RickyWei

About

🐦sparrow is library to generate out-of-box http project and provide tools to generate sql and api, which saves your life πŸ‘Ό

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages