-
- Notifications
You must be signed in to change notification settings - Fork 704
Add solution for Challenge 1 by agusu #549
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
Conversation
WalkthroughAdds a new Go CLI that reads two integers, sums them, and prints the result; and introduces an HTTP server scaffold with an authentication middleware protecting a /secure route while leaving /hello public. Exposes Sum, AuthMiddleware, and SetupServer functions. Changes
Sequence Diagram(s)sequenceDiagram autonumber participant User as User (stdin) participant Main as main() participant Sum as Sum(a,b) Note over Main: Parse "%d, %d" from stdin User->>Main: "3, 5" alt parse error Main-->>User: print error and exit else ok Main->>Sum: Sum(3,5) Sum-->>Main: 8 Main-->>User: print "8" end sequenceDiagram autonumber participant Client as Client participant Server as SetupServer() participant MW as AuthMiddleware participant Handler as secureHandler Client->>Server: GET /secure (X-Auth-Token) Server->>MW: Wrap secureHandler MW->>MW: Check header == "secret" alt Unauthorized MW-->>Client: 401 Unauthorized else Authorized MW->>Handler: Serve request Handler-->>Client: 200 OK (secure response) end Client->>Server: GET /hello Server-->>Client: 200 OK (hello) Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
challenge-1/submissions/agusu/solution-template.go (1)
10-14: Consider exiting with a non-zero status code on error.The error is printed but the program returns without signaling failure to the shell. This could mask issues in automated testing.
Apply this diff to exit with a non-zero status code:
_, err := fmt.Scanf("%d, %d", &a, &b) if err != nil { fmt.Println("Error reading input:", err) -return +os.Exit(1) }Note: You'll need to import the
ospackage:import ( "fmt" +"os" )challenge-5/submissions/agusu/solution-template.go (1)
13-22: Remove unnecessary parentheses and improve error message.The middleware logic is sound, but has two minor issues:
- Line 16: Unnecessary parentheses around the condition
- Line 17: Empty error message reduces clarity for API consumers
Apply this diff to address these issues:
func AuthMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { token := r.Header.Get("X-Auth-Token") - if (token != validToken) { - http.Error(w, "", http.StatusUnauthorized) + if token != validToken { + http.Error(w, "Unauthorized", http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
challenge-1/submissions/agusu/solution-template.go(1 hunks)challenge-5/submissions/agusu/solution-template.go(1 hunks)
🔇 Additional comments (2)
challenge-1/submissions/agusu/solution-template.go (1)
21-24: LGTM!The Sum function is correctly implemented and properly exported for external use.
challenge-5/submissions/agusu/solution-template.go (1)
34-47: LGTM!The server setup correctly demonstrates:
- Proper mux configuration
- Public vs. protected routes
- Middleware wrapping pattern
Summary by CodeRabbit