Implementation of Go server.#1
Implementation of Go server.#1Chandanyadav2025 wants to merge 1 commit intolittle-bear-labs:mainfrom
Conversation
| myURL := "https://localhost:8090/?n=10" | ||
| parsedURL, err := url.Parse(myURL) | ||
| if err != nil { | ||
| fmt.Println("Can't parse URL", err) | ||
| return | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
How can we make the value of n in url as variable value.
and how can we also parse more than two input value in raw query ?
There was a problem hiding this comment.
Please take a look at the request type: https://pkg.go.dev/net/http#Request
The incoming request is already parsed, so you can fetch a query value from there.
Secondly, the request Body can also contain data.
| fmt.Println("Can't parse URL", err) | ||
| return | ||
| } | ||
| parts := strings.Split(parsedURL.RawQuery, "=") |
There was a problem hiding this comment.
There should be a parsed query object. You shouldn't have to split here. https://pkg.go.dev/net/url#URL.Query
| } | ||
|
|
||
| func main() { | ||
| http.HandleFunc("/", hello) |
There was a problem hiding this comment.
While this works, you should really create an http.Server object.
|
|
||
| func main() { | ||
| http.HandleFunc("/", hello) | ||
| http.ListenAndServe(":8090", nil) |
There was a problem hiding this comment.
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.
| "strings" | ||
| ) | ||
|
|
||
| func calculateSum(number int) int { |
There was a problem hiding this comment.
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.
No description provided.