Conversation
28c7927 to
bb72d28
Compare
f1ad0ec to
a394332
Compare
a394332 to
f23c073
Compare
server/handle.go
Outdated
| inc := incomingMessage{} | ||
| err := reader.ReadJSON(&inc) | ||
| if err != nil { | ||
| log.Println(err) | ||
| break // Connection is dead. | ||
| } | ||
|
|
||
| // TODO Update package configparse for this purpose. | ||
| p, err := json.Marshal(inc.Data) | ||
| if err != nil { | ||
| log.Println(err) | ||
| break // Connection is dead. | ||
| } | ||
| cfg, err := configparse.JSON(p) | ||
| if err != nil { | ||
| log.Println(err) | ||
| break // Connection is dead. | ||
| } |
There was a problem hiding this comment.
We receive a config as JSON, but nested inside a message object. To decode the message we use websocket.Conn.ReadJSON(), which limits us given its signature: we have to give it the struct we want to decode the message into.
We already faced issues with JSON configs when parsing a config file, thus the package configparse internally uses unmarshaledConfig to decode in an intermediate struct.
Given the restriction of websocket.Conn.ReadJSON(), I needed to make the intermediate struct public to exploit it.
Just to remarshal it to parse it with configparse existing api.
I suspect we need to update configparse api to handle the new use case.
Maybe out of scope of this feature.
- Handler manages the websocket connection and interprets the messages - service (private) interfaces package runner
| const port = "8080" | ||
| const ( | ||
| port = "8080" | ||
| token = "6db67fafc4f5bf965a5a" //nolint:gosec |
There was a problem hiding this comment.
I'd be in favor of introducing actual authentication logics when they're specced rather than faking it now 🤔
| token = "6db67fafc4f5bf965a5a" //nolint:gosec |
There was a problem hiding this comment.
Thing is we need to have a mean to go around the cors.
So the token is needed in this pr.
Are you in favor of not even passing it as a argument from main (where it will be passed from anyway) and in-line it in server?
There was a problem hiding this comment.
We can still return true in CheckOrigin for now and implement the token logics later (I don't see how it is different from using a token that obtainable from the repo)
Regarding its implementation I'd keep the current one, except we get token from an environment variable instead of a hardcoded public value.
Description
This PR upgrade the basic http server into a websocket server, allowing continuous and bi-directional communication with
desktop.The procedure flow is:
Runner.Run(), pipes the progress as messages sends via the websocket. Then, whenRunner.Run()returns, the server sends the result (or the error) to the client.Implementation details
At high level:
server.Handlerhandles websocket stuff. It has a single exported method,Handler.ServerHTTP, making it ahttp.Handlerusable incmd/server/main.go. We configure the handler viaserver.NewHandler(note: configuration from the desktop app may be injected from here).The handler is the only one dealing with websocket directly. Serving on the correct path, it upgrades the connection, read incoming messages and invokes
service's methods accordingly.server.serviceinterfaces the packagerunner.Changes
servernow offers a websocket server (over a plain http one)socketiointerfaces reading from and writing to a websocket away from structserver.serviceNotes
Next steps suggestion
{ event: 'error', error: 'reason as string' }server.Handlerrestrict concurrent connections to/runto 1Closes #12