Initial commit providing the basics

This commit is contained in:
Tony Blyler 2021-01-21 23:21:40 -05:00
parent 7112efc5f5
commit 343e1fad53
6 changed files with 278 additions and 1 deletions

28
config/config.go Normal file
View file

@ -0,0 +1,28 @@
package config
import (
"context"
)
// Endpoint to listen for requests and what to execute
type Endpoint struct {
Command string `json:"command"`
Arguments []string `json:"arguments"`
HTTPMethod string `json:"http_method"`
AllowExternalArguments bool `json:"allow_external_arguments"`
AllowStdin bool `json:"allow_stdin"`
DiscardStderr bool `json:"discard_stderr"`
DiscardStdout bool `json:"discard_stdout"`
}
// Config information for httpwrap
type Config struct {
Endpoints map[string]*Endpoint `json:"endpoints"`
ListenAddress string `json:"listen_address"`
ListenPort uint16 `json:"listen_port"`
}
// Source that can get a Config instance
type Source interface {
Config(context.Context) (*Config, error)
}

37
config/json_file.go Normal file
View file

@ -0,0 +1,37 @@
package config
import (
"context"
"encoding/json"
"fmt"
"os"
)
// JSONFileSource gets the config from a JSON file
type JSONFileSource struct {
filePath string
}
// NewJSONFileSource creates a new JSONFileSource instance for the given path
func NewJSONFileSource(filePath string) *JSONFileSource {
return &JSONFileSource{
filePath: filePath,
}
}
// Config gets the config from this file source
func (jfs *JSONFileSource) Config(ctx context.Context) (*Config, error) {
config := &Config{}
data, err := os.ReadFile(jfs.filePath)
if err != nil {
return nil, fmt.Errorf("failed to read JSON config file at %s: %w", jfs.filePath, err)
}
err = json.Unmarshal(data, config)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON config file at %s: %w", jfs.filePath, err)
}
return config, nil
}