this example is based on gin and gorm. Gin is a web framework written in Go (Golang), Gorm is ORM library for Golang. This example, there are still many shortcomings, to be improved, welcome to correct.
To install API Example, you need to install Go and set your Go workspace first.
-
Download and install it
-
Test whether the installation is successful:
$ go
$ go env
- Via git install:
$ git clone https://github.com/strivefatup/api.git
The application directory structure is as follows
api Application root directory
├── README.md
├── config Configuration file directory
│ ├── config.go Config helper function file
│ ├── db.yml Databases configuration file
│ └── pagination.yml Pagination configuration file
├── http
│ ├── Middleware Customize middleware directory
│ └── controller Controller directory
│ ├── baseController.go Base Controller
│ ├── order
│ │ └── order.go
│ └── user
├── main.go Entry file
├── models models
│ ├── BaseModel.go Base Model
│ └── order
│ └── order.go
├── resources Static resource directory
│ └── log Save log file directory
├── routes Route directory
│ ├── order
│ │ └── order.go
│ ├── route.go
│ └── user
├── serveres Server layer
│ └── order
│ └── order.go
├── tools tool directory
│ ├── databases
│ │ ├── connPool.go
│ │ └── mysql
│ │ └── mysqlConnPool.go
│ └── http
│ └── response
│ └── response_list_struct.go
└── vendor
└── vendor.json
use govendor install third-party package
- install govendor
$ go get -u -v github.com/kardianos/govendor
- Install according to the vendor.json file
$ govendor sync
/routes/order/order.go file
package order
import (
"github.com/gin-gonic/gin"
"api/http/controller/order"
)
func Order(engine *gin.Engine){
route := engine.Group("/api")
{
route.GET("/order", order.GetOrderList)
route.GET("/order/:id", order.GetOrderById)
route.POST("/order", order.CreateOrder)
route.PUT("/order/:id", order.UpdateOrder)
route.DELETE("/order/:id", order.DeleteOrderById)
}
}
Default Port is 8085
$ go run main.go
Config function helper example
package main
import "api/config"
func main() {
config.Config("db.mysql.connection.password")
}