Пример #1
0
// ToHandler converts an httapi.Handler or http.HandlerFunc to an iris.Handler
func ToHandler(handler interface{}) Handler {
	//this is not the best way to do it, but I dont have any options right now.
	switch handler.(type) {
	case Handler:
		//it's already an iris handler
		return handler.(Handler)
	case http.Handler:
		//it's http.Handler
		h := fasthttpadaptor.NewFastHTTPHandlerFunc(handler.(http.Handler).ServeHTTP)

		return ToHandlerFastHTTP(h)
	case func(http.ResponseWriter, *http.Request):
		//it's http.HandlerFunc
		h := fasthttpadaptor.NewFastHTTPHandlerFunc(handler.(func(http.ResponseWriter, *http.Request)))
		return ToHandlerFastHTTP(h)
	default:
		panic(errHandler.Format(handler, handler))
	}
}
Пример #2
0
type Message struct {
	Id  int    `json:"id"`
	msg string `json:"string"`
}

var jsonHandle = func(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	messageList := make([]Message, 0)
	for i := 0; i < 100; i++ {
		messageList = append(messageList, Message{
			i,
			fmt.Sprintf("ezioruan%d", i),
		})
	}
	json.NewEncoder(w).Encode(&messageList)
}

var fastJsonHandle1 = fasthttpadaptor.NewFastHTTPHandlerFunc(jsonHandle)

var fastJsonHandle2 = func(ctx *fasthttp.RequestCtx) {
	ctx.SetContentType("application/json")
	messageList := make([]Message, 0)
	for i := 0; i < 100; i++ {
		messageList = append(messageList, Message{
			i,
			fmt.Sprintf("ezioruan%d", i),
		})
	}
	json.NewEncoder(ctx).Encode(&messageList)
}