-
Notifications
You must be signed in to change notification settings - Fork 0
Example websocket proof 5925307507322852050 #103
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
Merged
Suhaibinator
merged 3 commits into
websocket-support
from
example-websocket-proof-5925307507322852050
Dec 15, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/Suhaibinator/SRouter/pkg/router" | ||
| "github.com/gorilla/websocket" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| var upgrader = websocket.Upgrader{ | ||
| ReadBufferSize: 1024, | ||
| WriteBufferSize: 1024, | ||
| // Allow all origins for this example | ||
| CheckOrigin: func(r *http.Request) bool { return true }, | ||
| } | ||
|
|
||
| func main() { | ||
| // 1. Setup Server | ||
| logger, _ := zap.NewProduction() | ||
| defer logger.Sync() | ||
|
|
||
| routerConfig := router.RouterConfig{ | ||
| ServiceName: "websocket-example", | ||
| Logger: logger, | ||
| GlobalTimeout: 5 * time.Second, // Global timeout to test IsWebSocket bypass | ||
| } | ||
|
|
||
| // Simple auth - accept everything | ||
| authFunc := func(ctx context.Context, token string) (*string, bool) { | ||
| user := "generic-user" | ||
| return &user, true | ||
| } | ||
| userIdFunc := func(user *string) string { return *user } | ||
|
|
||
| r := router.NewRouter(routerConfig, authFunc, userIdFunc) | ||
|
|
||
| // REST Endpoint | ||
| r.RegisterRoute(router.RouteConfigBase{ | ||
| Path: "/hello", | ||
| Methods: []router.HttpMethod{router.MethodGet}, | ||
| Handler: func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write([]byte("Hello, World!")) | ||
| }, | ||
| }) | ||
|
|
||
| // WebSocket Endpoint | ||
| r.RegisterRoute(router.RouteConfigBase{ | ||
| Path: "/ws", | ||
| Methods: []router.HttpMethod{router.MethodGet}, | ||
| IsWebSocket: true, // Crucial: disables global timeout | ||
| Handler: func(w http.ResponseWriter, r *http.Request) { | ||
| conn, err := upgrader.Upgrade(w, r, nil) | ||
| if err != nil { | ||
| logger.Error("upgrade failed", zap.Error(err)) | ||
| return | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| for { | ||
| messageType, p, err := conn.ReadMessage() | ||
| if err != nil { | ||
| return | ||
| } | ||
| // Echo message back | ||
| if err := conn.WriteMessage(messageType, p); err != nil { | ||
| return | ||
| } | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| // Start server in goroutine | ||
| port := "8089" | ||
| server := &http.Server{Addr: ":" + port, Handler: r} | ||
|
|
||
| go func() { | ||
| if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
| log.Fatalf("ListenAndServe(): %v", err) | ||
| } | ||
| }() | ||
| fmt.Printf("Server started on port %s\n", port) | ||
|
|
||
| // Give server a moment to start | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| // 2. Test Client Logic | ||
| testREST(port) | ||
| testWebSocket(port) | ||
|
|
||
| // Shutdown | ||
| server.Shutdown(context.Background()) | ||
| fmt.Println("Done.") | ||
| } | ||
|
|
||
| func testREST(port string) { | ||
| fmt.Println("--- Testing REST Endpoint ---") | ||
| resp, err := http.Get(fmt.Sprintf("http://localhost:%s/hello", port)) | ||
| if err != nil { | ||
| log.Fatalf("REST request failed: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| log.Fatalf("REST expected status 200, got %d", resp.StatusCode) | ||
| } | ||
|
|
||
| body, _ := io.ReadAll(resp.Body) | ||
| fmt.Printf("REST Response: %s\n", string(body)) | ||
| fmt.Println("REST Test Passed!") | ||
| } | ||
|
|
||
| func testWebSocket(port string) { | ||
| fmt.Println("--- Testing WebSocket Endpoint ---") | ||
| u := url.URL{Scheme: "ws", Host: "localhost:" + port, Path: "/ws"} | ||
|
|
||
| c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) | ||
| if err != nil { | ||
| log.Fatalf("WebSocket dial failed: %v", err) | ||
| } | ||
| defer c.Close() | ||
|
|
||
| msg := "hello websocket" | ||
| err = c.WriteMessage(websocket.TextMessage, []byte(msg)) | ||
| if err != nil { | ||
| log.Fatalf("WebSocket write failed: %v", err) | ||
| } | ||
|
|
||
| _, message, err := c.ReadMessage() | ||
| if err != nil { | ||
| log.Fatalf("WebSocket read failed: %v", err) | ||
| } | ||
|
|
||
| fmt.Printf("WebSocket Response: %s\n", string(message)) | ||
| if string(message) != msg { | ||
| log.Fatalf("WebSocket expected echo '%s', got '%s'", msg, string(message)) | ||
| } | ||
| fmt.Println("WebSocket Test Passed!") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new indirect dependency on line 22 uses the module path
go.yaml.in/yaml/v2, which appears to be a misspelling of the standardgopkg.in/yaml.v2. Go will try to resolve modules by domain name, so keeping the incorrect host causesgo mod tidyor any build that resolves dependencies to fail with a module download error becausego.yaml.inis not the published module path. Please switch to the canonicalgopkg.in/yaml.v2or drop the entry if unused.Useful? React with 👍 / 👎.