aofei / air

An ideally refined web framework for Go.

Home Page:https://pkg.go.dev/github.com/aofei/air

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add MsgPack support

SaulDoesCode opened this issue · comments

vmihailenco/msgpack has a fairly decent and easy to use golang msgpack implementation it would be great to see native support in air, I've used echo and gin in the past and it would have been nice to see msgpack being handled natively the same way JSON is.

// WriteMsgPack responds to the client with the "application/msgpack" content v.
func (r *Response) WriteMsgPack(v interface{}) error {
	var (
		b   []byte
		err error
	)

	b, err = msgpack.Marshal(v)
	if err != nil {
		return err
	}

	r.Headers["content-type"] = &Header{
		Name:   "content-type",
		Values: []string{"application/msgpack"},
	}

	return r.WriteBlob(b)
}

Also maybe there is a way to stream json and msgpack.
Streaming might be more performant and memory efficient than writing blobs.
But I'm not sure how to do it right and handle stream failures properly.

I'm using these functions in my Gin application

// SendMsgpack send a msgpack encoded response with a status code
func SendMsgpack(c ctx, code int, msg interface{}) error {
	c.Status(code)
	c.Header("Content-Type", "application/msgpack")
	return msgpack.NewEncoder(c.Writer).Encode(msg)
}

// SendJSON send a json encoded response with a status code
func SendJSON(c ctx, code int, msg interface{}) error {
	c.Status(code)
	c.Header("Content-Type", "application/json")
	return json.NewEncoder(c.Writer).Encode(msg)
}

ps. air 很牛,真的,谢谢你创造它

Hi @SaulDoesCode,

I'm so glad to hear that you like this framework.

In fact, I don't know how powerful the MessagePack is, because I haven't used it in actual projects (but I've used Protocol Buffers and it works very well). So I'm not sure if we should add support for the MessagePack. Is that necessary?

And, of course, this framework supports streaming responses. Just call the Response#Write().

Please see commit 7c1d8e0. You can now write like this: json.NewEncoder(res.Body).Encode(...).

Hmm yeah, adding message pack support would add an extra dependency, one more thing to update and keep track off, and I suppose it is pretty niche, not that many people use it. That's ok yeah, I totally understand, I did add it in my fork just for convenience.

7c1d8e0, ah great, yes wonderful, this helps. I like streaming things 😝👍.

Oh one more thing, tdewolff/minify, and subsequently tdewolff/parse, need to be updated, they were throwing errors when I updated go.mod.

They seem to be working now with these versions:

github.com/tdewolff/minify v2.3.8-0.20181101065125-1920ee718c16+incompatible h1:NUIFjRnhP477SvkomaFBfYS4nvKUiaZkY1LUverjcmc=
github.com/tdewolff/minify v2.3.8-0.20181101065125-1920ee718c16+incompatible/go.mod h1:9Ov578KJUmAWpS6NeZwRZyT56Uf6o3Mcz9CEsg8USYs=
github.com/tdewolff/parse v2.3.5-0.20181101065215-3b56c4a41542+incompatible h1:qnrYarRqs7sDpm0h86oZX7wfVWeCG/0XT5aIa4w4PTY=
github.com/tdewolff/parse v2.3.5-0.20181101065215-3b56c4a41542+incompatible/go.mod h1:8oBwCsVmUkgHO8M5iCzSIDtpzXOT0WXX9cWhz+bIzJQ=

@SaulDoesCode

About tdewolff/parse and tdewolff/minify... sigh... I'm working on that (see tdewolff/parse#42).