Go-RestfulAPI









Github:github.com/golang-coll…

This project uses Go language to build Restful API service, and gin is used to build the project, including modules such as JWT, Swagger, Viper, ZAP, GORm, make, bash, etc., which can be dynamically expanded and shrunk by docker to achieve load balancing through one-click deployment.

This project uses Go language to build Restful API services, and gin to build the project, including JWT, Swagger, Viper, Zap, Gorm, Make, bash and other modules. It can be deployed by docker with one key and dynamically expand capacity to achieve load balance.

The directory structure

  • Conf is used to store configuration files
  • Config is used to read configuration files
  • Docs Swagger Generates documents
  • Handlers build concrete API services
    • Sd Health check
    • The user the user operation
    • Runtime gets the runtime IP address
  • Model structure definition
  • PKG Common function modules
    • Auth user encryption
    • Constvar paging query
    • Db Database connection pool
    • Errno Unified error code configuration
    • File Operation
    • Logging unified log
    • Token generated token
    • Version Gets the project version
  • Router Configures routing rules
    • Middleware middleware
  • Runtime Storing Logs
  • Service is used to store service-layer logic
  • Util Common tools
  • Admin. sh Starts and stops the service script
  • The Makefile compiles the build project
  • Unified entry for main.go project
  • Dockerfile Uses this file to build a Docker image
  • Docker-comemage. yml starts the service from this file

Config

You need to customize your config. Create the config.json file under conf folder.

The configuration sample is shown below

{
  "mysql": {
    "user": ""."password": ""."host": ""."db_name": ""
  },
  "redis": {
    "host": ""
  },
  "rabbitmq": {
    "user": ""."password": ""."host": ""}}Copy the code

Installation

Deployment projects offer the following two approaches:

  • Direct Deploy
  • Docker(Recommended)

Pre-requisite

  • Go 1.13.6
  • MySQL 5.7
  • Others: go. The mod

Quick Start

Please open the command line prompt and execute the command below. Make sure you have installed docker-compose in advance.

git clone [email protected]:golang-collection/Go-RestfulAPI.git
cd Go-RestfulAPI
make
docker-compose up -d
Copy the code

Next, you can look into the docker-compose.yml (with detailed config params).

Run

make

The make command generates a binary executable of the service from the Makefile.

Docker

Please use docker-compose to one-click to start up. By doing so, you don’t even have to configure RabbitMQ , Reds, MySQ,ElasticSearch. Create a file named docker-compose.yml and input the code below.

version: "3"

services:

  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports: ["8080"]

  lb:
    image: dockercloud/haproxy
    links:
      - web
    ports:
      - 80: 80
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
  
Copy the code

Then execute the command below, and the project will start up. Open the browser and enter http://localhost:80/swagger/index.html to see the swagger UI interface.

docker-compose up
Copy the code

You can expand the service by following this command, and then the load balancer will automatically assign each request to each service.

docker-compose up --scale web=3 -d
Copy the code

Direct

cd Go-RestfulAPI
make
bash admin.sh start
Copy the code

You can check the health of the service by following the command

bash admin.sh status
Copy the code

Appendix

  • Docker installation: docs.docker.com/
  • Docker – compose installation: docs.docker.com/compose/ins…

License

MIT

Copyright (c) 2020 Knowledge-Precipitation-Tribe