Moved http related functions to their own file; added 'sum.go' for a second endpoint.
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user