From 4b4f8ea20b58cce655ec0bbc883126a087df1b04 Mon Sep 17 00:00:00 2001 From: Rens Houben Date: Tue, 15 Jul 2025 12:41:07 +0200 Subject: [PATCH] Moved http related functions to their own file; added 'sum.go' for a second endpoint. --- README.md | 8 +++-- functions/fibonacci.go | 45 ------------------------- functions/http.go | 74 ++++++++++++++++++++++++++++++++++++++++++ functions/sum.go | 6 ++++ main.go | 14 ++------ 5 files changed, 88 insertions(+), 59 deletions(-) create mode 100644 functions/http.go create mode 100644 functions/sum.go diff --git a/README.md b/README.md index d21a3c6..2b8fb52 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ -# Fibnacci +# Fibonacci -Just a little proof-of-concept setup to help teach myself Go, Containerisation, and CI/CD. \ No newline at end of file +Just a little proof-of-concept setup to help teach myself Go, Containerisation, and CI/CD. + +The Go code itself is basically a HTTP listener that listens to a port (argv[1], defaulting to 8088 if not specified), and handles POST requests to /fibonacci by responding with the fibonacci number of the indicated sequence. + +I've added a second handler /sum to it as another part of my experiments. \ No newline at end of file diff --git a/functions/fibonacci.go b/functions/fibonacci.go index 3a72142..3e1adf2 100644 --- a/functions/fibonacci.go +++ b/functions/fibonacci.go @@ -1,16 +1,5 @@ package functions -import ( - "fmt" - "net/http" - "strconv" -) - -func HttpAbort(w http.ResponseWriter, code int, message string) { - w.WriteHeader(code) - fmt.Fprint(w, message) -} - func MyFibonacci(count int) (fibonaccivalue int) { fibonaccivalue = 1 var prev_fibonacci int = 0 @@ -21,37 +10,3 @@ func MyFibonacci(count int) (fibonaccivalue int) { } return } - -func HttFibonacci(w http.ResponseWriter, request *http.Request) { - switch request.Method { - case "POST": - error := request.ParseForm() - if error != nil { - fmt.Printf("Error parsing POST request: %s\n", error) - HttpAbort(w, http.StatusUnprocessableEntity, - "Unable to parse your POST request.\n") - } else { - s_seq := request.Form.Get("seq") - seq, error := strconv.Atoi(s_seq) - if error != nil { - fmt.Printf("Error parsing integer value out of %s: %s\n", - s_seq, - error) - HttpAbort(w, http.StatusExpectationFailed, - fmt.Sprintf("[%s] is not a number.\n", s_seq)) - } else if seq < 1 { - fmt.Printf("Invalid sequence number: %d\n", seq) - HttpAbort(w, http.StatusBadRequest, - "Please provide a positive integer as the sequence number.\n") - } else { - fmt.Printf("Received request for Fibonacci Sequence Number %d\n", - seq) - fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n", - seq, MyFibonacci(seq)) - } - } - default: - HttpAbort(w, http.StatusMethodNotAllowed, - fmt.Sprintf("Operation %s not implemented for this URL", request.Method)) - } -} diff --git a/functions/http.go b/functions/http.go new file mode 100644 index 0000000..a70bf40 --- /dev/null +++ b/functions/http.go @@ -0,0 +1,74 @@ +package functions + +// HTTP helper functions doing the input checking and calling the actual math functions + +import ( + "fmt" + "net/http" + "strconv" +) + +func HttpAbort(w http.ResponseWriter, code int, message string) { + w.WriteHeader(code) + fmt.Fprint(w, message) +} + +func HttpGetPostInt(request *http.Request, key string) (value int, error error) { + switch request.Method { + case "POST": + error = request.ParseForm() + if error != nil { + error = fmt.Errorf("Unable to parse HTTP POST input: %s", + error.Error()) + return + } + value, error = strconv.Atoi(request.Form.Get(key)) + if error != nil { + fmt.Printf("Error parsing parameter '%s'\n", key) + error = fmt.Errorf("Unable to parse parameter '%s' as an integer: %s", + key, error.Error()) + } + return + default: + error = fmt.Errorf("This endpoint does not support %s operations.\n", request.Method) + return + } +} + +func HttFibonacci(w http.ResponseWriter, request *http.Request) { + seq, error := HttpGetPostInt(request, "seq") + if error != nil { + HttpAbort(w, http.StatusBadRequest, + fmt.Sprintf("Could not get parameter: %s", error.Error())) + } else if seq < 1 { + HttpAbort(w, http.StatusBadRequest, + "Please provide a positive integer as the sequence number.\n") + } else { + fmt.Printf("Received request for Fibonacci Sequence Number %d\n", + seq) + fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n", + seq, MyFibonacci(seq)) + } + return +} + +func HttSum(w http.ResponseWriter, request *http.Request) { + var errorstr string + first, error := HttpGetPostInt(request, "first") + if error != nil { + errorstr = error.Error() + } + second, error := HttpGetPostInt(request, "second") + if error != nil { + errorstr = error.Error() + } + + if errorstr != "" { + HttpAbort(w, http.StatusBadRequest, + fmt.Sprintf("Invalid input: %s\n", errorstr)) + return + } + fmt.Printf("Received a request to add %d and %d.\n", first, second) + fmt.Fprintf(w, "The sum of %d and %d is %d.\n", first, second, sum(first, second)) + +} diff --git a/functions/sum.go b/functions/sum.go new file mode 100644 index 0000000..e1c743b --- /dev/null +++ b/functions/sum.go @@ -0,0 +1,6 @@ +package functions + +func sum(first int, second int) (result int) { + result = first + second + return +} diff --git a/main.go b/main.go index ce83d15..7c85383 100644 --- a/main.go +++ b/main.go @@ -9,17 +9,6 @@ import ( "darjeeling.systemec.nl/rhouben/fibonacci/functions" ) -func MyFibonacci(count int) (fibonaccivalue int) { - fibonaccivalue = 1 - var prev_fibonacci int = 0 - - for i := 1; i < count; i++ { - fibonaccivalue += prev_fibonacci - prev_fibonacci = fibonaccivalue - prev_fibonacci - } - return -} - func main() { var port_number int var error error @@ -40,6 +29,7 @@ func main() { } bind_socket = fmt.Sprintf(":%d", port_number) http.HandleFunc("/fibonacci", functions.HttFibonacci) - fmt.Printf("Listening on port %d...", port_number) + http.HandleFunc("/sum", functions.HttSum) + fmt.Printf("Listening on port %d...\n", port_number) http.ListenAndServe(bind_socket, nil) }