commit 620b400bcdad6183cff18d07a37b5e6bf074d93c Author: Rens Houben Date: Sun Jul 13 22:13:35 2025 +0200 Okay, this is starting to look like something. diff --git a/functions/fibonacci.go b/functions/fibonacci.go new file mode 100644 index 0000000..3a72142 --- /dev/null +++ b/functions/fibonacci.go @@ -0,0 +1,57 @@ +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 + + for i := 1; i < count; i++ { + fibonaccivalue += prev_fibonacci + prev_fibonacci = fibonaccivalue - prev_fibonacci + } + 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/go.mod b/go.mod new file mode 100644 index 0000000..7e0b355 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module darjeeling.systemec.nl/rhouben/fibonacci + +go 1.24.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ce83d15 --- /dev/null +++ b/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "net/http" + "os" + "strconv" + + "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 + var bind_socket string + fmt.Println("Hello, world. I'm teaching myself Go.") + + if len(os.Args) <= 1 { + port_number = 8088 + } else { + port_number, error = strconv.Atoi(os.Args[1]) + + if error != nil { + fmt.Printf("I had an error trying to parse %s into a number: %s\n", + os.Args[1], + error) + os.Exit(0) + } + } + bind_socket = fmt.Sprintf(":%d", port_number) + http.HandleFunc("/fibonacci", functions.HttFibonacci) + fmt.Printf("Listening on port %d...", port_number) + http.ListenAndServe(bind_socket, nil) +}