Added unit tests and workflow call for same

This commit is contained in:
2025-07-24 11:37:38 +02:00
parent 5d8d0050cd
commit 7564c9fb3a
7 changed files with 115 additions and 20 deletions

43
main.go
View File

@@ -5,6 +5,7 @@ import (
"net/http"
"os"
"strconv"
"flag"
"github.com/joho/godotenv"
@@ -15,7 +16,7 @@ func main() {
var port_number int = 8098 // hard default
var error error
var bind_socket string
fmt.Println("Hello, world. I'm teaching myself Go.")
var seq, port int
error = godotenv.Load() // Load .env file
if error != nil {
@@ -23,16 +24,7 @@ func main() {
}
port_arg := os.Getenv("FIB_PORT")
if port_arg != "" {
fmt.Printf("Loaded port value '%s' from environment.\n",
port_arg)
}
if len(os.Args) > 1 { // commandline overrules env
port_arg = os.Args[1]
fmt.Printf("Loading port value '%s' from command line.\n",
port_arg)
}
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",
@@ -41,9 +33,28 @@ func main() {
os.Exit(0)
}
}
bind_socket = fmt.Sprintf(":%d", port_number)
http.HandleFunc("/fibonacci", functions.HttFibonacci)
http.HandleFunc("/sum", functions.HttSum)
fmt.Printf("Listening on port %d...\n", port_number)
http.ListenAndServe(bind_socket, nil)
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)
}
}
}