Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Nov 21, 2024
1 parent e0fec12 commit a986130
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cmd/system-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func runCli(cCtx *cli.Context) (err error) {
pipeFile := cCtx.String("pipe-file")

// Create configuration file (load from file if requested)
config := systemapi.NewSystemAPIConfig()
config := systemapi.NewConfig()
if configFile != "" {
config, err = systemapi.LoadConfigFromFile(configFile)
config, err = systemapi.NewConfigFromFile(configFile)
if err != nil {
fmt.Println("Error loading config", err)
return err
Expand Down
34 changes: 20 additions & 14 deletions systemapi/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package systemapi
import (
"os"

"github.com/flashbots/system-api/common"
toml "github.com/pelletier/go-toml/v2"
)

var DefaultLogMaxEntries = common.GetEnvInt("MAX_EVENTS", 1000)

type systemAPIConfigGeneral struct {
ListenAddr string `toml:"listen_addr"`
PipeFile string `toml:"pipe_file"`
Expand All @@ -28,33 +31,36 @@ type SystemAPIConfig struct {
FileUploads map[string]string `toml:"file_uploads"`
}

func LoadConfigFromFile(path string) (*SystemAPIConfig, error) {
func NewConfig() *SystemAPIConfig {
return &SystemAPIConfig{
General: systemAPIConfigGeneral{
LogMaxEntries: DefaultLogMaxEntries,
},
Actions: make(map[string]string),
FileUploads: make(map[string]string),
}
}

func NewConfigFromFile(path string) (*SystemAPIConfig, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return LoadConfig(content)
return NewConfigFromTOML(content)
}

func LoadConfig(content []byte) (*SystemAPIConfig, error) {
func NewConfigFromTOML(content []byte) (*SystemAPIConfig, error) {
cfg := &SystemAPIConfig{}
err := toml.Unmarshal(content, cfg)
if err != nil {
return nil, err
}

// Apply default
if cfg.General.LogMaxEntries == 0 {
cfg.General.LogMaxEntries = DefaultLogMaxEntries
}

cfg.loadDefaults()
return cfg, nil
}

func NewSystemAPIConfig() *SystemAPIConfig {
return &SystemAPIConfig{
General: systemAPIConfigGeneral{},
Actions: make(map[string]string),
FileUploads: make(map[string]string),
func (cfg *SystemAPIConfig) loadDefaults() {
if cfg.General.LogMaxEntries == 0 {
cfg.General.LogMaxEntries = DefaultLogMaxEntries
}
}
4 changes: 2 additions & 2 deletions systemapi/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (

func TestLoadConfig(t *testing.T) {
path := "../systemapi-config.toml"
cfg, err := LoadConfigFromFile(path)
cfg, err := NewConfigFromFile(path)
require.NoError(t, err)
require.NotNil(t, cfg)
require.NotEmpty(t, cfg.Actions)
require.Equal(t, "echo test", cfg.Actions["echo_test"])
}

func TestEmptyConfig(t *testing.T) {
cfg, err := LoadConfig([]byte{})
cfg, err := NewConfigFromTOML([]byte{})
require.NoError(t, err)
require.NotNil(t, cfg)
require.Equal(t, DefaultLogMaxEntries, cfg.General.LogMaxEntries)
Expand Down
4 changes: 2 additions & 2 deletions systemapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getTestLogger() *httplog.Logger {

func newTestServer(t *testing.T) *Server {
t.Helper()
srv, err := NewServer(getTestLogger(), NewSystemAPIConfig())
srv, err := NewServer(getTestLogger(), NewConfig())
require.NoError(t, err)
return srv
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestBasicAuth(t *testing.T) {
basicAuthSecretHash := hex.EncodeToString(h.Sum(nil))

// Create the config
cfg := NewSystemAPIConfig()
cfg := NewConfig()
cfg.General.BasicAuthSecretPath = tempDir + "/basic_auth_secret"
cfg.General.BasicAuthSecretSalt = basicAuthSalt

Expand Down
5 changes: 0 additions & 5 deletions systemapi/vars.go

This file was deleted.

0 comments on commit a986130

Please sign in to comment.