Moving go code to subdir.

This commit is contained in:
2025-08-08 15:17:12 +02:00
parent 853bee22db
commit e8d042b65e
10 changed files with 17 additions and 24 deletions

16
code/.gitignore vendored Normal file
View File

@@ -0,0 +1,16 @@
# Allowlisting gitignore template for GO projects prevents us
# from adding various unwanted local files, such as generated
# files, developer configurations or IDE-specific files etc.
#
# Recommended: Go.AllowList.gitignore
# Ignore everything
*
.env
# But not these files...
!/.gitignore
!*.go
!go.sum
!go.mod

View File

@@ -0,0 +1,21 @@
package functions
import(
"errors"
"fmt"
)
func MyFibonacci(count int) (fibonaccivalue int, error error) {
fibonaccivalue = 1
error = nil
var prev_fibonacci int = 0
if count < 1 {
return -1, errors.New(fmt.Sprintf("%d is not a positive integer.",count))
}
for i := 1; i < count; i++ {
fibonaccivalue += prev_fibonacci
prev_fibonacci = fibonaccivalue - prev_fibonacci
}
return
}

View File

@@ -0,0 +1,15 @@
package functions
import (
"flag"
)
func FlagSet(name string) (found bool) {
found = false
flag.Visit(func (f *flag.Flag) {
if f.Name == name {
found = true
}
})
return
}

View File

@@ -0,0 +1,52 @@
package functions
import (
"testing"
"fmt"
)
// Test fibonacci function
func TestFibonacci(t *testing.T) {
var sequence = [4]int{1, 3, 5, 9}
var values = [4]int{1, 2, 5, 34}
for i := 0; i < len(sequence); i++ {
var seq, want = sequence[i], values[i]
outcome, error := MyFibonacci(seq)
if error != nil {
t.Errorf("Error trying to Fibonacci %d: %s", seq, error)
}
if outcome != want {
t.Errorf("Invalid outcome: Fibonacci sequence %d should be %d, got %d",
seq, want, outcome)
}
}
}
func TestFibBadNumber(t *testing.T) {
outcome,error := MyFibonacci(-4)
if error == nil {
t.Errorf("This should be an error, not a %d",outcome)
}
}
func TestSums(t *testing.T) {
var tests = []struct{
a, b int
want int
} {
{1, 2, 3},
{5, 3, 8},
{9, 4, 13},
}
for _, tt := range tests {
testname := fmt.Sprintf("%d+%d",tt.a, tt.b)
t.Run(testname, func(t *testing.T) {
result := sum(tt.a, tt.b)
if result != tt.want {
t.Errorf("Expected %d, got %d", tt.want,result)
}
})
}
}

79
code/functions/http.go Normal file
View File

@@ -0,0 +1,79 @@
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 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", 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)
value, error := MyFibonacci(seq)
if error != nil {
fmt.Printf("Error trying to Fibonacci %d: %s", seq, error)
fmt.Fprintf(w, "Error trying to Fibonacci %d: %s", seq, error)
} else {
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
seq, value)
}
}
}
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
code/functions/sum.go Normal file
View File

@@ -0,0 +1,6 @@
package functions
func sum(first int, second int) (result int) {
result = first + second
return
}

5
code/go.mod Normal file
View File

@@ -0,0 +1,5 @@
module darjeeling.systemec.nl/rhouben/fibonacci
go 1.24.4
require github.com/joho/godotenv v1.5.1

2
code/go.sum Normal file
View File

@@ -0,0 +1,2 @@
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

60
code/main.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"flag"
"github.com/joho/godotenv"
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
)
func main() {
var port_number int = 8098 // hard default
var error error
var bind_socket string
var seq, port int
error = godotenv.Load() // Load .env file
if error != nil {
fmt.Printf("Error trying to load .env file: %s\n", error)
}
port_arg := os.Getenv("FIB_PORT")
if port_arg != "" {
fmt.Printf("Loaded port value '%s' from environment.\n",port_arg)
port_number, error = strconv.Atoi(port_arg)
if error != nil {
fmt.Printf("I had an error trying to parse '%s' into a number: %s\n",
port_arg,
error)
os.Exit(0)
}
}
flag.IntVar(&port, "port", 8098,
fmt.Sprintf("Port number to bind to, default %d", port_number))
flag.IntVar(&seq, "number", 0,
"Fibonacci sequence value to calculate")
flag.Parse()
if functions.FlagSet("port") { // Command line overrides env
port_number = port
}
if !functions.FlagSet("number") {
fmt.Printf("Listening on port %d...\n", port_number)
bind_socket = fmt.Sprintf(":%d", port_number)
http.HandleFunc("/fibonacci", functions.HttFibonacci)
http.HandleFunc("/sum", functions.HttSum)
http.ListenAndServe(bind_socket, nil)
} else {
value, error := functions.MyFibonacci(seq)
if error != nil {
fmt.Printf("Failed to Fibonacci %d: %s", seq, error)
} else {
fmt.Printf("%d", value)
}
}
}