From 01e22e2b30ccd3cb14e5cf80ab6302fb4cfcc2c3 Mon Sep 17 00:00:00 2001 From: Torben Nehmer Date: Tue, 28 Jun 2022 19:09:15 +0200 Subject: [PATCH] Make Hello World Test URL configurable This should not be active by default for security reasons. --- default-config.yml | 1 + webapi/config.go | 3 +++ webapi/server.go | 8 +++++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/default-config.yml b/default-config.yml index 6797979..d351409 100644 --- a/default-config.yml +++ b/default-config.yml @@ -6,4 +6,5 @@ service: configdir: users/ statedir: state/ webapi: + allowhello: false listenaddress: :8080 diff --git a/webapi/config.go b/webapi/config.go index 93cd1a8..8731b7a 100644 --- a/webapi/config.go +++ b/webapi/config.go @@ -9,6 +9,7 @@ import ( type config struct { ListenAddress string + AllowHello bool } var C config @@ -19,6 +20,7 @@ func init() { func SetConfigDefaults() { viper.SetDefault("WebAPI.ListenAddress", ":8080") + viper.SetDefault("WebAPI.AllowHello", false) } func LoadConfig() { @@ -27,6 +29,7 @@ func LoadConfig() { // https://github.com/spf13/viper/issues/309 // viper.UnmarshalKey("WebAPI", &C) C.ListenAddress = viper.GetString("WebAPI.ListenAddress") + C.AllowHello = viper.GetBool("WebAPI.AllowHello") } func (obj *config) PrettyPrint() string { diff --git a/webapi/server.go b/webapi/server.go index e85678d..1cdb087 100644 --- a/webapi/server.go +++ b/webapi/server.go @@ -13,10 +13,12 @@ var router = mux.NewRouter() func Server() { router.StrictSlash(true) - router.HandleFunc("/hello", handleHello) + if C.AllowHello { + router.HandleFunc("/hello", handleHello) + } log.Printf("Listening to: %s", C.ListenAddress) - go log.Fatal(http.ListenAndServe(C.ListenAddress, router)) + log.Fatal(http.ListenAndServe(C.ListenAddress, router)) } func handleHello(w http.ResponseWriter, r *http.Request) { @@ -25,5 +27,5 @@ func handleHello(w http.ResponseWriter, r *http.Request) { log.Println(r.URL) log.Println(r.Form.Get("Lorem")) log.Println(r.Form) - fmt.Fprint(w, "

Hello World

") + fmt.Fprintf(w, "

Hello World

Lorem = %s

", r.Form.Get("Lorem")) }