-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds enviroment variable override to config loading
- Loading branch information
Showing
13 changed files
with
252 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ go.work.sum | |
khedra | ||
khedra.log | ||
config.yaml | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package app | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/config" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type KhedraApp struct { | ||
Cli *cli.App | ||
Config *config.Config | ||
FileLogger *slog.Logger | ||
ProgLogger *slog.Logger | ||
} | ||
|
||
func NewKhedraApp() *KhedraApp { | ||
cfg := config.MustLoadConfig("config.yaml") | ||
fileLogger, progLogger := config.NewLoggers(cfg.Logging) | ||
cli := initializeCli() | ||
|
||
k := &KhedraApp{ | ||
Config: cfg, | ||
FileLogger: fileLogger, | ||
ProgLogger: progLogger, | ||
Cli: cli, | ||
} | ||
|
||
return k | ||
} | ||
|
||
// Run runs the Khedra cli | ||
func (k *KhedraApp) Run() error { | ||
return k.Cli.Run(os.Args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package app | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func initializeCli() *cli.App { | ||
return &cli.App{ | ||
Name: "khedra", | ||
Usage: "A tool to index, monitor, serve, and share blockchain data", | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "init", | ||
Usage: "Initializes Khedra", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "mode", | ||
Usage: "Initialization mode (all, blooms, none)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: init", "mode", c.String("mode")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "scrape", | ||
Usage: "Controls the blockchain scraper", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "state", | ||
Usage: "Scraper state (on, off)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: scrape", "state", c.String("state")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "api", | ||
Usage: "Starts or stops the API server", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "state", | ||
Usage: "API state (on, off)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: api", "state", c.String("state")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "sleep", | ||
Usage: "Sets the duration between updates", | ||
Flags: []cli.Flag{ | ||
&cli.IntFlag{ | ||
Name: "duration", | ||
Usage: "Sleep duration in seconds", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: sleep", "duration", c.Int("duration")) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/TrueBlocks/trueblocks-khedra/v2/pkg/config" | ||
"github.com/urfave/cli/v2" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
"github.com/TrueBlocks/trueblocks-khedra/v2/app" | ||
) | ||
|
||
type Khedra struct { | ||
Config *config.Config | ||
Logger *slog.Logger | ||
} | ||
|
||
func main() { | ||
cfg := config.MustLoadConfig("config.yaml") | ||
|
||
fileLogger := &lumberjack.Logger{ | ||
Filename: filepath.Join(cfg.Logging.Folder, cfg.Logging.Filename), | ||
MaxSize: cfg.Logging.MaxSizeMb, | ||
MaxBackups: cfg.Logging.MaxBackups, | ||
MaxAge: cfg.Logging.MaxAgeDays, | ||
Compress: cfg.Logging.Compress, | ||
} | ||
// Create a new Khedra app... | ||
k := app.NewKhedraApp() | ||
|
||
fileHandler := slog.NewJSONHandler(fileLogger, nil) | ||
logger := slog.New(fileHandler) | ||
// slog.SetDefault(logger) | ||
|
||
logger.Info("Starting Khedra CLI") | ||
|
||
app := &cli.App{ | ||
Name: "khedra", | ||
Usage: "A CLI tool for Khedra", | ||
Commands: []*cli.Command{ | ||
{ | ||
Name: "init", | ||
Usage: "Initializes Khedra", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "mode", | ||
Usage: "Initialization mode (all, blooms, none)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: init", "mode", c.String("mode")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "scrape", | ||
Usage: "Controls the blockchain scraper", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "state", | ||
Usage: "Scraper state (on, off)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: scrape", "state", c.String("state")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "api", | ||
Usage: "Starts or stops the API server", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "state", | ||
Usage: "API state (on, off)", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: api", "state", c.String("state")) | ||
return nil | ||
}, | ||
}, | ||
{ | ||
Name: "sleep", | ||
Usage: "Sets the duration between updates", | ||
Flags: []cli.Flag{ | ||
&cli.IntFlag{ | ||
Name: "duration", | ||
Usage: "Sleep duration in seconds", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
slog.Info("command calls: sleep", "duration", c.Int("duration")) | ||
return nil | ||
}, | ||
}, | ||
}, | ||
} | ||
k.FileLogger.Info("Khedra started.") | ||
defer k.FileLogger.Info("Khedra stopped.") | ||
|
||
err := app.Run(os.Args) | ||
if err != nil { | ||
log.Fatal(err) | ||
// ...and run it | ||
if err := k.Run(); err != nil { | ||
k.ProgLogger.Error(err.Error()) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.