Skip to content

yaza-putu/golang-starter-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Starter Project

golang

Project layout design influenced by standard go project layout

Note : Be sure to rename the name of module before beginning the project.

How to start

  • install depedency

    make tidy
    # or
    go mod tidy
  • copy environment dev, test and set app_status=test for .env.test

    make config
    #or
    cp .env.example .env
    cp .env.example .env.test
  • generate key

      make key
  • run dev mode

      make serve
  • build

    make build
  • run test

     make gotest
  • make migration

     make migration table="name_of_table"
  • run migration

     make migrate-up
  • make seeder

    make seeder name="name_of_seeder"
  • run seeder

     make seed-up

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
// import
import "qila.co.id/internal/database"

dbMock :=  ...

database.Mock(dbMock)

special setting connection gorm in unit test (mocking)
set SkipInitializeWithVersion to true

db, err := gorm.Open(mysql.New(mysql.Config{
    ...
    SkipInitializeWithVersion: true,
}), &gorm.Config{})
  • if you need mock redis in unit test you can use
// import redis client
import redis_client "qila.co.id/internal/pkg/redis"

redisMock := ...

redis_client.Mock(redisMock)

Default Login

email : admin@mail.com

pass : Password1

Validation Unique With Struct Tag

  • unique
type v struct {
	Name string `validate:"unique=table_name:column_name"`
}
// ecample
type v struct {
Name string `validate:"unique=users:name"`
}
  • unique with ignore
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

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

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published