2025-07-13 22:13:35 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2025-07-24 11:37:38 +02:00
|
|
|
"flag"
|
2025-07-13 22:13:35 +02:00
|
|
|
|
2025-07-15 14:56:54 +02:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
|
2025-07-13 22:13:35 +02:00
|
|
|
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2025-07-15 14:56:54 +02:00
|
|
|
var port_number int = 8098 // hard default
|
2025-07-13 22:13:35 +02:00
|
|
|
var error error
|
|
|
|
|
var bind_socket string
|
2025-07-24 11:37:38 +02:00
|
|
|
var seq, port int
|
2025-07-13 22:13:35 +02:00
|
|
|
|
2025-07-15 14:56:54 +02:00
|
|
|
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 != "" {
|
2025-07-24 11:37:38 +02:00
|
|
|
fmt.Printf("Loaded port value '%s' from environment.\n",port_arg)
|
2025-07-15 14:56:54 +02:00
|
|
|
port_number, error = strconv.Atoi(port_arg)
|
2025-07-13 22:13:35 +02:00
|
|
|
if error != nil {
|
2025-07-15 14:56:54 +02:00
|
|
|
fmt.Printf("I had an error trying to parse '%s' into a number: %s\n",
|
|
|
|
|
port_arg,
|
2025-07-13 22:13:35 +02:00
|
|
|
error)
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 11:37:38 +02:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-13 22:13:35 +02:00
|
|
|
}
|