Creating a ready-made application for microservices in production in 5 minutes
在5分钟内为生产中的微服务创建现成的应用程序
In this article we will make an API Rest with Golang ready to run in production and set to the microservices.
在本文中,我们将使带有Golang的API Rest可以在生产环境中运行并设置为微服务。
All content shown in this article is available as a Github template at the following link:
本文中显示的所有内容均可通过以下链接作为Github模板使用:
先决条件: (Prerequisites:)
Before starting, you must have installed Golang in your development environment, so I leave here some links that explain how to install it:
开始之前,您必须已经在开发环境中安装了Golang,因此我在这里留下了一些说明如何安装它的链接:
Windows 10: https : //www.digitalocean.com/community/tutorials/how-to-install-go-and-set-up-a-local-programming-environment-on-windows-10-es
Ubuntu 18.04 LTS: https://www.digitalocean.com/community/tutorials/como-instalar-go-en-ubuntu-18-04-es
Ubuntu 18.04 LTS: https : //www.digitalocean.com/community/tutorials/como-instalar-go-en-ubuntu-18-04-es
Mac OS: https : //www.digitalocean.com/community/tutorials/how-to-install-go-and-set-up-a-local-programming-environment-on-macos-es
使用的框架和库: (Frameworks and Libraries used:)
- github.com/swaggo/gin-swagger github.com/swaggo/gin-swagger
- github.com/gin-gonic/gin github.com/gin-gonic/gin
- github.com/dgrijalva/jwt-go github.com/dgrijalva/jwt-go
- github.com/jinzhu/gorm github.com/jinzhu/gorm
- github.com/spf13/viper github.com/spf13/viper
如何使用此模板: (How to use this template:)
You can create a new project on Github if you access the template repository (https://github.com/antonioalfa22/go-rest-template) using the “Use this template” button:
如果您使用“使用此模板”按钮访问模板存储库( https://github.com/antonioalfa22/go-rest-template ),则可以在Github上创建一个新项目:
Using the button will take you to the page to create a new repository, where you can choose whether to include all the branches or not.
使用该按钮将带您到页面以创建一个新的存储库,您可以在其中选择是否包括所有分支。
The project is formed following the good practices for structuring projects in Golang specified in https://github.com/golang-standards/project-layout
该项目是根据https://github.com/golang-standards/project-layout中指定的在Golang中构建项目的良好做法而形成的
/cmd: In this folder are the input files to the project (main.go) as well as other files depending on the type of execution (eg database.db, config.yml, etc)
/ cmd:在此文件夹中,项目的输入文件(main.go)以及其他文件(取决于执行类型)(例如,database.db,config.yml等)
/docs: In this folder the swagger configuration files are saved to automatically generate the API documentation (available at http://localhost:3000/docs/index.html)
/ docs:在此文件夹中,需要保存swagger配置文件以自动生成API文档(位于http:// localhost:3000 / docs / index.html )
/internal: The application code is divided into two folders: api and pkg:
/内部:应用程序代码分为两个文件夹:api和pkg:
/internal/api: Main application, controllers, middlewares and router
/ internal / api:主要应用程序,控制器,中间件和路由器
/internal/pkg: Models, persistence and database.
/内部/ pkg: 模型,持久性和数据库。
/pkg: Modules reusable in other applications (For example: crypto, handlers, etc)
/ pkg:可在其他应用程序中重用的模块(例如:加密,处理程序等)
/test: Project unit tests
/ test:项目单元测试
Makefile: It facilitates the use of the application, it has the following format
Makefile:它促进了应用程序的使用,它具有以下格式
get-docs:
go get -u github.com/swaggo/swag/cmd/swag
docs: get-docs
swag init --dir cmd/api --parseDependency --output docs
build:
go build -o bin/restapi cmd/api/main.go
run:
go run cmd/api/main.go
test:
go test -v ./test/...
build-docker: build
docker build . -t api-rest
run-docker: build-docker
docker run -p 3000:3000 api-rest
Dockerfile:
Dockerfile:
FROM golang:1.12-alpine
RUN apk add --no-cache git gcc g++
# Set the Current Working Directory inside the container
WORKDIR /app
# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
# Build the Go app
RUN go build -o ./out/app ./cmd/api/main.go
# This container exposes port 8080 to the outside world
EXPOSE 3000
# Run the binary program produced by `go install`
CMD ["./out/app"]
#################################################################
FROM golang:1.12-alpine AS build_base
RUN apk add --no-cache git git gcc g++
# Set the Current Working Directory inside the container
WORKDIR /tmp
# We want to populate the module cache based on the go.{mod,sum} files.
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
# Build the Go app
RUN go build -o ./out/app ./cmd/api/main.go
# Start fresh from a smaller image
FROM alpine:3.9
RUN apk add ca-certificates
COPY --from=build_base /tmp/out/app /app/restapi
# This container exposes port 8080 to the outside world
EXPOSE 3000
# Run the binary program produced by `go install`
CMD ["/app/restapi"]
翻译自: https://medium.com/swlh/creating-a-production-ready-rest-api-with-go-6d91c5b09add