Added dotenv parsing, cleared up style errors.
This commit is contained in:
@@ -18,19 +18,19 @@ func HttpGetPostInt(request *http.Request, key string) (value int, error error)
|
|||||||
case "POST":
|
case "POST":
|
||||||
error = request.ParseForm()
|
error = request.ParseForm()
|
||||||
if error != nil {
|
if error != nil {
|
||||||
error = fmt.Errorf("Unable to parse HTTP POST input: %s",
|
error = fmt.Errorf("unable to parse post input: %s",
|
||||||
error.Error())
|
error.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
value, error = strconv.Atoi(request.Form.Get(key))
|
value, error = strconv.Atoi(request.Form.Get(key))
|
||||||
if error != nil {
|
if error != nil {
|
||||||
fmt.Printf("Error parsing parameter '%s'\n", key)
|
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())
|
key, error.Error())
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
default:
|
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
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,6 @@ func HttFibonacci(w http.ResponseWriter, request *http.Request) {
|
|||||||
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
|
fmt.Fprintf(w, "Fibonacci sequence index [%d]: %d\n",
|
||||||
seq, MyFibonacci(seq))
|
seq, MyFibonacci(seq))
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttSum(w http.ResponseWriter, request *http.Request) {
|
func HttSum(w http.ResponseWriter, request *http.Request) {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module darjeeling.systemec.nl/rhouben/fibonacci
|
module darjeeling.systemec.nl/rhouben/fibonacci
|
||||||
|
|
||||||
go 1.24.4
|
go 1.24.4
|
||||||
|
|
||||||
|
require github.com/joho/godotenv v1.5.1
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal 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
30
main.go
@@ -6,23 +6,37 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
|
"darjeeling.systemec.nl/rhouben/fibonacci/functions"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var port_number int
|
var port_number int = 8098 // hard default
|
||||||
var error error
|
var error error
|
||||||
var bind_socket string
|
var bind_socket string
|
||||||
fmt.Println("Hello, world. I'm teaching myself Go.")
|
fmt.Println("Hello, world. I'm teaching myself Go.")
|
||||||
|
|
||||||
if len(os.Args) <= 1 {
|
error = godotenv.Load() // Load .env file
|
||||||
port_number = 8088
|
|
||||||
} else {
|
|
||||||
port_number, error = strconv.Atoi(os.Args[1])
|
|
||||||
|
|
||||||
if error != nil {
|
if error != nil {
|
||||||
fmt.Printf("I had an error trying to parse %s into a number: %s\n",
|
fmt.Printf("Error trying to load .env file: %s\n", error)
|
||||||
os.Args[1],
|
}
|
||||||
|
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)
|
error)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user