|
| 1 | +# Govel |
| 2 | +Go clean architecture adopted from Laravel. |
| 3 | + |
| 4 | +Main layer: Entity -> Repository -> Service -> Controller |
| 5 | + |
| 6 | +## Features |
| 7 | +- [x] Support `mysql`, `postgres`, `sqlite`, `sqlserver` databases |
| 8 | +- [x] Support ORM database |
| 9 | +- [x] Support database migration |
| 10 | +- [x] Support fake data |
| 11 | + |
| 12 | +## Main Packages |
| 13 | +- [x] Gorm: The fantastic ORM library for Golang, aims to be developer friendly. `github.com/go-gorm/gorm` |
| 14 | +- [x] Fiber: The fastest HTTP engine for Go. `github.com/gofiber/fiber` |
| 15 | + |
| 16 | +## Builds |
| 17 | +Build project based on your system: |
| 18 | +- `make linux-build` for linux system |
| 19 | +- `make mac-build` for macOs darwin system |
| 20 | +- `make migrate-linux-build` build migrate only for linux system |
| 21 | +- `make migrate-mac-build` build migrate only for macOs darwin system |
| 22 | + |
| 23 | +## Database Migration |
| 24 | +After build, use these commands: |
| 25 | +- Use command `./migrate start` to start migration |
| 26 | +- Use command `./migrate seed` to create fake data |
| 27 | + |
| 28 | +## Declaring Models |
| 29 | +Govel using `gorm` package to manage the database. Please follow this docs for more https://gorm.io/docs/models.html |
| 30 | +```go |
| 31 | +type User struct { |
| 32 | + ID uint `gorm:"primaryKey"` |
| 33 | + SocialId string `gorm:"type:varchar(255);unique;default:null"` |
| 34 | + Email string `gorm:"type:varchar(255);unique;not null"` |
| 35 | + Password string `gorm:"type:varchar(255);default:null"` |
| 36 | + EmailVerifiedAt time.Time |
| 37 | + Nick string `gorm:"type:varchar(50);unique;not null"` |
| 38 | + Name string `gorm:"type:varchar(255);not null"` |
| 39 | + Pic string `gorm:"type:varchar(255);not null;default:/assets/static/user.png"` |
| 40 | + Location string `gorm:"type:varchar(255);default:Indonesia"` |
| 41 | + Desc string `gorm:"type:varchar(255);default:null"` |
| 42 | + Role int `gorm:"type:tinyint(2);default:1"` |
| 43 | + Status int `gorm:"type:tinyint(2);default:0"` |
| 44 | + ApiToken string `gorm:"type:varchar(80);default:null"` |
| 45 | + RememberToken string `gorm:"type:varchar(100);default:null"` |
| 46 | + CreatedAt time.Time |
| 47 | + UpdatedAt time.Time |
| 48 | + DeletedAt gorm.DeletedAt `gorm:"index"` |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Migration |
| 53 | +Register your table entity to the migration `database/migration/migrator.go`. |
| 54 | +```go |
| 55 | +func Migrator(db *gorm.DB) { |
| 56 | + db.AutoMigrate(&entity.User{}) |
| 57 | + db.AutoMigrate(&entity.AnotherTable{}) |
| 58 | +} |
| 59 | +``` |
| 60 | +Build the project and run command `migration start`. |
| 61 | + |
| 62 | +## Seeder With Faker |
| 63 | +Setup your fake data `database/seeder/seeder.go`. |
| 64 | +```go |
| 65 | +func Seeder(db *gorm.DB) { |
| 66 | + for i := 0; i < 30; i++ { |
| 67 | + hashed, err := bcrypt.GenerateFromPassword([]byte(faker.Word()), bcrypt.DefaultCost) |
| 68 | + exception.PanicIfNeeded(err) |
| 69 | + db.Create(&entity.User{ |
| 70 | + Email: faker.Word() + "@gmail.com", |
| 71 | + Password: string(hashed), |
| 72 | + Name: faker.Word(), |
| 73 | + Nick: faker.Word(), |
| 74 | + Role: 1, |
| 75 | + Status: 1, |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Route |
| 82 | +Like laravel, you can add your route in `route/api.go` or `route/web.go`. |
| 83 | + |
| 84 | +For more docs follow this `https://docs.gofiber.io/guide/routing` |
| 85 | +```go |
| 86 | +route.Post("/create", userController.Create) |
| 87 | +route.Get("/show/:id", userController.Show) |
| 88 | +``` |
| 89 | + |
| 90 | +Or you can register your controller directly: |
| 91 | +```go |
| 92 | +func APIRoute(route fiber.Router, database *gorm.DB) { |
| 93 | + // Setup Repository |
| 94 | + userRepository := repository.NewUserRepository(database) |
| 95 | + |
| 96 | + // Setup Service |
| 97 | + userService := service.NewUserService(&userRepository) |
| 98 | + |
| 99 | + // Setup Controller |
| 100 | + userController := controller.NewUserController(&userService) |
| 101 | + userController.Route(route) |
| 102 | +} |
| 103 | +``` |
| 104 | +And define your route from controller: |
| 105 | +```go |
| 106 | +func (controller *UserController) Route(route fiber.Router) { |
| 107 | + group := route.Group("/users") |
| 108 | + group.Post("/all", controller.All) |
| 109 | + group.Post("/create", controller.Create) |
| 110 | + group.Post("/update/:id", controller.Update) |
| 111 | + group.Post("/delete/:id", controller.Delete) |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Middleware |
| 116 | +There are 3 default middleware: |
| 117 | +- [x] APPMiddleware: affect all routes including api and web. |
| 118 | +- [x] APIMiddleware: affect api route only. |
| 119 | +- [x] WebMiddleware: affect web route only. |
| 120 | + |
| 121 | +You can create your own route and register the middleware: |
| 122 | +```go |
| 123 | +route.Use("/users", middleware.Authenticate) |
| 124 | +``` |
| 125 | +For more docs follow this `https://docs.gofiber.io/guide/routing#middleware` |
| 126 | + |
| 127 | +## Access Database from Controller |
| 128 | +You can access the database object from fiber context in the controller directly. But this is not recommended. |
| 129 | +```go |
| 130 | +func (ctx *UserController) Example(c *fiber.Ctx) error { |
| 131 | + // Access the database object from fiber context |
| 132 | + db := c.Locals("DB").(*gorm.DB) |
| 133 | + |
| 134 | + result := map[string]interface{}{} |
| 135 | + db.Table("users").Where("id = ?", 1).Take(&result) |
| 136 | + return c.Status(200).JSON(result) |
| 137 | +} |
| 138 | +``` |
0 commit comments