Code Monkey home page Code Monkey logo

auroch-gateway's Introduction

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.

auroch-gateway's People

Contributors

shipu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.