Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module implementation.go

go 1.22.3
38 changes: 38 additions & 0 deletions main.go
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 {
Copy link
Contributor

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:

  1. POST / which accepts a JSON object which contains the id and an SDP string.
  2. GET /<id> which returns the SDP stored with id or 404 if not found.

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
Copy link
Contributor

@ckousik ckousik Jul 19, 2024

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.
and how can we also parse more than two input value in raw query ?

Copy link
Contributor

Choose a reason for hiding this comment

The 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
The incoming request is already parsed, so you can fetch a query value from there.

Secondly, the request Body can also contain data.

parts := strings.Split(parsedURL.RawQuery, "=")
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

While this works, you should really create an http.Server object.

http.ListenAndServe(":8090", nil)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}