50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
|
|
"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
|
|
fmt.Println("Hello, world. I'm teaching myself Go.")
|
|
|
|
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)
|
|
}
|
|
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 != "" {
|
|
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)
|
|
}
|
|
}
|
|
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)
|
|
}
|