當前位置: 首頁>>代碼示例>>Golang>>正文


Golang RequestCtx.Write方法代碼示例

本文整理匯總了Golang中github.com/valyala/fasthttp.RequestCtx.Write方法的典型用法代碼示例。如果您正苦於以下問題:Golang RequestCtx.Write方法的具體用法?Golang RequestCtx.Write怎麽用?Golang RequestCtx.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/valyala/fasthttp.RequestCtx的用法示例。


在下文中一共展示了RequestCtx.Write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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)
}
開發者ID:bentranter,項目名稱:lazyblog,代碼行數:26,代碼來源:handler.go

示例2: 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)
}
開發者ID:bentranter,項目名稱:lazyblog,代碼行數:11,代碼來源:handler.go

示例3: 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
	}
}
開發者ID:smallnest,項目名稱:Jax-RS-Performance-Comparison,代碼行數:11,代碼來源:main.go

示例4: TestServerWrapHandler

func TestServerWrapHandler(t *testing.T) {
	e := echo.New()
	ctx := new(fasthttp.RequestCtx)
	req := NewRequest(ctx, nil)
	res := NewResponse(ctx, nil)
	c := e.NewContext(req, res)
	h := WrapHandler(func(ctx *fasthttp.RequestCtx) {
		ctx.Write([]byte("test"))
	})
	if assert.NoError(t, h(c)) {
		assert.Equal(t, http.StatusOK, ctx.Response.StatusCode())
		assert.Equal(t, "test", string(ctx.Response.Body()))
	}
}
開發者ID:o1egl,項目名稱:echo,代碼行數:14,代碼來源:server_test.go

示例5: HandleTest

func HandleTest(ctx *fasthttp.RequestCtx) {
	reader, err := os.Open("../testimages/loadimage/test.jpg")
	if err != nil {
		return
	}
	// Remember to free resources after you're done
	defer reader.Close()

	buffer := bytes.NewBuffer([]byte{})
	buffer.ReadFrom(reader)
	blob := buffer.Bytes()

	ctx.Write(blob)
}
開發者ID:phzfi,項目名稱:RIC,代碼行數:14,代碼來源:loadimageweb_test.go

示例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)
}
開發者ID:cokeboL,項目名稱:go-web-framework-benchmark,代碼行數:16,代碼來源:server.go

示例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)
}
開發者ID:bentranter,項目名稱:lazyblog,代碼行數:16,代碼來源:handler.go

示例8: 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)
}
開發者ID:funkygao,項目名稱:gafka,代碼行數:41,代碼來源:handler_fastpub.go

示例9: 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)
	}
}
開發者ID:Rocknrenew,項目名稱:filtron,代碼行數:22,代碼來源:api.go

示例10: 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")
	}
}
開發者ID:phzfi,項目名稱:RIC,代碼行數:40,代碼來源:main.go

示例11: ResJSON

// ResJSON 響應Json數據
func (fs *FastServer) ResJSON(ctx *fasthttp.RequestCtx, ti oauth2.TokenInfo) (err error) {
	data := map[string]interface{}{
		"access_token": ti.GetAccess(),
		"token_type":   fs.cfg.TokenType,
		"expires_in":   ti.GetAccessExpiresIn() / time.Second,
	}
	if scope := ti.GetScope(); scope != "" {
		data["scope"] = scope
	}
	if refresh := ti.GetRefresh(); refresh != "" {
		data["refresh_token"] = refresh
	}
	buf, err := json.Marshal(data)
	if err != nil {
		return
	}
	ctx.Response.Header.Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
	ctx.Response.Header.Set("Pragma", "no-cache")
	ctx.Response.Header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
	ctx.SetContentType("application/json;charset=UTF-8")
	ctx.SetStatusCode(200)
	_, err = ctx.Write(buf)
	return nil
}
開發者ID:Curtis-ly,項目名稱:oauth2,代碼行數:25,代碼來源:fastserver.go

示例12: plaintextHandler

// Test 6: Plaintext
func plaintextHandler(ctx *fasthttp.RequestCtx) {
	ctx.Write(helloWorldBytes)
}
開發者ID:tussion,項目名稱:FrameworkBenchmarks,代碼行數:4,代碼來源:hello.go

示例13: fastHttpHandler

//fasthttprouter
func fastHttpHandler(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
	if sleepTime > 0 {
		time.Sleep(sleepTimeDuration)
	}
	ctx.Write(message)
}
開發者ID:cokeboL,項目名稱:go-web-framework-benchmark,代碼行數:7,代碼來源:server.go

示例14: Act

func (b *blockAction) Act(_ string, ctx *fasthttp.RequestCtx) error {
	ctx.SetStatusCode(429)
	ctx.Write(b.message)
	return nil
}
開發者ID:Rocknrenew,項目名稱:filtron,代碼行數:5,代碼來源:action.go

示例15: hello

func hello(ctx *fasthttp.RequestCtx) {
	ctx.SetContentType("text/plain; charset=utf8")
	ctx.Write([]byte("hello world"))

}
開發者ID:chendx79,項目名稱:gafka,代碼行數:5,代碼來源:fasthttpd.go


注:本文中的github.com/valyala/fasthttp.RequestCtx.Write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。