新课程发布《G02 Go API 实战》

说明

本课程以构建一个论坛 API 为主题,带大家从零开始构建一个高性能、功能齐全的 API 程序框架。

项目名称 Gohub,请见 程序结构

课程链接 《G02 Go API 实战》

课程亮点

课程主要涉及以下三大部分的知识点:

  • API 开发
  • 命令行开发
  • 构建高效率的程序结构

本课程虽命名为 API 实战,但不止于 API。

产品

产品的选型为『论坛』,但是本课程关于论坛功能的讲解占了很小的篇幅。主要是想把更多篇幅放在构建 API 和一个高效率的程序结构上。

本课程的设计初衷是,让同学们在学完本课程后,可以将代码应用到现实的工作中。

RESTful API 最佳实践

一套优秀的 API 设计,需要具备如下特性:

  1. 使用 HTTPS
  2. 使用域名
  3. 版本区分
  4. 使用 URL 来定位资源
  5. 使用 HTTP 动词来描述操作
  6. 支持资源过滤
  7. 使用 HTTP 状态码
  8. 数据响应的一致性
  9. 支持限流
  10. API 文档
  11. 自带分页链接
  12. 强制 User-Agent

详细讲解请见 RESTful API 最佳实践

Gohub 基于这些规范和要求开发。

所有路由

请求方法 API 地址 说明
POST /api/v1/auth/login/using-phone 短信 + 手机号登录
POST /api/v1/auth/login/using-password 手机号、用户名、邮箱 + 密码
POST /api/v1/auth/login/refresh-token 刷新 Token
POST /api/v1/auth/password-reset/using-email 邮件密码重置
POST /api/v1/auth/password-reset/using-phone 短信验证码密码重置
POST /api/v1/auth/signup/using-phone 使用手机号注册
POST /api/v1/auth/signup/using-email 使用邮箱注册
POST /api/v1/auth/signup/phone/exist 手机号是否已注册
POST /api/v1/auth/signup/email/exist email 是否已支持
POST /api/v1/auth/verify-codes/phone 发送短信验证码
POST /api/v1/auth/verify-codes/email 发送邮件验证码
POST /api/v1/auth/verify-codes/captcha 获取图片验证码
GET /api/v1/user 获取当前用户
GET /api/v1/users 用户列表
PUT /api/v1/users 修改个人资料
PUT /api/v1/users/email 修改邮箱
PUT /api/v1/users/phone 修改手机号
PUT /api/v1/users/password 修改密码
PUT /api/v1/users/avatar 上传头像
GET /api/v1/categories 分类列表
POST /api/v1/categories 创建分类
PUT /api/v1/categories/:id 更新分类
DELETE /api/v1/categories/:id 删除分类
GET /api/v1/topics 话题列表
POST /api/v1/topics 创建话题
PUT /api/v1/topics/:id 更新话题
DELETE /api/v1/topics/:id 删除话题
GET /api/v1/topics/:id 获取话题
GET /api/v1/links 友情链接列表

第三方依赖

使用到的开源库:

自定义的包

现在来看下我们自建的库:

  • app —— 应用对象
  • auth —— 用户授权
  • cache —— 缓存
  • captcha —— 图片验证码
  • config —— 配置信息
  • console —— 终端
  • database —— 数据库操作
  • file —— 文件处理
  • hash —— 哈希
  • helpers —— 辅助方法
  • jwt —— JWT 认证
  • limiter —— API 限流
  • logger —— 日志记录
  • mail —— 邮件发送
  • migrate —— 数据库迁移
  • paginator —— 分页器
  • redis —— Redis 数据库操作
  • response —— 响应处理
  • seed —— 数据填充
  • sms —— 发送短信
  • str —— 字符串处理
  • verifycode —— 数字验证码

代码行数

Gohub 项目总共有 4600 行代码(工具 gocloc):

$ gocloc .
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Go                             122           1200            865           4629
TOML                             1              7             21             28
-------------------------------------------------------------------------------
TOTAL                          123           1207            886           4657
-------------------------------------------------------------------------------

所有命令

$ go run main.go -h
Default will run "serve" command, you can use "-h" flag to see all subcommands

Usage:
   [command]

Available Commands:
  cache       Cache management
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  key         Generate App Key, will print the generated Key
  make        Generate file and code
  make        Generate file and code
  migrate     Run database migration
  play        Likes the Go Playground, but running at our application context
  seed        Insert fake data to the database
  serve       Start web server

Flags:
  -e, --env string   load .env file, example: --env=testing will use .env.testing file
  -h, --help         help for this command

Use " [command] --help" for more information about a command.

make 命令:

$ go run main.go make -h
Generate file and code

Usage:
   make [command]

