GoFrame 快速開始

2022-03-26 14:36 更新

Hello World

視頻地址:https://www.bilibili.com/video/BV15R4y1G7hq/

包含以下內容:

  • 安裝?GoFrame CLI ?
  • 使用?CLI?創(chuàng)建一個Go項目
  • 工程目錄介紹

API Service Demo

視頻地址:https://www.bilibili.com/video/BV1b44y1M7oL/

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

我們以一個簡單的?API Service?為例來介紹如何使用?GoFrame?框架以及相應的?CLI?工具來開發(fā)一個接口項目。

包含以下內容:

  1. 包名設計
  2. 接口設計
  3. 接口文檔
  4. 配置管理
  5. 控制器實現(xiàn)
  6. 業(yè)務邏輯封裝
  7. 路由注冊
  8. 中間件使用
  9. Context及上下文變量

接口測試

我們通過?curl?命令來對其中兩個接口執(zhí)行簡單的測試。

用戶注冊 ?- ?/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?中已經(jīng)有了,并且是通過?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"}}


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號