Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websocket endpoint to advanced options #192

Merged
merged 24 commits into from
Mar 14, 2024

Conversation

MitchellBerend
Copy link
Contributor

@MitchellBerend MitchellBerend commented Feb 25, 2024

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Problem/Feature

Resolves #174

Description of Changes:

This PR adds an option to the --advanced flag that let's users add a websocket endpoint to their routes.go file automatically.

#184 is still open so if that get's merged before this one, this pr will also include the flag option like that pr describes.

Checklist

  • I have self-reviewed the changes being requested
  • I have updated the documentation (if applicable)

@MitchellBerend MitchellBerend marked this pull request as ready for review February 25, 2024 13:31
@MitchellBerend
Copy link
Contributor Author

Talked to @Ujstor about the generated websocket handler and they made the fair suggestion of changing the generated handler to be more in line with what websockets are typically used for; That is that they should be sending data without interaction of the client beyond the initial connection request.

The initial idea is for the server to send a timestamp to the client every 0.5 seconds.

@MitchellBerend MitchellBerend marked this pull request as draft February 26, 2024 13:40
@MitchellBerend MitchellBerend marked this pull request as ready for review February 27, 2024 18:57
@MitchellBerend MitchellBerend marked this pull request as draft February 27, 2024 20:59
@MitchellBerend
Copy link
Contributor Author

MitchellBerend commented Feb 27, 2024

During hand testing this feature the fiber option did not generate a full project 1. The linting jobs did pass but I think this qualifies as a 'flaky' test to me. Maybe these tests need to also include a build test of the generated project to make sure the application stays functional.

Footnotes

  1. the <- got sanitized into &lt;- since the template expects to output html

@MitchellBerend MitchellBerend marked this pull request as ready for review February 27, 2024 21:17
Copy link
Owner

@Melkeydev Melkeydev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR - excited for this.
A few comments and i think better error handling is needed

README.md Outdated

You can now use the `--advanced` flag when running the `create` command to get access to the following features. This is a multi-option prompt; one or more features can be used at the same time:

- [HTMX](https://htmx.org/) support using [Templ](https://templ.guide/)
- CI/CD workflow setup using [Github Actions](https://docs.github.com/en/actions)

- [Websocket](https://en.wikipedia.org/wiki/WebSocket) sets up a websocket endpoint
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to either link to some Go related source material and not just a wikipedia article on web sockets

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could link the most used websocket library 1 in the generated code but that would not encompass fiber as an option as that is using it's own websocket implementation.

Is that okay with you?

Footnotes

  1. https://pkg.go.dev/nhooyr.io/websocket

Comment on lines 741 to 744
importsPlaceHolder := ""
if p.AdvancedOptions["Websocket"] {
importsPlaceHolder += string(p.FrameworkMap[p.ProjectType].templater.WebsocketImports())
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this be necessary if this function is only called when the use selects web socket as an option?
Can we omit the if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye good point, I think this is a left over if statement from the refactor I did before sending this up.


importTmpl, err := template.New("imports").Parse(importsPlaceHolder)
if err != nil {
log.Fatal(err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I want to fix the error handling in go-blueprint.
Can we do something like

if err != nil {
    log.Fatalf("function failed: %v", err)
}

Comment on lines 752 to 751
if err != nil {
log.Fatal(err)
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

socket, err := websocket.Accept(w, r, nil)

if err != nil {
log.Print("could not open websocket")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to surface the error to understand what the error is

Comment on lines 53 to 56
log.Print("could not open websocket")
_, _ = w.Write([]byte("could not open websocket"))
w.WriteHeader(http.StatusInternalServerError)
return nil
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - we need to surface the error

Copy link
Collaborator

@Ujstor Ujstor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MitchellBerend

PR #184 is merged and introduced an inside flag feature. Some minor changes are needed so that the WS flag can be used.

Copy link
Collaborator

@briancbarrow briancbarrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that one change with the err and then it should be good to go.

Co-authored-by: Brian Barrow <briancbarrow@gmail.com>
@briancbarrow briancbarrow self-requested a review March 14, 2024 13:47
Copy link
Collaborator

@briancbarrow briancbarrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

Copy link
Collaborator

@Ujstor Ujstor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done! GG

@Ujstor Ujstor merged commit 076a576 into Melkeydev:main Mar 14, 2024
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding WebSocket
4 participants