mirror of
https://github.com/alvierahman90/gohookr.git
synced 2024-11-23 13:49:52 +00:00
Add required field ListenAddress to config, document in readme
This commit is contained in:
parent
ec030052c3
commit
b31f8496fc
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"ListenAddress": "127.0.0.1:8654",
|
||||||
"Services": {
|
"Services": {
|
||||||
"test": {
|
"test": {
|
||||||
"Script": "./example.sh",
|
"Script": "./example.sh",
|
||||||
|
53
main.go
53
main.go
@ -18,14 +18,20 @@ import (
|
|||||||
|
|
||||||
var config_filename = "/etc/gohookr.json"
|
var config_filename = "/etc/gohookr.json"
|
||||||
var checkSignature = true
|
var checkSignature = true
|
||||||
|
var config Config
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/webhooks/{service}", webhookHandler)
|
r.HandleFunc("/webhooks/{service}", webhookHandler)
|
||||||
|
|
||||||
port := ":80"
|
raw_config, err := ioutil.ReadFile(config_filename)
|
||||||
if p, ok := os.LookupEnv("PORT"); ok {
|
if err != nil {
|
||||||
port = fmt.Sprintf(":%v", p)
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
config = Config{}
|
||||||
|
json.Unmarshal(raw_config, &config)
|
||||||
|
if err := config.Validate(); err != nil {
|
||||||
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if p, ok := os.LookupEnv("CONFIG"); ok {
|
if p, ok := os.LookupEnv("CONFIG"); ok {
|
||||||
@ -36,7 +42,7 @@ func main() {
|
|||||||
checkSignature = p != "true"
|
checkSignature = p != "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(port, r))
|
log.Fatal(http.ListenAndServe(config.ListenAddress, r))
|
||||||
}
|
}
|
||||||
|
|
||||||
func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -48,14 +54,6 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
payload = string(p)
|
payload = string(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_config, err := ioutil.ReadFile(config_filename)
|
|
||||||
if err != nil {
|
|
||||||
writeResponse(w, 500, "Internal Server Error: Could not open config file")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
config := Config{}
|
|
||||||
json.Unmarshal(raw_config, &config)
|
|
||||||
|
|
||||||
// check what service is specified in URL (/webhooks/{service}) and if it exists
|
// check what service is specified in URL (/webhooks/{service}) and if it exists
|
||||||
service, ok := config.Services[string(mux.Vars(r)["service"])]
|
service, ok := config.Services[string(mux.Vars(r)["service"])]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -103,13 +101,32 @@ func getSha256HMACSignature(secret []byte, data string) string {
|
|||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Config) Validate() error {
|
||||||
|
if c.ListenAddress == "" {
|
||||||
|
return requiredFieldError{"ListenAddress", ""}
|
||||||
|
}
|
||||||
|
|
||||||
|
for serviceName, service := range c.Services {
|
||||||
|
if service.Script == "" {
|
||||||
|
return requiredFieldError{"Script", serviceName}
|
||||||
|
}
|
||||||
|
if service.SignatureHeader == "" {
|
||||||
|
return requiredFieldError{"SignatureHeader", serviceName}
|
||||||
|
}
|
||||||
|
if service.Secret == "" {
|
||||||
|
return requiredFieldError{"Secret", serviceName}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Test struct {
|
type Test struct {
|
||||||
Command string
|
Command string
|
||||||
Arguments []string
|
Arguments []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
Gitea bool
|
|
||||||
Script string
|
Script string
|
||||||
Secret string
|
Secret string
|
||||||
SignatureHeader string
|
SignatureHeader string
|
||||||
@ -117,5 +134,15 @@ type Service struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
ListenAddress string
|
||||||
Services map[string]Service
|
Services map[string]Service
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type requiredFieldError struct {
|
||||||
|
fieldName string
|
||||||
|
serviceName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e requiredFieldError) Error() string {
|
||||||
|
return fmt.Sprintf("%v cannot be empty (%v)", e.fieldName, e.serviceName)
|
||||||
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
# gohookr
|
# gohookr
|
||||||
|
|
||||||
A _really_ simple webhook receiver, which listens at `0.0.0.0:<port>/webhooks/<webhook-name>`.
|
A _really_ simple webhook receiver, which listens at `/webhooks/<webhook-name>`.
|
||||||
|
|
||||||
Default port is 80 and can be overriden by setting the environment variable `PORT`.
|
|
||||||
|
|
||||||
Default config path is `/etc/gohookr.conf` and can be overriden by setting environment variable
|
Default config path is `/etc/gohookr.conf` and can be overriden by setting environment variable
|
||||||
`CONFIG`.
|
`CONFIG`.
|
||||||
@ -46,10 +44,15 @@ real tests are run can simply be put before the tests.
|
|||||||
|
|
||||||
## Example Config
|
## Example Config
|
||||||
|
|
||||||
|
Required config keys are `ListenAddress` and `Services`.
|
||||||
|
|
||||||
|
Requried keys per service are `Script`, `Secret`, `SignatureHeader`.
|
||||||
|
|
||||||
An example config file can be found [here](./config.json) but also below:
|
An example config file can be found [here](./config.json) but also below:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
|
"ListenAddress": "127.0.0.1:8654",
|
||||||
"Services": {
|
"Services": {
|
||||||
"test": {
|
"test": {
|
||||||
"Script": "./example.sh",
|
"Script": "./example.sh",
|
||||||
|
Loading…
Reference in New Issue
Block a user