项目作者: kyhsa93

项目描述 :
Example for Go, Gin, MySQL, Redis, REST api, CQRS, Swagger
高级语言: Go
项目地址: git://github.com/kyhsa93/gin-rest-cqrs-example.git
创建时间: 2019-12-05T04:31:06Z
项目社区:https://github.com/kyhsa93/gin-rest-cqrs-example

开源协议:

下载


Gin CQRS Rest api example

This is sample Go project.

Go: https://golang.org/

Gin: https://gin-gonic.com/

Gorm: http://gorm.io/

Docker: https://www.docker.com/

Getting started

This is REST api made by Gin, redis, mysql with Go.

Prerequisites

Please install Go and docker.

I recommand to use docker for your environment.

Create development environment

Firstable, create directory $GOPATH/src/github.com/kyhsa93/ and clone this repository into your under that.

  1. mkdir $GOPATH/src/github.com/kyhsa93
  2. cd $GOPATH/src/github.com/kyhsa93
  3. git clone https://github.com/kyhsa93/gin-rest-cqrs-example.git

And then, build this program for set all dependencies.

  1. go build

go.mod and go.sum is already exists. so you don’t need run go mod init.

Next up, generate mysql and redis.

If you already have mysql and redis in your environment, you can use that.

But if you don’t have one or both of them, try followed process.

Install docker for your OS from link in top of this document.

If your docker is successfully installed, you can use docker cli.

  1. docker run --name gin-rest-cqrs-example -d -p 3306:3306 -e MYSQL_DATABASE=gin-rest-cqrs-example -e MYSQL_ROOT_PASSWORD=test -v ~/database/gin-rest-cqrs-example:/var/lib/mysql mysql:5.7
  2. docker run --name redis -d -p 6379:6379 redis:alpine
  3. OR
  4. docker-compose -f docker-compose.dev.yml up -d # create mysql, redis container for development environment
  5. docker-compose -f docker-compose.dev.yml down # remove created containers

Note. If you use docker container or any other docker resources included docker compose, recommended remove that after you use.

Now you can connect mysql in localhost:3306, that user ‘root’ and password is ‘test’.

Finally, your development environment is created.

And now you can start api with followed command.

  1. go run main.go

If you want apply your code change into running process, save all changes and rerun go run main.go.

Start with docker

If you can use docker cli, you can build docker image.

  1. docker build -t gin-rest-cqrs-example .
  2. docker images # list up docker images

And then you can create and run docker container using builded image.

  1. docker run -d -p 5000:5000 gin-rest-cqrs-example
  2. docker ps # list up runnint docker containers

And now you can connect api through http://localhost:5000.

Start with docker compose

Docker compose in this project is include api, redis and mysql.

Run followed command in this project directory root.

  1. docker-compose up -d # pull images, create and run containers in background process

If all containers are created, you can access api on http://localhost, and database also you can connect through by http://localhost:3306.

Default database user is ‘root’ and password is ‘test’.

After use docker-compose, you have to stop and remove all resources created by docker-compose in this project.

Run followed command in project root.

  1. docker-compose down # stop and remove containers in defined docker-compose.yml

Note. docker-compose in this project, does not build from this source code. If you want to build and use image from this code, you have to modify docker-compose.yml

Configurations

All configurations are in ./config

Most default configuration can use with your environment values.

And also you can modify configurations.

Documentation

Document about this project us made swagger.

Start this api and connect api host in your browser.

Note. Swagger in this project is use swag (https://github.com/swaggo/swag). Please check before you use it.

Scripts

  1. git clone https://github.com/kyhsa93/gin-rest-cqrs-example.git # clone this project
  2. go build # build this project
  3. docker run --name gin-rest-cqrs-example -d -p 3306:3306 -e MYSQL_DATABASE=gin-rest-cqrs-example -e MYSQL_ROOT_PASSWORD=test -v ~/database/
  4. gin-rest-cqrs-example:/var/lib/mysql mysql:5.7 # create mysql container
  5. docker run --name redis -d -p 6379:6379 redis:alpine # create redis container
  6. docker-compose -f docker-compose.dev.yml up -d # create mysql, redis container for development environment
  7. docker-compose -f docker-compose.dev.yml down # remove created containers
  8. go run main.go # start
  9. docker build -t gin-rest-cqrs-example . # build docker image
  10. docker images # list up docker images
  11. docker run -d -p 5000:5000 gin-rest-cqrs-example
  12. docker ps # list up runnint docker containers
  13. docker-compose up -d # build images, create and run containers in background process
  14. docker-compose down # stop and remove containers in defined docker-compose.yml
  15. swag init # build swagger

Github: https://github.com/kyhsa93/gin-rest-cqrs-example

Dockerhub: https://hub.docker.com/repository/docker/kyhsa93/gin-rest-cqrs-example