|
| 1 | +## 说明 |
| 2 | + |
| 3 | +本项目是 [《G02 Go API 实战》](https://learnku.com/courses/go-api/) 实战课程的源码,基于 MIT 协议开源。 |
| 4 | + |
| 5 | + |
| 6 | +项目名称 Gohub,以论坛 API 为主题,设计的初衷是将其打造为高性能、功能齐全的 API 框架。 |
| 7 | + |
| 8 | +程序结构说明,请见 [程序结构](https://learnku.com/courses/go-api/1.17/program-structure/11772)。 |
| 9 | + |
| 10 | +## RESTful API 最佳实践 |
| 11 | + |
| 12 | +一套优秀的 API 设计,需要具备如下特性: |
| 13 | + |
| 14 | +1. 使用 HTTPS |
| 15 | +2. 使用域名 |
| 16 | +3. 版本区分 |
| 17 | +4. 使用 URL 来定位资源 |
| 18 | +5. 使用 HTTP 动词来描述操作 |
| 19 | +6. 支持资源过滤 |
| 20 | +7. 使用 HTTP 状态码 |
| 21 | +8. 数据响应的一致性 |
| 22 | +9. 支持限流 |
| 23 | +10. API 文档 |
| 24 | +11. 自带分页链接 |
| 25 | +12. 强制 User-Agent |
| 26 | + |
| 27 | +详细讲解请见 [RESTful API 最佳实践](https://learnku.com/courses/go-api/1.17/api-development-best-practices/11769) 。 |
| 28 | + |
| 29 | +## 所有路由 |
| 30 | + |
| 31 | +| 请求方法 | API 地址 | 说明 | |
| 32 | +| ------------ | ------------ | ------------ | |
| 33 | +| POST | /api/v1/auth/login/using-phone | 短信 + 手机号登录 | |
| 34 | +| POST | /api/v1/auth/login/using-password | 手机号、用户名、邮箱 + 密码 | |
| 35 | +| POST | /api/v1/auth/login/refresh-token | 刷下 Token | |
| 36 | +| POST | /api/v1/auth/password-reset/using-email | 邮件密码重置 | |
| 37 | +| POST | /api/v1/auth/password-reset/using-phone | 短信验证码密码重置 | |
| 38 | +| POST | /api/v1/auth/signup/using-phone | 使用手机号注册 | |
| 39 | +| POST | /api/v1/auth/signup/using-email | 使用邮箱注册 | |
| 40 | +| POST | /api/v1/auth/signup/phone/exist | 手机号是否已注册 | |
| 41 | +| POST | /api/v1/auth/signup/email/exist | email 是否已支持 | |
| 42 | +| POST | /api/v1/auth/verify-codes/phone | 发送短信验证码 | |
| 43 | +| POST | /api/v1/auth/verify-codes/email | 发送邮件验证码 | |
| 44 | +| POST | /api/v1/auth/verify-codes/captcha | 获取图片验证码 | |
| 45 | +| GET | /api/v1/user | 获取当前用户 | |
| 46 | +| GET | /api/v1/users | 用户列表 | |
| 47 | +| PUT | /api/v1/users | 修改个人资料 | |
| 48 | +| PUT | /api/v1/users/email | 修改邮箱 | |
| 49 | +| PUT | /api/v1/users/phone | 修改手机号 | |
| 50 | +| PUT | /api/v1/users/password | 修改密码 | |
| 51 | +| PUT | /api/v1/users/avatar | 上传头像 | |
| 52 | +| GET | /api/v1/categories | 分类列表 | |
| 53 | +| POST | /api/v1/categories | 创建分类 | |
| 54 | +| PUT | /api/v1/categories/:id | 更新分类 | |
| 55 | +| DELETE | /api/v1/categories/:id | 删除分类 | |
| 56 | +| GET | /api/v1/topics | 话题列表 | |
| 57 | +| POST | /api/v1/topics | 创建话题 | |
| 58 | +| PUT | /api/v1/topics/:id | 更新话题 | |
| 59 | +| DELETE | /api/v1/topics/:id | 删除话题 | |
| 60 | +| GET | /api/v1/topics/:id | 获取话题 | |
| 61 | +| GET | /api/v1/links | 友情链接列表 | |
| 62 | + |
| 63 | + |
| 64 | +## 第三方依赖 |
| 65 | + |
| 66 | +使用到的开源库: |
| 67 | + |
| 68 | +- [gin](https://github.com/gin-gonic/gin) —— 路由、路由组、中间件 |
| 69 | +- [zap](https://github.com/gin-contrib/zap) —— 高性能日志方案 |
| 70 | +- [gorm](https://github.com/go-gorm/gorm) —— ORM 数据操作 |
| 71 | +- [cobra](https://github.com/spf13/cobra) —— 命令行结构 |
| 72 | +- [viper](https://github.com/spf13/viper) —— 配置信息 |
| 73 | +- [cast](https://github.com/spf13/cast) —— 类型转换 |
| 74 | +- [redis](https://github.com/go-redis/redis/v8) —— Redis 操作 |
| 75 | +- [jwt](https://github.com/golang-jwt/jwt) —— JWT 操作 |
| 76 | +- [base64Captcha](https://github.com/mojocn/base64Captcha) —— 图片验证码 |
| 77 | +- [govalidator](https://github.com/thedevsaddam/govalidator) —— 请求验证器 |
| 78 | +- [limiter](https://github.com/ulule/limiter/v3) —— 限流器 |
| 79 | +- [email](https://github.com/jordan-wright/email) —— SMTP 邮件发送 |
| 80 | +- [aliyun-communicate](https://github.com/KenmyZhang/aliyun-communicate) —— 发送阿里云短信 |
| 81 | +- [ansi](https://github.com/mgutz/ansi) —— 终端高亮输出 |
| 82 | +- [strcase](https://github.com/iancoleman/strcase) —— 字符串大小写操作 |
| 83 | +- [pluralize](https://github.com/gertd/go-pluralize) —— 英文字符单数复数处理 |
| 84 | + |
| 85 | + |
| 86 | +## 自定义的包 |
| 87 | + |
| 88 | +现在来看下我们自建的库: |
| 89 | + |
| 90 | +- app —— 应用对象 |
| 91 | +- auth —— 用户授权 |
| 92 | +- cache —— 缓存 |
| 93 | +- captcha —— 图片验证码 |
| 94 | +- config —— 配置信息 |
| 95 | +- console —— 终端 |
| 96 | +- database —— 数据库操作 |
| 97 | +- file —— 文件处理 |
| 98 | +- hash —— 哈希 |
| 99 | +- helpers —— 辅助方法 |
| 100 | +- jwt —— JWT 认证 |
| 101 | +- limiter —— API 限流 |
| 102 | +- logger —— 日志记录 |
| 103 | +- mail —— 邮件发送 |
| 104 | +- migrate —— 数据库迁移 |
| 105 | +- paginator —— 分页器 |
| 106 | +- redis —— Redis 数据库操作 |
| 107 | +- response —— 响应处理 |
| 108 | +- seed —— 数据填充 |
| 109 | +- sms —— 发送短信 |
| 110 | +- str —— 字符串处理 |
| 111 | +- verifycode —— 数字验证码 |
| 112 | + |
| 113 | + |
| 114 | +## 代码行数 |
| 115 | + |
| 116 | +Gohub 项目总共有 4600 行代码(工具 [gocloc](https://github.com/hhatto/gocloc)): |
| 117 | + |
| 118 | +``` |
| 119 | +$ gocloc . |
| 120 | +------------------------------------------------------------------------------- |
| 121 | +Language files blank comment code |
| 122 | +------------------------------------------------------------------------------- |
| 123 | +Go 122 1200 865 4629 |
| 124 | +TOML 1 7 21 28 |
| 125 | +------------------------------------------------------------------------------- |
| 126 | +TOTAL 123 1207 886 4657 |
| 127 | +------------------------------------------------------------------------------- |
| 128 | +``` |
| 129 | + |
| 130 | +## 所有命令 |
| 131 | + |
| 132 | +``` |
| 133 | +$ go run main.go -h |
| 134 | +Default will run "serve" command, you can use "-h" flag to see all subcommands |
| 135 | +
|
| 136 | +Usage: |
| 137 | + [command] |
| 138 | +
|
| 139 | +Available Commands: |
| 140 | + cache Cache management |
| 141 | + completion Generate the autocompletion script for the specified shell |
| 142 | + help Help about any command |
| 143 | + key Generate App Key, will print the generated Key |
| 144 | + make Generate file and code |
| 145 | + make Generate file and code |
| 146 | + migrate Run database migration |
| 147 | + play Likes the Go Playground, but running at our application context |
| 148 | + seed Insert fake data to the database |
| 149 | + serve Start web server |
| 150 | +
|
| 151 | +Flags: |
| 152 | + -e, --env string load .env file, example: --env=testing will use .env.testing file |
| 153 | + -h, --help help for this command |
| 154 | +
|
| 155 | +Use " [command] --help" for more information about a command. |
| 156 | +``` |
| 157 | + |
| 158 | +make 命令: |
| 159 | + |
| 160 | +``` |
| 161 | +$ go run main.go make -h |
| 162 | +Generate file and code |
| 163 | +
|
| 164 | +Usage: |
| 165 | + make [command] |
| 166 | +
|
| 167 | +Available Commands: |
| 168 | + apicontroller Create api controller,exmaple: make apicontroller v1/user |
| 169 | + cmd Create a command, should be snake_case, exmaple: make cmd buckup_database |
| 170 | + factory Create model's factory file, exmaple: make factory user |
| 171 | + migration Create a migration file, example: make migration add_users_table |
| 172 | + model Crate model file, example: make model user |
| 173 | + policy Create policy file, example: make policy user |
| 174 | + request Create request file, example make request user |
| 175 | + seeder Create seeder file, example: make seeder user |
| 176 | +
|
| 177 | +Flags: |
| 178 | + -h, --help help for make |
| 179 | +
|
| 180 | +Global Flags: |
| 181 | + -e, --env string load .env file, example: --env=testing will use .env.testing file |
| 182 | +
|
| 183 | +Use " make [command] --help" for more information about a command. |
| 184 | +``` |
| 185 | + |
| 186 | +migrate 命令: |
| 187 | + |
| 188 | +``` |
| 189 | +$ go run main.go migrate -h |
| 190 | +Run database migration |
| 191 | +
|
| 192 | +Usage: |
| 193 | + migrate [command] |
| 194 | +
|
| 195 | +Available Commands: |
| 196 | + down Reverse the up command |
| 197 | + fresh Drop all tables and re-run all migrations |
| 198 | + refresh Reset and re-run all migrations |
| 199 | + reset Rollback all database migrations |
| 200 | + up Run unmigrated migrations |
| 201 | +
|
| 202 | +Flags: |
| 203 | + -h, --help help for migrate |
| 204 | +
|
| 205 | +Global Flags: |
| 206 | + -e, --env string load .env file, example: --env=testing will use .env.testing file |
| 207 | +
|
| 208 | +Use " migrate [command] --help" for more information about a command. |
| 209 | +``` |
0 commit comments