GoFrame 快速开始

Hello World

视频地址:https://www.bilibili.com/video/BV15R4y1G7hq/

包含以下内容:

  • 安装​GoFrame CLI
  • 使用​CLI​创建一个Go项目
  • 工程目录介绍

API Service Demo

视频地址:https://www.bilibili.com/video/BV1b44y1M7oL/

代码地址:https://github.com/gogf/gf-demo-user

我们以一个简单的​API Service​为例来介绍如何使用​GoFrame​框架以及相应的​CLI​工具来开发一个接口项目。

包含以下内容:

  1. 包名设计
  2. 接口设计
  3. 接口文档
  4. 配置管理
  5. 控制器实现
  6. 业务逻辑封装
  7. 路由注册
  8. 中间件使用
  9. Context及上下文变量

接口测试

我们通过​curl​命令来对其中两个接口执行简单的测试。

用户注册 ​- ​/user/signup​​

注册一个账号​test001​,昵称为​john​,密码为​123456​。

curl -d nickname=john&passport=test001&password=123456&password2=123456 http://127.0.0.1:8199/user/sign-up
{"code":0,"message":"","data":null}

我们再次使用刚才的信息注册一次试试。

curl -d nickname=john&passport=test001&password=123456&password2=123456 http://127.0.0.1:8199/user/sign-up
{"code":50,"message":"Passport "test001" is already token by others","data":null}

用户登录 – ​/user/signin

我们先访问获取用户信息的接口,验证鉴权中间件是否生效。

curl http://127.0.0.1:8199/user/profile
Forbidden

我们用刚才注册的账号登录。

curl -i -d passport=test001&password=123456 http://127.0.0.1:8199/user/sign-in
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH,HEAD,CONNECT,OPTIONS,TRACE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3628800
Content-Type: application/json
Server: GoFrame HTTP Server
Set-Cookie: gfsessionid=14sc9nep0u6yl0cieluexn0n0w2008q7; Path=/; Expires=Wed, 09 Mar 2022 15:52:44 GMT; SameSite
Trace-Id: 4830f6adbb72da16f34b7162f93080d8
Date: Tue, 08 Mar 2022 15:52:44 GMT
Content-Length: 35

{"code":0,"message":"","data":null}

我们这里使用了​-i​选项用于终端打印出服务端返回的​Header​信息,目的是为了获取​sessionid​。​GF​框架默认的​sessionid​名称为​gfsessionid​,我们看到返回的​Header​中已经有了,并且是通过​Cookie​方式返回的。

随后我们再次访问获取用户信息接口,并且这次提交​gfsessionid​,该信息可以通过​Header​也可以通过​Cookie​提交,服务端都是能够自动识别的。

curl -H gfsessionid:14sc9nep0u6yl0cieluexn0n0w2008q7 http://127.0.0.1:8199/user/profile
{"code":0,"message":"","data":{"id":1,"passport":"test001","password":"123456","nickname":"john","createAt":"2022-03-08 23:51:40","updateAt":"2022-03-08 23:51:40"}}

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/goframe/21153.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年5月18日
下一篇 2023年5月18日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注