-
Notifications
You must be signed in to change notification settings - Fork 1
Implementation of Go server. #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module implementation.go | ||
|
|
||
| go 1.22.3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| func calculateSum(number int) int { | ||
| sum := 0 | ||
| for i := 1; i <= number; i++ { | ||
| sum += i | ||
| } | ||
| return sum | ||
| } | ||
| func hello(w http.ResponseWriter, req *http.Request) { | ||
| myURL := "https://localhost:8090/?n=10" | ||
| parsedURL, err := url.Parse(myURL) | ||
| if err != nil { | ||
| fmt.Println("Can't parse URL", err) | ||
| return | ||
| } | ||
|
Comment on lines
+19
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will never fail. Also, this parses a constant string. You should be using the URL from the request. It should already be in a parsed state.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can we make the value of n in url as variable value.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take a look at the request type: https://pkg.go.dev/net/http#Request Secondly, the request Body can also contain data. |
||
| parts := strings.Split(parsedURL.RawQuery, "=") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a parsed query object. You shouldn't have to split here. https://pkg.go.dev/net/url#URL.Query |
||
| n, err := strconv.Atoi(parts[1]) | ||
| if err != nil { | ||
| fmt.Println("number is not found", err) | ||
| return | ||
| } | ||
| var res int = calculateSum(n) | ||
| fmt.Fprintf(w, "The sum from 1 to %d is: %d", n, res) | ||
| } | ||
|
|
||
| func main() { | ||
| http.HandleFunc("/", hello) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this works, you should really create an |
||
| http.ListenAndServe(":8090", nil) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should look into the differences of binding to a port with just ":8090" vs "localhost:8090", "127.0.0.1:8090", your local network (192.168.x.x), and "0.0.0.0:8090". It is also trivial to make the port configurable here using an environment variable. |
||
| } | ||
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.
This is not needed. Instead, create an endpoint which can accept an (id, SDP) pair (for now you can simulate SDP using any string), and return the SDP when it is queried.
eg.
Build two endpoints:
POST /which accepts a JSON object which contains the id and an SDP string.GET /<id>which returns the SDP stored withidor 404 if not found.