don't remember what this does lol

This commit is contained in:
Akbar Rahman 2023-10-28 21:17:05 +01:00
parent 8f0cf776b1
commit 749a3bc9b3
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
6 changed files with 51 additions and 24 deletions

13
config.yml Normal file
View File

@ -0,0 +1,13 @@
listenaddress: 127.0.0.1:8654
services:
test:
script:
program: "echo"
arguments:
- test
tests:
- program: ./example.sh
appendpayload: true
disablesignatureverification: false
signatureheader: test
secret: thisisasecret

View File

@ -1,5 +1,12 @@
package config
import (
"encoding/json"
"io/ioutil"
"gopkg.in/yaml.v3"
)
// The struct that represents the config.json file
type Config struct {
ListenAddress string
@ -33,3 +40,23 @@ func (c Config) Validate() error {
return nil
}
func (c *Config) Load(config_filename string) error {
raw_config, err := ioutil.ReadFile(config_filename)
if err != nil {
return err
}
err = json.Unmarshal(raw_config, &c)
if err == nil {
return c.Validate()
}
err = yaml.Unmarshal(raw_config, &c)
if err == nil {
return c.Validate()
}
return err
}

0
example.sh Executable file → Normal file
View File

5
go.mod
View File

@ -2,4 +2,7 @@ module git.alv.cx/alvierahman90/gohookr
go 1.16
require github.com/gorilla/mux v1.8.0
require (
github.com/gorilla/mux v1.8.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

4
go.sum
View File

@ -1,2 +1,6 @@
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

26
main.go
View File

@ -4,7 +4,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -33,7 +32,7 @@ func main() {
checkSignature = p != "true"
}
c, err := loadConfig(config_filename)
var err = c.Load(config_filename)
if err != nil {
panic(err.Error())
}
@ -49,28 +48,9 @@ func main() {
log.Fatal(http.ListenAndServe(c.ListenAddress, r))
}
func loadConfig(config_filename string) (config.Config, error) {
var c config.Config
raw_config, err := ioutil.ReadFile(config_filename)
if err != nil {
return config.Config{}, err
}
if err := json.Unmarshal(raw_config, &c); err != nil {
return config.Config{}, err
}
if err := c.Validate(); err != nil {
return config.Config{}, err
}
return c, nil
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
c, err := loadConfig(config_filename)
var c config.Config
var err = c.Load(config_filename)
if err != nil {
writeResponse(w, 500, "Unable to read config file")
}