golang-gin-boilerplate

command module
v0.0.0-...-18e0029 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2022 License: MIT Imports: 3 Imported by: 0

README

golang-gin-boilerplate

Golang Artifact Boilerplate for Rest Api. It's based on Gin

create .env file if not exists in root directory:

cp .env.example .env

For Generate Crud:

go run ./art crud package_name crud_module_name
Example:

Run

go run ./art crud github.com/shipu/golang-gin-boilerplate notice

to generate crud.

Folder structure:

src/notice
├── controllers
│   └── notice_controller.go
├── models
│   └── notice.go
├── routes
│   └── api.go
└── services
└── notice_service.go
For Register Routes:

In routes/main.go

import (
    ...	
    ...
    ...
    noticeRoute "github.com/shipu/golang-gin-boilerplate/src/notice/routes"
)

func Register() {
    ...
    ...
    noticeRoute.NoticeSetup()
}
For Initialize Model Collection:

In routes/main.go

import (
    ...	
    ...
    ...
    notice "github.com/shipu/golang-gin-boilerplate/src/notice/models"
)

func Boot() {
    ...
    ...
    notice.NoticeSetup()
}

Congratulation !!! You have successfully generated rest crud api.

Now you can run your application with go run main.go command or air.

Swagger Api Documentation: http://localhost:8080/docs/index.html

Config :

Suppose your config is config/mongo.go:

package config

type MongoConfig struct {
	Username   string `mapstructure:"MONGO_USER" default:""`
	Password   string `mapstructure:"MONGO_PASS" default:""`
	Host       string `mapstructure:"MONGO_HOST" default:""`
	Port       string `mapstructure:"MONGO_PORT" default:""`
	Database   string `mapstructure:"MONGO_DATABASE" default:""`
	Connection string `mapstructure:"MONGO_CONNECTION" default:"mongodb"`
}

and your .env is:

MONGO_CONNECTION=mongodb
MONGO_HOST=
MONGO_PORT=
MONGO_USER=
MONGO_PASS=
MONGO_DATABASE=

For initialization MongoConfig config.

add below example code in config/main.go

import (
    ...	
    ...
    ...
    . "github.com/shipu/artifact"
)

func Register() {
    ...
    ...
    Config.AddConfig("NoSql", new(MongoConfig)).Load()
}

func Boot() {
    ...
    ...
}

if you want to run something when application start, you can add anything in Boot function.

To get config:
Config.GetString("NoSql.Host")

Config Method List:

GetString("key")
GetInt("key")
Get("key")

Route

Suppose example route is notice/routes/api.go:

package routes

import (
	. "github.com/shipu/artifact"
	c "github.com/shipu/golang-gin-boilerplate/src/notice/controllers"
)

func Setup() {
    v1 := Router.Group("api/v1")
    v1.GET("notices", c.NoticeIndex())
    v1.POST("notices", c.NoticeCreate())
    v1.GET("notices/:noticeId", c.NoticeShow())
    v1.PUT("notices/:noticeId", c.NoticeUpdate())
    v1.DELETE("notices/:noticeId", c.NoticeDelete())
}

For Register example route. in routes/main.go

import (
    ...	
    ...
    ...
    noticeRoute "github.com/shipu/golang-gin-boilerplate/src/notice/routes"
)

func Register() {
    ...
    ...
    noticeRoute.Setup()
}

Response

In Gin

Where c is the *gin.Context context.

data := map[string]interface{}{
    "app": "Golang",
}
c.JSON(200, gin.H{
    "status_code":  200,
    "message": "Success",
    "data": data,
})

In artifact boilerplate, you can use Res.

data := map[string]interface{}{
    "app": "Golang",
}

Res.Code(200).
    Message("Success").
    Data(data).
	Json(c)

Res Api Methods:

Json
PureJSON
JsonP
AsciiJSON
IndentedJSON
Html
Xml
Yaml
ProtoBuf
AbortWithStatusJSON
Abort
AbortWithError
Redirect

Mongo Collection

var TodoCollection artifact.MongoCollection = artifact.Mongo.Collection("todos")

TodoCollection.Find(bson.M{})

All Go Mongo Driver Support.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY SWAG; DO NOT EDIT This file was generated by swaggo/swag
src

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL