本文整理汇总了Golang中github.com/valyala/fasthttp.RequestCtx.Error方法的典型用法代码示例。如果您正苦于以下问题:Golang RequestCtx.Error方法的具体用法?Golang RequestCtx.Error怎么用?Golang RequestCtx.Error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/valyala/fasthttp.RequestCtx
的用法示例。
在下文中一共展示了RequestCtx.Error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: mainHandler
func mainHandler(ctx *fasthttp.RequestCtx) {
// Performance hack for prefork mode - periodically close keepalive
// connections for evenly distributing connections among available
// processes.
if *prefork {
maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
if time.Since(ctx.ConnTime()) > maxDuration {
atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
ctx.SetConnectionClose()
}
}
path := ctx.Path()
switch string(path) {
case "/plaintext":
plaintextHandler(ctx)
case "/json":
jsonHandler(ctx)
case "/db":
dbHandler(ctx)
case "/queries":
queriesHandler(ctx)
case "/fortune":
fortuneHandler(ctx)
case "/update":
updateHandler(ctx)
default:
ctx.Error("unexpected path", fasthttp.StatusBadRequest)
}
}
示例2: idHandlerFunc
func idHandlerFunc(ctx *fasthttp.RequestCtx, idWorker *goflake.IdWorker, retry int) {
ua := string(ctx.UserAgent())
var (
id uint64
err error
)
for i := 0; i < retry; i++ {
id, err = idWorker.GetId(ua)
if err == nil {
break
}
}
r := map[string]string{
"id": strconv.FormatUint(id, 10),
}
if strings.HasSuffix(string(ctx.Path()), ".msgpack") {
ctx.SetContentType("application/x-msgpack; charset=UTF-8")
if err := codec.NewEncoder(ctx, mh).Encode(r); err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
} else {
ctx.SetContentType("application/json; charset=UTF-8")
if err := json.NewEncoder(ctx).Encode(r); err != nil {
ctx.Error(fmt.Sprintf(`{"error":"%v"}`, err.Error()), fasthttp.StatusInternalServerError)
}
}
}
示例3: 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)
}
示例4: CheckHost
// CheckHost Implement a CheckHost method on our new type
func (hs HostSwitch) CheckHost(ctx *fasthttp.RequestCtx) {
// Check if a http.Handler is registered for the given host.
// If yes, use it to handle the request.
if handler := hs[string(ctx.Host())]; handler != nil {
handler(ctx)
} else {
// Handle host names for wich no handler is registered
ctx.Error("Forbidden", 403) // Or Redirect?
}
}
示例5: fastHTTPHandler
func fastHTTPHandler(ctx *fasthttp.RequestCtx) {
if string(ctx.Method()) == "GET" {
switch string(ctx.Path()) {
case "/rest/hello":
ctx.Write([]byte("Hello world"))
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
return
}
}
示例6: fastHttpRawHandler
//fasthttp
func fastHttpRawHandler(ctx *fasthttp.RequestCtx) {
if string(ctx.Method()) == "GET" {
switch string(ctx.Path()) {
case "/hello":
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Write(message)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
return
}
ctx.Error("Unsupported method", fasthttp.StatusMethodNotAllowed)
}
示例7: GetAllPosts
// GetAllPosts is a method for getting every post
func GetAllPosts(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
posts, err := model.GetAll(s)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusNotFound)
return
}
resp, err := json.MarshalIndent(posts, "", " ")
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Write(resp)
}
示例8: mainHandler
func mainHandler(ctx *fasthttp.RequestCtx) {
path := ctx.Path()
switch string(path) {
case "/plaintext":
plaintextHandler(ctx)
case "/json":
jsonHandler(ctx)
case "/db":
dbHandler(ctx)
case "/queries":
queriesHandler(ctx)
case "/fortune":
fortuneHandler(ctx)
case "/update":
updateHandler(ctx)
default:
ctx.Error("unexpected path", fasthttp.StatusBadRequest)
}
}
示例9: SetPost
// SetPost is the API method for creating a new post.
func SetPost(ctx *fasthttp.RequestCtx, _ fasthttprouter.Params) {
v := &struct {
Title string
Body string
}{}
err := json.Unmarshal(ctx.PostBody(), v)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
p := model.NewPost(v.Title, v.Body)
id, err := p.Set(s)
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
ctx.Response.Header.Set("Content-Type", "application/json")
resp, err := json.MarshalIndent(map[string]string{
"id": string(id),
}, "", " ")
if err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
ctx.Write(resp)
}
示例10: 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)
}
示例11: ServeHTTP
// ServeHTTP is called whenever there is a new request.
// This is quite similar to JavaEE Servlet interface.
func (h *MyHandler) ServeHTTP(ctx *fasthttp.RequestCtx) {
// In the future we can use requester can detect request spammers!
// requester := ctx.RemoteAddr()
// Increase request count
count := &(h.requests)
atomic.AddUint64(count, 1)
if ctx.IsGet() {
url := ctx.URI()
operations, format, err, invalid := ParseURI(url, h.imageSource, h.watermarker)
if err != nil {
ctx.NotFound()
logging.Debug(err)
return
}
if invalid != nil {
ctx.Error(invalid.Error(), 400)
return
}
blob, err := h.operator.GetBlob(operations...)
if err != nil {
ctx.NotFound()
logging.Debug(err)
} else {
ctx.SetContentType("image/" + format)
ctx.Write(blob)
logging.Debug("Blob returned")
}
} else if ctx.IsPost() {
// POST is currently unused so we can use this for testing
h.RetrieveHello(ctx)
logging.Debug("Post request received")
}
}
示例12: requestHandler
func requestHandler(ctx *fasthttp.RequestCtx) {
headers := http.Header{}
ctx.Request.Header.VisitAll(func(key, value []byte) {
headers.Add(string(key), string(value))
})
u, err := url.Parse(fmt.Sprint("http://", *upstreamHost, "/", string(ctx.Request.RequestURI())))
if err != nil {
ctx.Error("Bad Gateway", fasthttp.StatusBadGateway)
log.Print(err)
return
}
req := &http.Request{
Method: string(ctx.Method()),
URL: u,
Proto: map[bool]string{true: "HTTP/1.1", false: "HTTP/1.0"}[ctx.Request.Header.IsHTTP11()],
ProtoMinor: map[bool]int{true: 1, false: 0}[ctx.Request.Header.IsHTTP11()],
Header: headers,
Body: ioutil.NopCloser(bytes.NewReader(ctx.Request.Body())),
ContentLength: int64(ctx.Request.Header.ContentLength()),
Host: string(ctx.Request.Header.Host()),
}
resp, err := upstreamClient.Do(req)
if err != nil {
ctx.Error("Bad Gateway", fasthttp.StatusBadGateway)
log.Print(err)
return
}
defer resp.Body.Close()
for k, v := range resp.Header {
ctx.Response.Header.Add(k, strings.Join(v, ","))
}
io.Copy(ctx.Response.BodyWriter(), resp.Body)
}
示例13: Handler
func (a *API) Handler(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("application/json")
ctx.Response.Header.Set("Access-Control-Allow-Origin", "*")
switch string(ctx.Path()) {
case "/rules":
j, err := json.Marshal(a.Proxy.Rules)
if err != nil {
ctx.Error(fmt.Sprintf("{\"error\": \"%v\"}", err), 500)
return
}
ctx.Write(j)
case "/rules/reload":
if err := a.Proxy.ReloadRules(a.RuleFile); err != nil {
ctx.Error(fmt.Sprintf("{\"error\": \"%v\"}", err), 500)
return
}
log.Println("Rule file reloaded")
ctx.Write([]byte("{\"status\": \"ok\"}"))
default:
ctx.Error("{\"error\": \"Not found\"}", fasthttp.StatusNotFound)
}
}
示例14: pubWsHandler
// /ws/topics/:topic/:ver
func (this *Gateway) pubWsHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
ctx.Error("not implemented", fasthttp.StatusBadRequest)
}
示例15: Handler
// Handler makes the router implement the fasthttp.ListenAndServe interface.
func (r *Router) Handler(ctx *fasthttp.RequestCtx) {
if r.PanicHandler != nil {
defer r.recv(ctx)
}
path := string(ctx.Path())
method := string(ctx.Method())
if root := r.trees[method]; root != nil {
if f, ps, tsr := root.getValue(path); f != nil {
f(ctx, ps)
return
} else if method != "CONNECT" && path != "/" {
code := 301 // Permanent redirect, request with GET method
if method != "GET" {
// Temporary redirect, request with same method
// As of Go 1.3, Go does not support status code 308.
code = 307
}
if tsr && r.RedirectTrailingSlash {
var uri string
if len(path) > 1 && path[len(path)-1] == '/' {
uri = path[:len(path)-1]
} else {
uri = path + "/"
}
ctx.Redirect(uri, code)
return
}
// Try to fix the request path
if r.RedirectFixedPath {
fixedPath, found := root.findCaseInsensitivePath(
CleanPath(path),
r.RedirectTrailingSlash,
)
if found {
uri := string(fixedPath)
ctx.Redirect(uri, code)
return
}
}
}
}
if method == "OPTIONS" {
// Handle OPTIONS requests
if r.HandleOPTIONS {
if allow := r.allowed(path, method); len(allow) > 0 {
ctx.Response.Header.Set("Allow", allow)
return
}
}
} else {
// Handle 405
if r.HandleMethodNotAllowed {
if allow := r.allowed(path, method); len(allow) > 0 {
ctx.Response.Header.Set("Allow", allow)
if r.MethodNotAllowed != nil {
r.MethodNotAllowed(ctx)
} else {
ctx.SetStatusCode(fasthttp.StatusMethodNotAllowed)
ctx.SetContentTypeBytes(defaultContentType)
ctx.SetBodyString(fasthttp.StatusMessage(fasthttp.StatusMethodNotAllowed))
}
return
}
}
}
// Handle 404
if r.NotFound != nil {
r.NotFound(ctx)
} else {
ctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound),
fasthttp.StatusNotFound)
}
}