Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yaza-putu/golang-starter-api

Golang Starter API with standard Go project layout, central logger, include migration generator, seeder generator, jwt, echo, gorm, i18n
https://github.com/yaza-putu/golang-starter-api

echo golang golang-starter-project gorm i18n jwt redis standard-project-layout swagger-openapi-spec

Last synced: about 1 month ago
JSON representation

Golang Starter API with standard Go project layout, central logger, include migration generator, seeder generator, jwt, echo, gorm, i18n

Awesome Lists containing this project

README

        

# Golang Starter Project
![golang](https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Go_Logo_Blue.svg/1200px-Go_Logo_Blue.svg.png)

Project layout design influenced by [standard go project layout](https://github.com/golang-standards/project-layout)
## How to start

### By using the [Gos Tool](https://github.com/yaza-putu/gos)

```bash
go install github.com/yaza-putu/gos@latest
```
If it's not accessible, try moving it to the global bin
```bash
mv ~/go/bin/gos /usr/local/bin
```
Create new project with one command
```bash
gos create --echo
```

### Manual
```bash
git clone https://github.com/yaza-putu/golang-starter-api.git && cd golang-starter-api && rm -rf .git
```
- install depedency
```bash
make tidy
# or
go mod tidy
```
- initialize module name
```bash
make init module=your_module_name
```
- copy environment dev, test and set app_status=test for .env.test
```bash
make config
#or
cp .env.example .env
cp .env.example .env.test
```

- generate key
```bash
make key
```

- run dev mode
```bash
make serve
```
- build
```bash
make build
```

- run test
```bash
make gotest
```

- make migration
```bash
make migration table="name_of_table"
```

- run migration
```bash
make migrate-up
```

- make seeder
```bash
make seeder name="name_of_seeder"
```
- run seeder
```bash
make seed-up
```

# Support auto migrate when running app
You only need to set the env variable db_auto_migrate to true to enable this
```bash
db_auto_migrate=true
```

# How to mock gorm and redis
For this template using global variable for database connection and redis connection
- if you need mock database in unit test you can use
```go
// import
import "github.com/yaza-putu/golang-starter-api/internal/database"

dbMock := ...

database.Mock(dbMock)
```
special setting connection gorm in unit test (mocking)

set SkipInitializeWithVersion to true
```go
db, err := gorm.Open(mysql.New(mysql.Config{
...
SkipInitializeWithVersion: true,
}), &gorm.Config{})
```

- if you need mock redis in unit test you can use
```go
// import redis client
import redis_client "github.com/yaza-putu/golang-starter-api/internal/pkg/redis"

redisMock := ...

redis_client.Mock(redisMock)
```

# Default Login
email : [email protected]

pass : Password1

## Validation Unique With Struct Tag
- unique
```go
type v struct {
Name string `validate:"unique=table_name:column_name"`
}
// ecample
type v struct {
Name string `validate:"unique=users:name"`
}
```
- unique with ignore
```go
type v struct {
Name string `validate:"unique=table_name:column_name:ignore_with_field_name"`
ID string `validate:"required"`
}
// example
type v struct {
Name string `validate:"unique=users:name:ID"`
ID string `validate:"required" json:"id"`
}
```

# Validation file upload
```go
type FileHandler struct {
File multipart.File `validate:"required,filetype=image/png image/jpeg image/jpg"`
}

fs := FileHandler{}

f, err := ctx.FormFile("file")
if err == nil {
// send file into FileHandler struct to validate
fs.File, err = f.Open()
if err != nil {
return err
}
}
// validate with custom validation from go-playground/validator
val, err := request.Validation(&fs)

```
## Stack
- [Echo](https://echo.labstack.com)
- [Gorm](https://gorm.io)
- [Env](https://github.com/spf13/viper)
- [Redis](https://github.com/redis/go-redis)