Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cmd/arkd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ const ONE_BTC = float64(1_00_000_000)

// commands
var (
startCmd = &cli.Command{
Name: "start",
Usage: "Starts the arkd server",
Action: startAction,
// startCmdWrapper is a wrapper around the start command defined in start.go
// TODO: This should disappear when we migrate all sub-commands from urfave/cli to spf13/cobra.
startCmdWrapper = &cli.Command{
Name: "start",
Usage: "Starts the arkd server",
SkipFlagParsing: true,
Action: func(_ *cli.Context) error {
return startCmd.Execute()
},
}

walletCmd = &cli.Command{
Expand Down
57 changes: 2 additions & 55 deletions cmd/arkd/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package main

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/arkade-os/arkd/internal/config"
grpcservice "github.com/arkade-os/arkd/internal/interface/grpc"
"github.com/arkade-os/arkd/internal/telemetry"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -23,53 +17,6 @@ const (
tlsCertFile = "cert.pem"
)

func startAction(_ *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("invalid config: %s", err)
}

log.SetLevel(log.Level(cfg.LogLevel))
if cfg.OtelCollectorEndpoint != "" {
log.AddHook(telemetry.NewOTelHook())
}

svcConfig := grpcservice.Config{
Datadir: cfg.Datadir,
Port: cfg.Port,
AdminPort: cfg.AdminPort,
NoTLS: cfg.NoTLS,
NoMacaroons: cfg.NoMacaroons,
TLSExtraIPs: cfg.TLSExtraIPs,
TLSExtraDomains: cfg.TLSExtraDomains,
HeartbeatInterval: cfg.HeartbeatInterval,
EnablePprof: cfg.EnablePprof,
}

svc, err := grpcservice.NewService(Version, svcConfig, cfg)
if err != nil {
return err
}

log.Infof("ark server config: %s", cfg)

log.Debug("starting service...")
if err := svc.Start(); err != nil {
return err
}

log.RegisterExitHandler(svc.Stop)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, os.Interrupt)
<-sigChan

log.Debug("shutting down service...")
log.Exit(0)

return nil
}

func main() {
app := cli.NewApp()
app.Version = Version
Expand All @@ -78,7 +25,7 @@ func main() {
app.UsageText = "Run the Ark Server with:\n\tarkd\nManage the Ark Server with:\n\tarkd [global options] command [command options]"
app.Commands = append(
app.Commands,
startCmd,
startCmdWrapper,
versionCmd,
walletCmd,
signerCmd,
Expand All @@ -93,7 +40,7 @@ func main() {
convictionsCmd,
)

app.DefaultCommand = startCmd.Name
app.DefaultCommand = startCmd.Use
app.Flags = append(app.Flags, urlFlag, datadirFlag, macaroonFlag)

if err := app.Run(os.Args); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions cmd/arkd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"strings"

"github.com/spf13/viper"
)

// EnvReplacer replaces `-` to `_`.
// This is used to map flag like `--my-param` to environment variables like `MY_PARAM`.
var envReplacer = strings.NewReplacer("-", "_")

func init() {
viper.SetEnvPrefix("ARKD")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(envReplacer)
}
94 changes: 94 additions & 0 deletions cmd/arkd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/arkade-os/arkd/internal/config"
grpcservice "github.com/arkade-os/arkd/internal/interface/grpc"
"github.com/arkade-os/arkd/internal/telemetry"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)

var startCmd = &cobra.Command{
Use: "start",
Short: "Start the Ark Server",
Long: `Start the Ark Server daemon to serve gRPC requests.`,
RunE: func(cmd *cobra.Command, args []string) error {
return startAction(nil)
},
}

// startAction is a wrapper to a cobra command that starts the arkd server.
func startAction(_ *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return fmt.Errorf("invalid config: %s", err)
}

log.SetLevel(log.Level(cfg.LogLevel))
if cfg.OtelCollectorEndpoint != "" {
log.AddHook(telemetry.NewOTelHook())
}

svcConfig := grpcservice.Config{
Datadir: cfg.Datadir,
Port: cfg.Port,
AdminPort: cfg.AdminPort,
NoTLS: cfg.NoTLS,
NoMacaroons: cfg.NoMacaroons,
TLSExtraIPs: cfg.TLSExtraIPs,
TLSExtraDomains: cfg.TLSExtraDomains,
HeartbeatInterval: cfg.HeartbeatInterval,
EnablePprof: cfg.EnablePprof,
}

svc, err := grpcservice.NewService(Version, svcConfig, cfg)
if err != nil {
return err
}

log.Infof("ark server config: %s", cfg)

log.Debug("starting service...")
if err := svc.Start(); err != nil {
return err
}

log.RegisterExitHandler(svc.Stop)

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, os.Interrupt)
<-sigChan

log.Debug("shutting down service...")
log.Exit(0)

return nil
}

func init() {
config.SetupFlags(startCmd)

// TODO: Move this to the root cmd upon urfave/cli -> spf13/cobra migration.
_ = viper.BindPFlags(startCmd.Flags())

// Update the flag's Usage to display the corresponding env var name when --help is invoked.
startCmd.Flags().VisitAll(func(f *pflag.Flag) {
envVar := strings.ToUpper(envReplacer.Replace(f.Name))
envUsage := fmt.Sprintf("[ARKD_%s]", envVar)

if f.Usage == "" {
f.Usage = envUsage
} else {
f.Usage = f.Usage + " " + envUsage
}
})
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/redis/go-redis/v9 v9.10.0
github.com/shopspring/decimal v1.2.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.20.1
github.com/sqlc-dev/pqtype v0.3.0
github.com/stretchr/testify v1.11.1
Expand Down Expand Up @@ -122,6 +123,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect
Expand Down Expand Up @@ -256,7 +258,7 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/spf13/pflag v1.0.9
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyf
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
Expand Down Expand Up @@ -555,10 +557,12 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
Expand Down Expand Up @@ -701,6 +705,7 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20170613210332-850760c427c5/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
Loading
Loading