Move config to separate package

This commit is contained in:
Akbar Rahman 2021-08-04 12:04:01 +01:00
parent 2189ee511c
commit 8677f5bfdd
4 changed files with 77 additions and 68 deletions

19
config/command.go Normal file
View File

@ -0,0 +1,19 @@
package config
import "os/exec"
type Command struct {
Program string
Arguments []string
AppendPayload bool
}
func (c Command) Execute(payload string) ([]byte, error) {
arguments := make([]string, 0)
copy(c.Arguments, arguments)
if c.AppendPayload {
arguments = append(arguments, payload)
}
return exec.Command(c.Program, arguments...).Output()
}

39
config/config.go Normal file
View File

@ -0,0 +1,39 @@
package config
import (
"encoding/json"
"fmt"
)
type Config struct {
ListenAddress string
Services map[string]struct {
Script Command
Secret string
SignatureHeader string
Tests []Command
}
}
func (c Config) Validate() error {
if c.ListenAddress == "" {
return requiredFieldError{"ListenAddress", ""}
}
jsonbytes, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(jsonbytes))
for serviceName, service := range c.Services {
if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName}
}
if service.SignatureHeader == "" {
return requiredFieldError{"SignatureHeader", serviceName}
}
if service.Secret == "" {
return requiredFieldError{"Secret", serviceName}
}
}
return nil
}

12
config/errors.go Normal file
View File

@ -0,0 +1,12 @@
package config
import "fmt"
type requiredFieldError struct {
fieldName string
serviceName string
}
func (e requiredFieldError) Error() string {
return fmt.Sprintf("%v cannot be empty (%v)", e.fieldName, e.serviceName)
}

75
main.go
View File

@ -11,14 +11,14 @@ import (
"log"
"net/http"
"os"
"os/exec"
"git.alra.uk/alvierahman90/gohookr/config"
"github.com/gorilla/mux"
)
var config_filename = "/etc/gohookr.json"
var checkSignature = true
var config Config
var c config.Config
func main() {
r := mux.NewRouter()
@ -36,14 +36,13 @@ func main() {
if err != nil {
panic(err.Error())
}
config = Config{}
json.Unmarshal(raw_config, &config)
if err := config.Validate(); err != nil {
json.Unmarshal(raw_config, &c)
if err := c.Validate(); err != nil {
panic(err.Error())
}
log.Fatal(http.ListenAndServe(config.ListenAddress, r))
log.Fatal(http.ListenAndServe(c.ListenAddress, r))
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
@ -56,7 +55,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
}
// check what service is specified in URL (/webhooks/{service}) and if it exists
service, ok := config.Services[string(mux.Vars(r)["service"])]
service, ok := c.Services[string(mux.Vars(r)["service"])]
if !ok {
writeResponse(w, 404, "Service Not Found")
return
@ -101,63 +100,3 @@ func getSha256HMACSignature(secret []byte, data string) string {
io.WriteString(h, data)
return hex.EncodeToString(h.Sum(nil))
}
func (c Config) Validate() error {
if c.ListenAddress == "" {
return requiredFieldError{"ListenAddress", ""}
}
jsonbytes, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(jsonbytes))
for serviceName, service := range c.Services {
if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName}
}
if service.SignatureHeader == "" {
return requiredFieldError{"SignatureHeader", serviceName}
}
if service.Secret == "" {
return requiredFieldError{"Secret", serviceName}
}
}
return nil
}
func (c Command) Execute(payload string) ([]byte, error) {
arguments := make([]string, 0)
copy(c.Arguments, arguments)
if c.AppendPayload {
arguments = append(arguments, payload)
}
return exec.Command(c.Program, arguments...).Output()
}
type Command struct {
Program string
Arguments []string
AppendPayload bool
}
type Service struct {
Script Command
Secret string
SignatureHeader string
Tests []Command
}
type Config struct {
ListenAddress string
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)
}