Melkeydev / go-blueprint

Go-blueprint allows users to spin up a quick Go project using a popular framework
https://docs.go-blueprint.dev/
MIT License
5.98k stars 337 forks source link

Websocket example implementation #25

Closed MitchellBerend closed 1 year ago

MitchellBerend commented 1 year ago

Would a ping pong web socket endpoint example add a lot of value? The way I imagine it, it would add a dependency to the generated project, this dependency does get linked to by the docs.

I think gorilla has their own websocket implementation but I haven't worked with that one yet.

naive example implementation


handler(w http.ResponseWriter, req *http.Request) {
    socket, _ := websocket.Accept(w, req, nil)
    ctx := req.Context()
    for {
        _, socketBytes, _ := socket.Read(ctx)
        if string(socketBytes) == "PING" {
            _ = socket.Write(ctx, websocket.MessageText, "PONG")
        } else {
            _ = socket.Write(ctx, websocket.MessageText, "HUH?")
        }
    }
}
Melkeydev commented 1 year ago

:thinking: I am not against it - but I am thinking what would be the value add in terms of what go-blueprint attempts to provide, which is an easy way for users to get started with a Go project. If we a websocket, how will that work with the other frameworks?

MitchellBerend commented 1 year ago

I would try and reuse the same handler if possible for all frameworks but I would have to look at each framework to see if that is possible.

MitchellBerend commented 1 year ago

Okay so I picked gin as my first target and it is possible to keep the same handler function but gin's way of passing the ResponseWriter and Request are a little different than the standard lib. I will open a pr for this so this issue does not get clogged with implementations.

image

MitchellBerend commented 1 year ago

Decided this feature doesn't really fit in this project very well.