協(xié)同開發(fā)巧用gitignore中間件避免網(wǎng)絡(luò)請求攜帶登錄信息
協(xié)同開發(fā)時本地測試
昨天的文章中提到了Go如何優(yōu)雅的進(jìn)行本地測試,今天分享一下:在多人協(xié)同開發(fā)中,如果大家都進(jìn)行本地測試可能會出現(xiàn)的問題。
最大的問題就是git合并的問題,大家都改這個test文件,就會導(dǎo)致有沖突。
我們可以通過把test文件加到.gitignore中來解決這個問題。
比如,我的測試文件所在目錄是:app/system/script/test.go。 我就在.gitignore中添加:
app/system/script/test.go
這樣我們就不用浪費(fèi)時間在解決git沖突上了。
GoFrame如何優(yōu)雅的獲得方法名
今天又發(fā)現(xiàn)一個優(yōu)雅的記錄錯誤日志的神器:runtime.Caller(0)
我們可以通過這個命令動態(tài)獲取對應(yīng)的方法,從而靈活的記錄錯誤日志,方便跟蹤定位問題。
示例如下:
shared.ApiLog()中第三個參數(shù)就是動態(tài)獲取的方法名。
//上下架
func (s *goodsService) Shelves(req *goods_unify.DoShelvesReq, r *ghttp.Request) (err error) {
defer func() {
if err != nil {
funcName, _, _, _ := runtime.Caller(0)
shared.ApiLog(r, "error/client_server_goods", runtime.FuncForPC(funcName).Name(), err.Error())
}
}()
err = service.GoodsUnify.DoShelves(r.Context(), req)
if err != nil {
return
}
return
}
巧用中間件
比如在登錄之后將登錄信息寫到上下文中,避免每次請求都攜帶登錄信息。
中間件在登錄之后設(shè)置關(guān)鍵信息到context上下文中
package middileware
const (
CtxAppKey = "AK"
CtxAppID = "app_id"
CtxChannel = "channel_id"
)
var Middleware = middlewareShared{}
type middlewareShared struct {
}
func (s *middlewareShared) Sign(r *ghttp.Request) {
code = checkSignV2(r)
r.Middleware.Next()
}
func checkSignV2(r *ghttp.Request) (code tools.Code) {
code, appKey, applicationInfo, sign, parmas := getSignv2Params(r)
if 1 != code.Code {
return
}
bodyBytes, err := ioutil.ReadAll(r.Request.Body)
if nil != err {
code = code.UnKnow()
return
}
r.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // 關(guān)鍵點(diǎn)
signRight, signParam := createSignV2(applicationInfo.Data.SecretKey, parmas, string(bodyBytes))
if signRight != sign {
code = code.SignErr("算法錯誤")
return
}
r.SetParam("appKey", appKey)
r.SetParam("appId", applicationInfo.Data.Id)
r.SetCtxVar(CtxAppID, applicationInfo.Data.Id)
r.SetCtxVar(CtxChannel, applicationInfo.Data.ChannelId)
return
}
業(yè)務(wù)邏輯直接通過context直接取值
通過r.Context().Value()獲取數(shù)據(jù):
//校驗(yàn)請求方權(quán)限
func checkLevel(r *ghttp.Request) (err error) {
if gconv.Int(r.Context().Value(middileware.CtxChannel)) !=10 {
err = errors.New("沒有權(quán)限")
return
}
return
}
case when
當(dāng)需要批量更新數(shù)據(jù)庫時,case when是個不錯的選擇,我再深入了解一下用法,后面單獨(dú)出一篇介紹 case when的文章。
總結(jié)
這篇文章總結(jié)了在協(xié)同開發(fā)中,可以把我們的調(diào)試文件添加到gitignore中,避免和其他同時因?yàn)檎{(diào)試文件而沖突,節(jié)省解決沖突的時間。
通過GoFrame的runtime.Caller(0)獲取方法名。
巧用中間件,避免請求中攜帶登錄信息。
更多關(guān)于gitignore避免網(wǎng)絡(luò)請求攜帶登錄信息的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go Gin實(shí)現(xiàn)文件上傳下載的示例代碼
這篇文章主要介紹了Go Gin實(shí)現(xiàn)文件上傳下載的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Go語言中Gin框架使用JWT實(shí)現(xiàn)登錄認(rèn)證的方案
在如今前后端分離開發(fā)的大環(huán)境中,我們需要解決一些登陸,后期身份認(rèn)證以及鑒權(quán)相關(guān)的事情,通常的方案就是采用請求頭攜帶token的方式進(jìn)行實(shí)現(xiàn),本文給大家介紹了Go語言中Gin框架使用JWT實(shí)現(xiàn)登錄認(rèn)證的方案,需要的朋友可以參考下2024-11-11

