Moved http related functions to their own file; added 'sum.go' for a second endpoint.
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
# Fibnacci
|
||||
# Fibonacci
|
||||
|
||||
Just a little proof-of-concept setup to help teach myself Go, Containerisation, and CI/CD.
|
||||
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.
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
74
functions/http.go
Normal file
74
functions/http.go
Normal file
@@ -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))
|
||||
|
||||
}
|
||||
6
functions/sum.go
Normal file
6
functions/sum.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package functions
|
||||
|
||||
func sum(first int, second int) (result int) {
|
||||
result = first + second
|
||||
return
|
||||
}
|
||||
14
main.go
14
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user