Available Commands:
  apicontroller Create api controller,exmaple: make apicontroller v1/user
  cmd           Create a command, should be snake_case, exmaple: make cmd buckup_database
  factory       Create model's factory file, exmaple: make factory user
  migration     Create a migration file, example: make migration add_users_table
  model         Crate model file, example: make model user
  policy        Create policy file, example: make policy user
  request       Create request file, example make request user
  seeder        Create seeder file, example:  make seeder user

Flags:
  -h, --help   help for make

Global Flags:
  -e, --env string   load .env file, example: --env=testing will use .env.testing file

Use " make [command] --help" for more information about a command.

migrate 命令:

$ go run main.go migrate -h
Run database migration

Usage:
   migrate [command]

Available Commands:
  down        Reverse the up command
  fresh       Drop all tables and re-run all migrations
  refresh     Reset and re-run all migrations
  reset       Rollback all database migrations
  up          Run unmigrated migrations

Flags:
  -h, --help   help for migrate

Global Flags:
  -e, --env string   load .env file, example: --env=testing will use .env.testing file

Use " migrate [command] --help" for more information about a command.

课程链接

课程链接 《G02 Go API 实战》

摈弃世俗浮躁,追求技术精湛
本帖已被设为精华帖!
本帖由系统于 2年前 自动加精
Summer
讨论数量: 83

下个课程什么时候到来?

2年前 评论

催更,下个课程啥时候出呀?

1年前 评论

催更 下一本什么时候出呀

1年前 评论

催更微服务 + 1

1年前 评论

催更微服务!!!!!!

1年前 评论
王老板的前端

有 Golang 项目部署的相关教程么? hhh

已经找到了 14.1. 应用打包《G01 Go 实战:Web 入门》

1年前 评论

可以来一门 微服务,rpc相关的课程

2年前 评论
Summer (楼主) 2年前

催更下一个课程,可以预付费

1年前 评论

:+1:出的都支持了,很棒。

2年前 评论

感觉这个课程很硬核!我想请问一下老师,这个课程最后交付是否有前端代码?老师什么时候可以出一个微服务,分布式之类的课程吗?!

2年前 评论
WeeHong

請問這之後會不會推出單元測試?🥺

2年前 评论

哇,真是令人兴奋的教程,能不能再出一个教程,关于游戏那个方向的?

1年前 评论

催更下一阶段 go3 谢谢

4个月前 评论

催更下一阶段 go3 谢谢

4个月前 评论

催更下一阶段 go3 谢谢

4个月前 评论

一些大公司已经不用resetful方式了,全是Post 很奇葩

2个月前 评论
QYlaravel 2个月前
HadesTso

昨天才建了个新仓库写blog 今天就来货了 :joy:

2年前 评论

已购买,支持一下 :+1:

2年前 评论

千呼万唤始出来! :+1:

2年前 评论

还有下一门Go课程的计划吗?

2年前 评论
Summer (楼主) 2年前

summer大哥 真好啊。等了好久的课程 终于要出来了 :grin:

2年前 评论
Summer (楼主) 2年前

Summer 失踪了已久还以为忘了更新视频;这波带好货杀回来了,可以,给力👍

2年前 评论
Summer (楼主) 2年前
rovast

:+1:

2年前 评论

催更下一阶段 go3 谢谢 :+1:

1年前 评论

催更 G03 谢谢~

8个月前 评论

最大的败笔没有单元测试,这个又不是php现在稍微大的公司就让单元测试,是不是讲一下

10个月前 评论

催更微服务 + 1

11个月前 评论

能不能加一下定时任务和常驻任务相关

1年前 评论
Bunny3936 1年前

催更下一阶段 go3 谢谢

8个月前 评论

催更下一阶段 go3 谢谢 :joy:

6个月前 评论

催更下一阶段 go3 谢谢

5个月前 评论
刘天承

催更下一阶段 go3 谢谢

4个月前 评论

go3什么时候开始

2个月前 评论

催更下一阶段 go3 谢谢

2个月前 评论

催更 go3 有时间表吗

2个月前 评论
wanghan

催更下一阶段 go3 谢谢

2个月前 评论

他来了 他来了,他带着新教程来了 :smirk:

2年前 评论
MangoDa

file

2年前 评论

多谢Summer老师,终于来了。 :+1:

2年前 评论
zhaocrazy

学库手机版也那么好看 :+1:

2年前 评论
Jennie

那就卷起来 :smirk:

2年前 评论

我这边始终跑不起来一直卡在那里

file 而且编译也没反应

1年前 评论
fenglin 10个月前
renshengsiji (作者) 9个月前

太需要了,非常感谢

2年前 评论
Summer (楼主) 2年前

哥,下一门课大概什么时候上架呀?

1年前 评论

搞一个go框架的把

1年前 评论

我之前买过的什么会员在哪里看

2年前 评论

老大转go了吗

1年前 评论

刚购买,准备学习了

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!