{"id":17010552,"url":"https://github.com/daoshenzzg/go-web-demo","last_synced_at":"2025-07-10T05:36:08.574Z","repository":{"id":46169669,"uuid":"101039509","full_name":"daoshenzzg/go-web-demo","owner":"daoshenzzg","description":"基于gin的go web框架。包含log、mysql、redis、httpClient、grpcClient、hystrix 等组件，swagger、pprof集成。","archived":false,"fork":false,"pushed_at":"2020-04-17T08:26:25.000Z","size":15929,"stargazers_count":87,"open_issues_count":1,"forks_count":30,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T08:39:32.466Z","etag":null,"topics":["gin","httpclient","mysql","pprof","redis","swagger","validation"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/daoshenzzg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-22T08:21:11.000Z","updated_at":"2025-03-08T14:20:25.000Z","dependencies_parsed_at":"2022-09-24T15:01:02.186Z","dependency_job_id":null,"html_url":"https://github.com/daoshenzzg/go-web-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daoshenzzg%2Fgo-web-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daoshenzzg%2Fgo-web-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daoshenzzg%2Fgo-web-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daoshenzzg%2Fgo-web-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/daoshenzzg","download_url":"https://codeload.github.com/daoshenzzg/go-web-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/daoshenzzg%2Fgo-web-demo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259155162,"owners_count":22813765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gin","httpclient","mysql","pprof","redis","swagger","validation"],"created_at":"2024-10-14T06:04:49.861Z","updated_at":"2025-06-10T21:36:59.629Z","avatar_url":"https://github.com/daoshenzzg.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-web-demo: golang + gin\n这是一个基于[golang](https://golang.org/) + [gin](https://gin-gonic.com/)的基础Web框架。项目是完全按照Gopher\n公认的[项目标准结构](https://github.com/golang-standards/project-layout)定制，很多组件的实现大量参考[B站](https://www.bilibili.com)\n开源的[Kratos](https://github.com/bilibili/kratos)设计理念。为加深自己对go的理解，写了一些Web常用的Examples。\n\n包含如下特性：\n* 基于[gin](https://gin-gonic.com/)的轻量级web框架，拥有更加出色的性能。\n    * gin-validator表单验证。底层实现:https://gopkg.in/go-playground/validator.v8\n    * middleware拦截功能：可以自由的实现一些拦截handler\n    * swagger API文档\n    * pprof：性能分析工具\n    * 优雅退出机制\n\n* 基于[toml](https://github.com/toml-lang/toml)的配置文件，真的是谁用谁知道。\n* 参考[Kratos](https://github.com/bilibili/kratos)实现的log组件。支持配置日志位置、按文件大小滚动日志、缓冲大小等。   \n* 参考[Kratos](https://github.com/bilibili/kratos)实现的mysql组件。读写分离、支持事务、慢查询日志记录等。\n* 基于[redigo](https://github.com/gomodule/redigo)封装的redis组件。这也是golang官方推荐的redis包。\n* 基于net/http封装的http连接池组件。\n* 经典的错误码设计理念，与golang的error处理机制完美结合。\n* and so on...\n\n# 项目结构\n\u003cimg src=\"docs/project.png\" height=\"400px;\"/\u003e\n\n# 更新 2020-04-17\n* 新加基于 [hystrix-go](https://github.com/afex/hystrix-go) 的熔断器。\n* 优化 http Client 组件，并集成 hystrix，配置 demo 如下\n```toml\n[httpClient]\n    [httpClient.abc]\n        addr = \"http://api.abc.com\"\n        [httpClient.abc.clientConf]\n            maxTotal = 10\n            maxPerHost  = 10\n            keepAlive = \"5s\"\n            dialTimeout = \"1s\"\n            timeout = \"1s\"\n            [httpClient.abc.clientConf.breaker]\n                namespace = \"abc\"\n                timeout = \"3s\"\n                maxConcurrentRequests = 5\n                requestVolumeThreshold= 1\n                sleepWindow = \"5s\"\n                errorPercentThreshold = 50\n```\n* 新加 gRPC Client 组件，并集成 hystrix，配置 demo 如下\n```toml\n[grpcClient]\n    [grpcClient.sayHello]\n        addr = \"10.1.172.180:9101\"\n        [grpcClient.sayHello.clientConf]\n            dialTimeout = \"1s\"\n            timeout = \"1s\"\n            poolSize = 4\n            [grpcClient.sayHello.clientConf.breaker]\n                namespace = \"sayHello\"\n                timeout = \"1s\"\n                maxConcurrentRequests = 1000\n                requestVolumeThreshold= 10\n                sleepWindow = \"5s\"\n                errorPercentThreshold = 60\n```\n* 完善相关组件单元测试\n\n# 使用说明\n\n## 安装编译\n```$xslt \n$ go get github.com/daoshenzzg/go-web-demo\n\n# go get代理，org包要科学上网\n$ export GOPROXY=https://goproxy.io\n\n# 开启GO111MODULE模式\n$ export GO111MODULE=on\n\n# 使用GO MODULE模式管理包\n$ go mod init\n\n# 编译\n$ cd ../app/demo-api/cmd; go build\n\n# 运行\n$ ./cmd -conf ../configs/application.toml \n或者指定端口\n$ ./cmd -conf ../configs/application.toml --http.port=8080\n```\n\n## Swagger\n\n### 安装swagger\n1、go get\n```$xslt\n$ go get -u github.com/swaggo/swag/cmd/swag\n```\n若 $GOPATH/bin 没有加入$PATH中，你需要执行将其可执行文件移动到$GOBIN下\n```$xslt\n$ mv $GOPATH/bin/swag /usr/local/go/bin\n```\n2、gopm get\n```$xslt\ngopm get -g -v github.com/swaggo/swag/cmd/swag\n\ncd $GOPATH/src/github.com/swaggo/swag/cmd/swag\n\ngo install\n```\n同理将其可执行文件移动到$GOBIN下\n\n### 验证是否安装成功\n```$xslt\n$ swag -v\nswag version v1.5.0\n```\n\n### 编写API注释\n```$xslt\n// @Summary 添加学生\n// @Produce json\n// @Param studName query string true \"学生姓名\"\n// @Param studAge query int true \"年龄\"\n// @Param studSex query string true \"性别\"\n// @Success 200 {object} render.JSON\n// @Router /api/v1/student/add [post]\n```\n\n### 生成\n我们进入到demo-api的根目录，执行初始化命令\n```$xslt\n$ cd ../app/demo-api\n$ swag init\n  2019/07/12 18:33:53 Generate swagger docs....\n  2019/07/12 18:33:53 Generate general API Info\n  2019/07/12 18:33:59 Generating render.JSON\n  2019/07/12 18:33:59 create docs.go at  docs/docs.go\n  2019/07/12 18:33:59 create swagger.json at  docs/swagger.json\n  2019/07/12 18:33:59 create swagger.yaml at  docs/swagger.yaml\n```\n\n### 验证\n大功告成，访问：http://localhost:8080/swagger/index.html\n\n\u003cimg src=\"docs/swagger.png\" height=\"400px;\"/\u003e\n\n## pprof性能分析工具\n\n### 安装pprof\n请参考：https://github.com/DeanThompson/ginpprof\n\n### 验证\n大功告成，访问：http://localhost:8080/debug/pprof/\n\u003cimg src=\"docs/pprof.png\" height=\"400px;\"/\u003e\n\n### pprof实战\n请参考：https://blog.wolfogre.com/posts/go-ppof-practice/\n\n## 代码片段\n\n### 基于toml的配置文件\n用起来不要太爽，谁用谁知道...\n\n\u003cimg src=\"docs/code_config.png\" height=\"400px;\"/\u003e\n\n### Router配置\n基于gin的Router高效、强大、简单\n\n\u003cimg src=\"docs/code_router.png\" height=\"400px;\"/\u003e\n\n### Middleware拦截器\n基于gin的middleware，可以实现日志打印、权限验证等等..\n\n\u003cimg src=\"docs/code_middleware.png\" height=\"400px;\"/\u003e\n\n### 表单Validator功能\n很实用的表单validation功能，文档：https://gopkg.in/go-playground/validator.v8\n\n\u003cimg src=\"docs/code_validation.png\" height=\"400px;\"/\u003e\n\n### 优雅退出\n\u003cimg src=\"docs/code_graceful.png\" height=\"400px;\"/\u003e\n\n### 经典优雅的错误码设计\n定义Codes，并直接errors.Cause(e).(Codes)进行强转判断，完美兼容golang的error显性处理机制\n\n\u003cimg src=\"docs/code_ecode.png\" height=\"400px;\"/\u003e\n\n### DB事务操作\n\u003cimg src=\"docs/code_tx.png\" height=\"400px;\"/\u003e\n\n### Redis操作\n\u003cimg src=\"docs/code_redis.png\" height=\"400px;\"/\u003e\n\n### Http连接池请求连接复用\n\u003cimg src=\"docs/code_http.png\" height=\"400px;\"/\u003e\n\n# 后续规划\n* [x] DB、Redis、Http、gRPC组件\n* [x] 熔断器\n* [ ] Router限速器功能\n\n# 参考项目\n* https://github.com/gin-gonic/gin\n* https://github.com/EDDYCJY/go-gin-example\n* https://github.com/bilibili/kratos\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaoshenzzg%2Fgo-web-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdaoshenzzg%2Fgo-web-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdaoshenzzg%2Fgo-web-demo/lists"}