本文整理匯總了Golang中github.com/buaazp/fasthttprouter.Params類的典型用法代碼示例。如果您正苦於以下問題:Golang Params類的具體用法?Golang Params怎麽用?Golang Params使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Params類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: GetPostJSON
// GetPostJSON is the API method for getting a post's JSON.
func GetPostJSON(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
id := ps.ByName("id")
postJSON, err := model.GetJSON([]byte(id), s)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusNotFound)
return
}
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Write(postJSON)
}
示例2: pubRawHandler
// /raw/msgs/:topic/:ver
func (this *Gateway) pubRawHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
var (
topic string
ver string
appid string
pubkey string
)
ver = params.ByName(UrlParamVersion)
topic = params.ByName(UrlParamTopic)
header := ctx.Request.Header
appid = string(header.Peek(HttpHeaderAppid))
pubkey = string(header.Peek(HttpHeaderPubkey))
if err := manager.Default.OwnTopic(appid, pubkey, topic); err != nil {
log.Error("app[%s] %s %+v: %v", appid, ctx.RemoteAddr(), params, err)
ctx.SetConnectionClose()
ctx.Error("invalid secret", fasthttp.StatusUnauthorized)
return
}
cluster, found := manager.Default.LookupCluster(appid)
if !found {
log.Error("cluster not found for app: %s", appid)
ctx.Error("invalid appid", fasthttp.StatusBadRequest)
return
}
var out = map[string]string{
"store": "kafka",
"broker.list": strings.Join(meta.Default.BrokerList(cluster), ","),
"topic": manager.Default.KafkaTopic(appid, topic, ver),
}
b, _ := json.Marshal(out)
ctx.SetContentType("application/json; charset=utf8")
ctx.Write(b)
}
示例3: MultiParams
// MultiParams is the multi params handler
func MultiParams(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
fmt.Fprintf(ctx, "hi, %s, %s!\n", ps.ByName("name"), ps.ByName("word"))
}
示例4: Hello
// Hello is the Hello handler
func Hello(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
fmt.Fprintf(ctx, "hello, %s!\n", ps.ByName("name"))
}
示例5: pubHandler
// /topics/:topic/:ver?key=mykey&async=1&delay=100
func (this *Gateway) pubHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
t1 := time.Now()
topic := params.ByName(UrlParamTopic)
header := ctx.Request.Header
appid := string(header.Peek(HttpHeaderAppid))
pubkey := string(header.Peek(HttpHeaderPubkey))
if err := manager.Default.OwnTopic(appid, pubkey, topic); err != nil {
log.Error("app[%s] %s %+v: %v", appid, ctx.RemoteAddr(), params, err)
ctx.SetConnectionClose()
ctx.Error("invalid secret", fasthttp.StatusUnauthorized)
return
}
msgLen := ctx.Request.Header.ContentLength()
switch {
case msgLen == -1:
log.Warn("pub[%s] %s %+v invalid content length", appid, ctx.RemoteAddr(), params)
ctx.Error("invalid content length", fasthttp.StatusBadRequest)
return
case int64(msgLen) > options.MaxPubSize:
log.Warn("pub[%s] %s %+v too big content length:%d", appid, ctx.RemoteAddr(), params, msgLen)
ctx.Error(ErrTooBigPubMessage.Error(), fasthttp.StatusBadRequest)
return
case msgLen < options.MinPubSize:
log.Warn("pub[%s] %s %+v too small content length:%d", appid, ctx.RemoteAddr(), params, msgLen)
ctx.Error(ErrTooSmallPubMessage.Error(), fasthttp.StatusBadRequest)
return
}
ver := params.ByName(UrlParamVersion)
queryArgs := ctx.Request.URI().QueryArgs()
key := queryArgs.Peek("key")
asyncArg := queryArgs.Peek("async")
async := len(asyncArg) == 1 && asyncArg[0] == '1'
//delay := hack.String(queryArgs.Peek("delay"))
if options.Debug {
log.Debug("pub[%s] %s {topic:%s, ver:%s, key:%s, async:%+v} %s",
appid, ctx.RemoteAddr(),
topic, ver, key, async,
string(ctx.Request.Body()))
}
if !options.DisableMetrics {
this.pubMetrics.PubQps.Mark(1)
this.pubMetrics.PubMsgSize.Update(int64(len(ctx.PostBody())))
}
pubMethod := store.DefaultPubStore.SyncPub
if async {
pubMethod = store.DefaultPubStore.AsyncPub
}
cluster, found := manager.Default.LookupCluster(appid)
if !found {
log.Error("cluster not found for app: %s", appid)
ctx.Error("invalid appid", fasthttp.StatusBadRequest)
return
}
err := pubMethod(cluster,
manager.Default.KafkaTopic(appid, topic, ver),
key, ctx.PostBody())
if err != nil {
if !options.DisableMetrics {
this.pubMetrics.PubFail(appid, topic, ver)
}
log.Error("%s: %v", ctx.RemoteAddr(), err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
// write the reponse
ctx.Write(ResponseOk)
if !options.DisableMetrics {
this.pubMetrics.PubOk(appid, topic, ver)
this.pubMetrics.PubLatency.Update(time.Since(t1).Nanoseconds() / 1e6) // in ms
}
}