Skip to content

Commit

Permalink
Merge pull request #9 from numary/dev/cli-flags
Browse files Browse the repository at this point in the history
add http addr flag
  • Loading branch information
altitude authored Jul 6, 2021
2 parents aa33162 + f4fef97 commit 8fc168e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
10 changes: 7 additions & 3 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/numary/ledger/config"
"github.com/numary/ledger/core"
"github.com/numary/ledger/ledger"
"github.com/numary/ledger/ledger/query"
"go.uber.org/fx"
)

type HttpAPI struct {
addr string
engine *gin.Engine
}

func NewHttpAPI(lc fx.Lifecycle, l *ledger.Ledger) *HttpAPI {
func NewHttpAPI(lc fx.Lifecycle, l *ledger.Ledger, c config.Config) *HttpAPI {
r := gin.Default()

r.Use(cors.Default())
Expand Down Expand Up @@ -56,7 +58,8 @@ func NewHttpAPI(lc fx.Lifecycle, l *ledger.Ledger) *HttpAPI {
err := l.Commit([]core.Transaction{t})

c.JSON(200, gin.H{
"ok": err == nil,
"ok": err == nil,
"err": err.Error(),
})
})

Expand All @@ -82,6 +85,7 @@ func NewHttpAPI(lc fx.Lifecycle, l *ledger.Ledger) *HttpAPI {

h := &HttpAPI{
engine: r,
addr: c.Server.Http.BindAddress,
}

lc.Append(fx.Hook{
Expand All @@ -96,5 +100,5 @@ func NewHttpAPI(lc fx.Lifecycle, l *ledger.Ledger) *HttpAPI {
}

func (h *HttpAPI) Start() {
h.engine.Run("localhost:3068")
h.engine.Run(h.addr)
}
29 changes: 27 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"go.uber.org/fx"
)

var (
FlagBindAddr string
)

var root = &cobra.Command{
Use: "numary",
}
Expand All @@ -20,11 +24,20 @@ func Execute() {
Use: "server",
}

server.AddCommand(&cobra.Command{
start := &cobra.Command{
Use: "start",
Run: func(cmd *cobra.Command, args []string) {
app := fx.New(
fx.Provide(
func() *config.Overrides {
v := config.Overrides{}

if cmd.Flag("http-bind-addr").Value.String() != "" {
v["http-bind-addr"] = cmd.Flag("http-bind-addr").Value.String()
}

return &v
},
config.GetConfig,
ledger.NewLedger,
api.NewHttpAPI,
Expand All @@ -35,7 +48,19 @@ func Execute() {

app.Run()
},
})
}

start.Flags().StringVarP(
&FlagBindAddr,
"http-bind-addr",
// no shorthand
"",
// no default
"",
"override http api bind address",
)

server.AddCommand(start)

conf := &cobra.Command{
Use: "config",
Expand Down
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Config struct {
} `json:"storage"`
}

type Overrides map[string]interface{}

func DefaultConfig() Config {
c := Config{}

Expand All @@ -51,7 +53,7 @@ func (c Config) Serialize() string {
return string(b)
}

func GetConfig() Config {
func GetConfig(overrides *Overrides) Config {
candidates := []string{
path.Join("/etc/numary", filename),
}
Expand Down Expand Up @@ -111,5 +113,9 @@ func GetConfig() Config {
fmt.Println("fallback to default config")
}

if addr, ok := (*overrides)["http-bind-addr"]; ok {
conf.Server.Http.BindAddress = addr.(string)
}

return conf
}

0 comments on commit 8fc168e

Please sign in to comment.