Skip to content
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

Added new function to main.go #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
25 changes: 22 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"

"github.com/ServiceWeaver/weaver"
)
Expand Down Expand Up @@ -39,12 +40,30 @@ func main() {
fmt.Fprintf(w, "Hello, %s!\n", reversed)
})

http.HandleFunc("/reverse_2", func(w http.ResponseWriter, r *http.Request) {
reversed, err := reverser.Reverse(r.Context(), r.URL.Query().Get("name"))
// Get a client to the Reverser component.
mathserve, err := weaver.Get[Mathserve](root)
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
num1, _ := strconv.Atoi(r.URL.Query().Get("num1"))
num2, _ := strconv.Atoi(r.URL.Query().Get("num2"))
num, err := mathserve.Add(r.Context(), num1, num2)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintf(w, "Hello, %s!\n", reversed)
fmt.Fprintf(w, "The answer is, %d!\n", num)
})

http.HandleFunc("/sub", func(w http.ResponseWriter, r *http.Request) {
num1, _ := strconv.Atoi(r.URL.Query().Get("num1"))
num2, _ := strconv.Atoi(r.URL.Query().Get("num2"))
num, err := mathserve.Sub(r.Context(), num1, num2)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprintf(w, "The answer is, %d!\n", num)
})

http.Serve(lis, nil)

Choose a reason for hiding this comment

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

This code patch looks like it adds two new endpoints /add and /sub, which use the mathserve declared earlier to perform basic addition and subtraction operations.

One improvement suggestion would be to add proper error handling for the strconv.Atoi() calls, as they could potentially return errors if the input string is not a valid integer.

Additionally, it would be helpful to rename num to something more descriptive, such as result, to make the code easier to understand.

Expand Down
46 changes: 46 additions & 0 deletions mathserve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"fmt"

"github.com/ServiceWeaver/weaver"
)

// Reverser component.
type Mathserve interface {
Add(context.Context, int, int) (int, error)
Sub(context.Context, int, int) (int, error)
}

type mathserve struct {
weaver.Implements[Mathserve]
}

func (m *mathserve) Add(_ context.Context, x, y int) (int, error) {
if x == 0 || y == 0 {
return 0, fmt.Errorf("Zero values")
}

result := 0

for i := 0; i < x; i++ {
result += y
}

return result, nil
}

func (m *mathserve) Sub(_ context.Context, x, y int) (int, error) {
if x == 0 || y == 0 {
return 0, fmt.Errorf("Zero values")
}

result := 0

for i := 0; i < x; i++ {
result -= y
}

return result, nil
}

Choose a reason for hiding this comment

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

This is a Go code implementing an interface Mathserve which has two methods: Add and Sub. The methods take in two integers and return their sum and difference, respectively. Here's my review:

  • Overall, the code looks good.
  • It's good to see that the code uses context.Context for the parameters, although it's not being used inside the methods yet.
  • The error handling looks okay. The methods return an error if any input value is zero. However, it might be better to use a more descriptive error message.
  • There's no way to inject this component with its dependencies currently because its constructor is not a part of this code patch.
  • If the goal is to perform mathematical operations faster, then using loops can be computationally expensive. It would be good to consider using recursive functions or other techniques to optimize the performance.
  • It might also be a good idea to implement test cases to ensure that the code is working as expected under various input conditions.

Overall, the code seems to be functioning correctly and just needs some minor improvements.

242 changes: 242 additions & 0 deletions weaver_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.