Code Monkey home page Code Monkey logo

api's Introduction

Gin Web RESTful API Example

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.

Installation

To install API Example, you need to install Go and set your Go workspace first.

  1. Download and install it

  2. Test whether the installation is successful:

$ go 
$ go env 
  1. Via git install:
$ git clone https://github.com/strivefatup/api.git

Example Directory Structure

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

Install A Third-party Package

use govendor install third-party package

  1. install govendor
$ go get -u -v github.com/kardianos/govendor
  1. Install according to the vendor.json file
$ govendor sync

API Examples

Using GET, POST, PUT and DELETE

/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)
    }
}

Run This Example

Default Port is 8085

$ go run main.go

Hint

Config function helper example

package main

import "api/config"

func main() {
    config.Config("db.mysql.connection.password")
}

api's People

Stargazers

codercat avatar  avatar  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.