Make Hello World Test URL configurable

This should not be active by default for security reasons.
This commit is contained in:
Torben Nehmer 2022-06-28 19:09:15 +02:00
parent 9b29d9213c
commit 01e22e2b30
3 changed files with 9 additions and 3 deletions

View File

@ -6,4 +6,5 @@ service:
configdir: users/
statedir: state/
webapi:
allowhello: false
listenaddress: :8080

View File

@ -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 {

View File

@ -13,10 +13,12 @@ var router = mux.NewRouter()
func Server() {
router.StrictSlash(true)
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, "<html><body><p>Hello World</p></body></html>")
fmt.Fprintf(w, "<html><body><h1>Hello World</h1><p>Lorem = %s</p></body></html>", r.Form.Get("Lorem"))
}