All Projects → ihahoo → gin-api-boilerplate

ihahoo / gin-api-boilerplate

Licence: MIT license
A Go RESTful API server with gin and docker

Programming Languages

go
31211 projects - #10 most used programming language
Makefile
30231 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to gin-api-boilerplate

easy-gin
一套基于 Gin 框架的 MVC 脚手架,使用 govendor 包管理
Stars: ✭ 74 (+362.5%)
Mutual labels:  gin, restful-api
golang api
This is a source code of my golang restful api series on youtube.
Stars: ✭ 36 (+125%)
Mutual labels:  gin, restful-api
Ppgo api demo gin
API接口应用Demo 基于Gin
Stars: ✭ 90 (+462.5%)
Mutual labels:  gin, restful-api
gateCracker
No description or website provided.
Stars: ✭ 20 (+25%)
Mutual labels:  restful-api
gmsec
golang micro service base on gin. golang 微服务集成框架
Stars: ✭ 141 (+781.25%)
Mutual labels:  gin
feedpushr
A simple feed aggregator daemon with sugar on top.
Stars: ✭ 222 (+1287.5%)
Mutual labels:  restful-api
spike
Golang Use Gin Framework implementation of the project
Stars: ✭ 20 (+25%)
Mutual labels:  gin
go-onion-architecture-sample
Sample RestAPI project write in Go
Stars: ✭ 40 (+150%)
Mutual labels:  restful-api
angular6-httpclient-example
Angular 6 HttpClient: Consume RESTful API Example
Stars: ✭ 38 (+137.5%)
Mutual labels:  restful-api
go-saas
go data framework for saas(multi-tenancy)
Stars: ✭ 101 (+531.25%)
Mutual labels:  gin
restful-services-in-pyramid
RESTful / HTTP services in Pyramid and Python course handout materials
Stars: ✭ 56 (+250%)
Mutual labels:  restful-api
laya-template
服务基本框架,template
Stars: ✭ 13 (-18.75%)
Mutual labels:  gin
metalarchives-api
RESTful API for metal-archives.com
Stars: ✭ 26 (+62.5%)
Mutual labels:  restful-api
egg-RESTfulAPI
基于egg.js2.x的RESTfulAPI风格的项目模板,用于快速构建高性能的服务端。
Stars: ✭ 70 (+337.5%)
Mutual labels:  restful-api
portal
A lightweight framework for golang object (struct) serialization (mapping). Inspired heavily by marshmallow (a Python library).
Stars: ✭ 24 (+50%)
Mutual labels:  restful-api
ogen
OpenAPI v3 code generator for go
Stars: ✭ 436 (+2625%)
Mutual labels:  restful-api
go-web-demo
基于gin的go web框架。包含log、mysql、redis、httpClient、grpcClient、hystrix 等组件,swagger、pprof集成。
Stars: ✭ 72 (+350%)
Mutual labels:  gin
rest.vertx
A JAX-RS like annotation processor for vert.x verticals and more
Stars: ✭ 138 (+762.5%)
Mutual labels:  restful-api
Mangadb
A Secured RESTful API Service for Manga
Stars: ✭ 17 (+6.25%)
Mutual labels:  restful-api
go-fastapi
Create an API and get Swagger definition for free
Stars: ✭ 76 (+375%)
Mutual labels:  gin

gin-api-boilerplate

A Go RESTful API server with gin and docker

使用go 1.11起自带的 go mod 做包管理,不再使用Dep。新版go语言,项目目录可以不用在$GOPATH中了。不再使用codegangsta/gin,使用fresh为自动重新启动服务工具。

安装

安装最新的Go

$ git clone https://github.com/ihahoo/gin-api-boilerplate.git

启动开发环境

安装自动重新启动服务工具fresh,这样在开发过程中,修改保存了文件后,会自动重启web服务。建议用docker环境开发和部署,下面构建的docker镜像,会安装此脚本。

$ go get github.com/pilu/fresh

启动服务:

$ make dev

测试服务:http://localhost:8080/hello

构建和启动正式环境

$ make build
$ make start

清理

$ make clean

Docker

  • 安装Docker (请设置阿里云等镜像加速器, 不用加速器拉镜像速度很慢)
  • 安装Docker Compose (一般在Windows和Mac下安装了Docker,同时会安装docker-compose)

开发环境是通过源代码的方式运行调试;测试和正式环境镜像只有可执行文件和配置文件,无源代码。

构建和启动docker开发环境

生成开发用的docker镜像,开发环境将项目根目录作为数据卷挂载到容器中。

$ make start-docker-dev

或者在项目根文件夹下通过以下命令启动:

$ docker-compose up -d

第一次启动会稍微花费点时间自动构建docker镜像,成功构建镜像后,下次启动不会再花费时间构建镜像。

通过docker-compose启动api web, postgres, redis三个容器,api web端口为8080, postgres端口为5432, redis端口为6379。数据库可通过本地客户端工具连接进行操作和调试。

测试接口是否正常启动:请访问http://localhost:8080/hello看是否有反馈信息。

停止开发环境

$ make stop-docker-dev

或者在项目根文件夹下运行命令:

$ docker-compose down

生成测试环境docker镜像

修改Makefile,可以修改镜像名称

$ make docker-image-staging

生成生产环境docker镜像

修改Makefile,可以修改镜像名称

$ make docker-image

包依赖管理

包依赖管理使用Go Modules,通过go mod vendor可以把当前项目的依赖模块安装到vendor目录下,使用Go Modules需要Go 1.11版本以上。

本项目将vendor加入到git版本控制中,主要是因为一些包在国内会被屏蔽,另外做自动化构建和部署的时候,外部依赖包都在vendor中了,比较方便。

注意

  • 本脚手架使用gin作为HTTP框架,使用方式请查看官网。也可以换成其它HTTP框架,使用方式类似。
  • golint为Go语言的Lint工具。详情请看官网。可以设置代码编辑器支持golint动态检测。
  • 一些配套工具比如配置文件、日志、数据库、reids等封装库,放到go-api-lib
  • 主配置文件采用json格式,文件位置data/config/app.json,配置库使用了viper,详情请查看官网。
  • 封装了Logrus库为日志工具
  • 日常开发,在api/下放置接口的函数或方法,在routes/下配置路由,一般按功能分不同文件分开。
  • docker/development 为开发环境docker构建文件,docker/staging为测试环境docker-compose文件,docker/production为生产环境docker-compose文件。
  • 一般正式发布下服务器部署只包含data/目录(存放配置文件、日志、数据库文件等作为docker数据卷加载),docker-compose.yml(启动docker容器的编排文件)。
  • 一般正式发布下的docker镜像里的项目目录下,只有Makefile(make脚本),bin/(编译过的可执行文件,启动服务的主执行文件),data/(挂载的数据卷,存放配置文件、日志、数据库文件等),部署出去的是编译好的可执行文件,相对安全独立。
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].