Skip to content

Commit

Permalink
chore: use json config
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jan 20, 2025
1 parent b46d618 commit 78e9d3b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 758 deletions.
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:

server:
image: docker.io/nats:latest
ports:
- "4222:4222"
- "8222:8222"
volumes:
- server:/data
configs:
- source: sys.conf
target: /examples/sys.conf
command:
- '-c'
- '/examples/sys.conf'

configs:
sys.conf:
file: './examples/sys.conf'

volumes:
server:
4 changes: 4 additions & 0 deletions examples/sys.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": 4222,
"http_port": 8223
}
28 changes: 22 additions & 6 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package config

import (
"encoding/json"

"github.com/zeiss/pkg/cast"
)

// New returns a new Config object.
func New() *Config {
return &Config{}
}

// Default ...
func Default() *Config {
return &Config{
Host: cast.Ptr("0.0.0.0"),
Port: cast.Ptr(4222),
}
}

// Config ...
type Config struct {
// Host ...
Host string `json:"host,omitempty"`
Host *string `json:"host,omitempty"`
// Port ...
Port *int `json:"port,omitempty"`
// HTTPPort ...
HTTPPort *int `json:"http_port,omitempty"`
// Gateway ...
Gateway *Gateway `json:"gateway,omitempty"`
}

// Marshal ...
func (c *Config) Marshal() ([]byte, error) {
return json.Marshal(c)
}

// Gateway ...
type Gateway struct {
// Name ...
Expand Down Expand Up @@ -56,11 +77,6 @@ type AuthCallout struct {
XKey string `json:"xkey"`
}

// Marhshal ...
func (c *Config) Marshal() ([]byte, error) {
return nil, nil
}

// Property ...
type Property struct {
// Name ...
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ func TestNew(t *testing.T) {
cfg := config.New()
require.NotNil(t, cfg)
}

func TestDefault(t *testing.T) {
t.Parallel()

cfg := config.Default()
require.NotNil(t, cfg)

json, err := cfg.Marshal()
require.NoError(t, err)
require.JSONEq(t, `{"host":"0.0.0.0","port":4222}`, string(json))
}
Loading

0 comments on commit 78e9d3b

Please sign in to comment.