go語言beego框架分頁器操作及接口頻率限制示例
獲取所有文章數(shù)據(jù)
o := orm.NewOrm()
qs := o.QueryTable("Article")
12
獲取總條數(shù)
count, _ := qs.Count() 1
設(shè)置每頁的條數(shù)
pageSetNum := 2 1
總頁數(shù)和當(dāng)前頁碼
// 總頁數(shù)
pageCount := math.Ceil((float64(count) / float64(pageSetNum)))
// 獲取當(dāng)前頁碼
pageNum, err := this.GetInt("pageNum")
if err != nil {
pageNum = 1
}
1234567
獲取分頁數(shù)據(jù)
//存儲(chǔ)分頁數(shù)據(jù)的切片 articles := new([]models.Article) //獲取分頁數(shù)據(jù) qs.Limit(pageSetNum, pageSetNum*(pageNum - 1)).All(articles) 1234
返回?cái)?shù)據(jù)
beego.Info(*articles) this.Data["articles"] = *articles this.Data["count"] = count this.Data["pageCount"] = pageCount this.Data["pageNum"] = pageNum this.TplName = "index.html"
beego接口頻率限制
package utils
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/logs"
"github.com/ulule/limiter"
"github.com/ulule/limiter/v3"
"github.com/ulule/limiter/v3/drivers/store/memory"
"net/http"
"strings"
)
// RateLimiter this is a struct
type RateLimiter struct {
Limiter *limiter.Limiter
Username string
UserType string
UserToken string
RemainTimes int
MaxTimes int
}
func RateLimit(rateLimit *RateLimiter, ctx *context.Context) {
var (
limiterCtx limiter.Context
err error
req = ctx.Request
)
opt := limiter.Options{
IPv4Mask: limiter.DefaultIPv4Mask,
IPv6Mask: limiter.DefaultIPv6Mask,
TrustForwardHeader: false,
}
ip := limiter.GetIP(req, opt)
if strings.HasPrefix(ctx.Input.URL(), "/") {
limiterCtx, err = rateLimit.Limiter.Get(req.Context(), ip.String())
} else {
logs.Info("The api request is not track ")
}
if err != nil {
ctx.Abort(http.StatusInternalServerError, err.Error())
return
}
if limiterCtx.Reached {
logs.Debug("Too Many Requests from %s on %s", ip, ctx.Input.URL())
// refer to https://beego.me/docs/mvc/controller/errors.md for error handling
ctx.Abort(http.StatusTooManyRequests, "429")
return
}
}
func PanicError(e error) {
if e != nil {
panic(e)
}
}
func RunRate() {
// 限制每秒登錄的請(qǐng)求次數(shù)
theRateLimit := &RateLimiter{}
// 100 reqs/second: "100-S" "100-s"
loginMaxRate := beego.AppConfig.String("total_rule::reqrate")
loginRate, err := limiter.NewRateFromFormatted(loginMaxRate + "-s")
PanicError(err)
theRateLimit.Limiter = limiter.New(memory.NewStore(), loginRate)
beego.InsertFilter("/*", beego.BeforeRouter, func(ctx *context.Context) {
RateLimit(theRateLimit, ctx)
}, true)
}
在main.go 里面調(diào)用方法即可
以上就是go語言beego框架分頁器操作及接口頻率限制示例的詳細(xì)內(nèi)容,更多關(guān)于go beego框架分頁器操作接口頻率限制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang 模塊引入及表格讀寫業(yè)務(wù)快速實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Golang模塊引入及表格讀寫業(yè)務(wù)的快速實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Go源碼字符串規(guī)范檢查lint工具strchecker使用詳解
這篇文章主要為大家介紹了Go源碼字符串規(guī)范檢查lint工具strchecker使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
go并發(fā)實(shí)現(xiàn)素?cái)?shù)篩的代碼
這篇文章主要介紹了go并發(fā)實(shí)現(xiàn)素?cái)?shù)篩的代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Go語言實(shí)現(xiàn)的最簡單數(shù)獨(dú)解法
前面給大家介紹過使用javascript實(shí)現(xiàn)的簡單的數(shù)獨(dú)解法,小伙伴們都非常喜歡,今天我們再來分享一則go語言實(shí)現(xiàn)的簡單的數(shù)獨(dú)解法,有需要的小伙伴來參考下。2015-03-03
Go?gRPC教程實(shí)現(xiàn)Simple?RPC
這篇文章主要為大家介紹了Go?gRPC教程實(shí)現(xiàn)Simple?RPC示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

