Skip to content

Commit c8f89de

Browse files
committed
init project
0 parents  commit c8f89de

38 files changed

+1853
-0
lines changed

.env.example

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
APP_NAME=Govel-SQL
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=false
5+
APP_PORT=8000
6+
APP_TIMEZONE=Asia/Jakarta
7+
APP_LOCALE=id
8+
9+
DB_CONNECTION=mysql
10+
DB_HOST=127.0.0.1
11+
DB_PORT=3306
12+
DB_DATABASE=goveldb
13+
DB_USERNAME=root
14+
DB_PASSWORD=
15+
DB_TIMEZONE=Asia/Jakarta
16+
17+
MAIL_MAILER=smtp
18+
MAIL_HOST=smtp.example.com
19+
MAIL_PORT=587
20+
MAIL_USERNAME=[email protected]
21+
MAIL_PASSWORD=example
22+
MAIL_ENCRYPTION=tls
23+
24+
CACHE_DRIVER=redis
25+
26+
REDIS_HOST=127.0.0.1
27+
REDIS_PASSWORD=null
28+
REDIS_PORT=6379
29+
REDIS_CLIENT=predis
30+
REDIS_CACHE_DB=0

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
.env
8+
main
9+
migrate
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
vignoring
18+
19+
# Dependency directories (remove the comment below to include it)
20+
# vendor/

README.md

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
```

app/entity/user.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package entity
2+
3+
import (
4+
"time"
5+
6+
"gorm.io/gorm"
7+
)
8+
9+
type User struct {
10+
ID uint `gorm:"primaryKey"`
11+
SocialId string `gorm:"type:varchar(255);unique;default:null"`
12+
Email string `gorm:"type:varchar(255);unique;not null"`
13+
Password string `gorm:"type:varchar(255);default:null"`
14+
EmailVerifiedAt *time.Time
15+
Nick string `gorm:"type:varchar(50);unique;not null"`
16+
Name string `gorm:"type:varchar(255);index:,class:FULLTEXT;not null"`
17+
Pic string `gorm:"type:varchar(255);not null;default:/assets/static/user.png"`
18+
Location string `gorm:"type:varchar(255);default:Indonesia"`
19+
Desc string `gorm:"type:varchar(255);default:null"`
20+
Role int `gorm:"type:tinyint(2);default:1"`
21+
Status int `gorm:"type:tinyint(2);default:0"`
22+
ApiToken string `gorm:"type:varchar(80);default:null"`
23+
RememberToken string `gorm:"type:varchar(100);default:null"`
24+
CreatedAt time.Time
25+
UpdatedAt time.Time
26+
DeletedAt gorm.DeletedAt `gorm:"index"`
27+
}

app/exception/error.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package exception
2+
3+
func PanicIfNeeded(err interface{}) {
4+
if err != nil {
5+
panic(err)
6+
}
7+
}
8+
9+
func PanicResponse(msg string) {
10+
panic(ValidationError{
11+
Message: msg,
12+
})
13+
}

app/exception/handler.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package exception
2+
3+
import (
4+
"govel/app/model"
5+
6+
"github.com/gofiber/fiber/v2"
7+
)
8+
9+
func ErrorHandler(ctx *fiber.Ctx, err error) error {
10+
_, ok := err.(ValidationError)
11+
if ok {
12+
return ctx.Status(400).JSON(model.WebResponse{
13+
Code: 400,
14+
Message: "BAD_REQUEST",
15+
Data: err.Error(),
16+
})
17+
}
18+
19+
return ctx.Status(500).JSON(model.WebResponse{
20+
Code: 500,
21+
Message: "INTERNAL_SERVER_ERROR",
22+
Data: err.Error(),
23+
})
24+
}

app/exception/validation.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package exception
2+
3+
type ValidationError struct {
4+
Message string
5+
}
6+
7+
func (validationError ValidationError) Error() string {
8+
return validationError.Message
9+
}

0 commit comments

Comments
 (0)