Gojira is a cli tool to create clean architecture app for you including gin-gonic, bcrypt and jwt.
- Creates Clean Architecture project for you
- Clean Architecture Folder Structure (https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
- Gin Gonic (https://github.com/gin-gonic/gin)
- Swagger (https://github.com/swaggo/gin-swagger)
- Jwt (https://github.com/dgrijalva/jwt-go)
- Bcrypt (https://golang.org/x/crypto/bcrypt)
- Async - Async functions
- Auto add swagger for your endpoint
- Modules - Auto generate module with crud flow
- Database Service - Auto generate db service client
- Mysql
- Gorm
- Prisma
- Example tasks api
- Testing (Auto generate test example when creating a new modules)
Gojira requires Go v1.11+ to run.
Install the dependencies.
go get github.com/illud/gojira
Or
go install github.com/illud/gojira@latest
In your terminal type to see all avaible commands:
gojira
To create a new gin-gonic with clean architecture project(This includes a crud example with the name of Tasks):
[x] New project
[ ] Module
[ ] Module with crud
[ ] DB service
Enter Project Name: yourprojectname
To create a new module with simple example flow: please use snake_case when the module name consist of two or more words
[ ] New project
[x] Module
[ ] Module with crud
[ ] DB service
Enter Module Name: your_module_name
To create a new module with crud flow: please use snake_case when the module name consist of two or more words
[ ] New project
[ ] Module
[x] Module with crud
[ ] DB service
Enter Module Name: your_module_name
To create a new db service client with Mysql, Gorm or Prisma:
[ ] New project
[ ] Module
[ ] Module with crud
[x] DB service
Mysql - to learn more visit (https://github.com/go-sql-driver/mysql)
Enter DB(mysql, gorm or prisma) Name: mysql
Gorm - to learn more visit (https://github.com/jinzhu/gorm)
Enter DB(mysql, gorm or prisma) Name: gorm
Prisma - to learn more visit (https://github.com/prisma/prisma-client-go)
Enter DB(mysql, gorm or prisma) Name: prisma
db "github.com/yourProjectName/infraestructure/databases"
Example for Mysql:
// Insert new tasks
res, err := db.Client().Exec("INSERT INTO tasks VALUES(DEFAULT, 'Title', 'Desc')")
if err != nil {
fmt.Println("ERROR: ", err)
}
fmt.Println(res)
To learn more visit (https://github.com/go-sql-driver/mysql)
Example for Gorm:
// Insert new tasks
err := db.Client().Save(&tasksEntity.Task{
Title: "TEST",
Description: "This is a description",
})
if err != nil {
fmt.Println(err)
}
To learn more visit (https://github.com/jinzhu/gorm)
For prisma import:
dbClient "github.com/yourProjectName/infraestructure/databases"
"github.com/yourProjectName/infraestructure/databases/prisma/db"
Example for prisma:
// Insert new tasks
createdTask, err := dbClient.Client().Tasks.CreateOne(
db.Tasks.Title.Set("Hi from Prisma!"),
db.Tasks.Description.Set("Prisma is a database toolkit and makes databases easy."),
).Exec(dbClient.Context)
if err != nil {
fmt.Println(err)
}
result, _ := json.MarshalIndent(createdTask, "", " ")
fmt.Printf("created task: %s\n", result)
To learn more visit (https://github.com/prisma/prisma-client-go)
How to use async:
package main
import async "github.com/yourProjectName/utils/async"
//This functions wait 3 seconds to return 1
func DoneAsync() int {
fmt.Println("Warming up ...")
time.Sleep(3 * time.Second)
fmt.Println("Done ...")
return 1
}
func main() {
fmt.Println("Let's start ...")
//Here you call the function as async function
future := async.Exec(func() interface{} {
return DoneAsync()//The function that will await
}).Await()
fmt.Println("Done is running ...")
fmt.Println(future)
}
Build your application and after that, go to https://localhost:5000/swagger/index.html , you to see your Swagger UI.
When you create a new module swagger will be added automatically then you only need to modified what you need, but remember each time you modified swagger use the next command
swag init
To learn more visit (https://github.com/swaggo/gin-swagger)
To run tests use
go test -v ./...
To get coverage
go test -v -cover --coverprofile=coverage.out -coverpkg=./... ./...
To view test coverage on your browser
go tool cover -html=coverage.out
Total coverage
Windows
go tool cover -func=coverage.out | findstr total:
Linux
go tool cover -func=coverage.out | grep total:
Folder Structure:
|-- controller
| |
| |-tasks --> This is and example package(Create your own packages)
| |
| |-tasks.controller.go --> This is and example controller file(Create your own controllers)
|
|-- domain
| |
| |-usecase
| |
| |-tasks --> This is and example package(Create your own packages)
| |
| |-task.usecase.go --> This is and example usecase file(Create your own usecases)
|
|-- infraestructure
| |
| |-databases
| | |
| | |-client.go
| |
| |-entities
| | |
| | |-tasks --> This is and example package(Create your own packages)
| | |
| | |-tasks.entity.go --> This is and example entity file(Create your own entity)
| |
| |-respository
| |
| |-tasks --> This is and example package(Create your own packages)
| |
| |-tasks.repository.go --> This is and example repository file(Create your own repositories)
|
|-- utils
| |
| |-async
| | |
| | |-async.go
| |
| |-errors
| | |
| | |-errros.go
| |
| |-services
| |
| |-jwt
| | |
| | |-jwt.go
| |
| |-bcrypt
| |
| |-bcrypt.go
|
|-- env
| |
| |-env.go --> This is the env service
|
|
|-- routing
| |
| |-routing.go --> This is where all your routes lives
|
|
|-- test --> This is the test folder
| |
| |-tasks --> This is a test example folder
| | |
| | |-getTasks_test.go --> This is a test example file
| |
| |-other test folder --> your other test folder
flowchart TD
C{Folder Structure}
C -->|controller| D[task] -->N(task.go)
C -->|domain| E[usecase] -->Ñ(task) -->O(task.usecase.go)
C -->|infraestructure| F[databases]
C -->|utils| G[databases]
C -->|env| K[env.go]
C -->|routing| L[routing.go]
C -->|test| M[test.go]
MIT
Gojira is MIT licensed.