added config

This commit is contained in:
A_D 2018-11-16 21:31:45 +02:00
parent 3138ae1fbe
commit 99afed6a1c
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945
4 changed files with 41 additions and 3 deletions

30
config.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"encoding/json"
"io/ioutil"
"os"
)
type Config struct {
Secret string
BindHost string
BindPort string
}
func getConfig() Config {
f, err := os.Open("config.json")
if err != nil {
panic(err)
}
defer f.Close()
out := Config{}
data, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
json.Unmarshal(data, &out)
return out
}

5
config.json.example Normal file
View File

@ -0,0 +1,5 @@
{
"secret": "superSecretSecretIsSecret",
"bind_host": "127.0.0.1",
"bind_port": "5000"
}

View File

@ -1,5 +1,6 @@
package main
func main() {
ListenForWebhook()
config := getConfig()
ListenForWebHook(config)
}

View File

@ -81,7 +81,7 @@ type Person struct {
Username string `json:"username"`
}
func ListenForWebhook() {
func ListenForWebHook(config Config) {
http.HandleFunc(
"/", func(writer http.ResponseWriter, request *http.Request) {
data, err := ioutil.ReadAll(request.Body)
@ -97,7 +97,9 @@ func ListenForWebhook() {
request.Body.Close()
},
)
err := http.ListenAndServe("0.0.0.0:5000", nil)
err := http.ListenAndServe(config.BindHost + ":" + config.BindPort, nil)
if err != nil {
fmt.Println(err)
}