Added dotenv parsing, cleared up style errors.

This commit is contained in:
2025-07-15 14:56:54 +02:00
parent 4b4f8ea20b
commit edd89bd159
4 changed files with 28 additions and 11 deletions

View File

@@ -18,19 +18,19 @@ func HttpGetPostInt(request *http.Request, key string) (value int, error error)
case "POST":
error = request.ParseForm()
if error != nil {
error = fmt.Errorf("Unable to parse HTTP POST input: %s",
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",
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.\n", request.Method)
error = fmt.Errorf("this endpoint does not support %s operations", request.Method)
return
}
}
@@ -49,7 +49,6 @@ func HttFibonacci(w http.ResponseWriter, request *http.Request) {
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
seq, MyFibonacci(seq))
}
return
}
func HttSum(w http.ResponseWriter, request *http.Request) {

2
go.mod
View File

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

2
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=

30
main.go
View File

@@ -6,23 +6,37 @@ import (
"os"
"strconv"
"github.com/joho/godotenv"
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
)
func main() {
var port_number int
var port_number int = 8098 // hard default
var error error
var bind_socket string
fmt.Println("Hello, world. I'm teaching myself Go.")
if len(os.Args) <= 1 {
port_number = 8088
} else {
port_number, error = strconv.Atoi(os.Args[1])
error = godotenv.Load() // Load .env file
if error != nil {
fmt.Printf("I had an error trying to parse %s into a number: %s\n",
os.Args[1],
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)
}