feat: add localized ledger explorer dashboard
This commit is contained in:
1 parent
ce5f8b4a84
commit
5b4cb5a2a3
31 files changed
+5458
-42
No files matched your search
@@ -16,12 +16,25 @@ type Config struct {
|
||||
Database DatabaseConfig `koanf:"database"`
|
||||
}
|
||||
|
||||
type DashboardConfig struct {
|
||||
Environment string `koanf:"environment"`
|
||||
HTTP HTTPConfig `koanf:"http"`
|
||||
Database DatabaseConfig `koanf:"database"`
|
||||
}
|
||||
|
||||
type GRPCConfig struct {
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
ShutdownTimeout time.Duration `koanf:"shutdown-timeout"`
|
||||
}
|
||||
|
||||
type HTTPConfig struct {
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
ReadHeaderTimeout time.Duration `koanf:"read-header-timeout"`
|
||||
ShutdownTimeout time.Duration `koanf:"shutdown-timeout"`
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
@@ -39,21 +52,11 @@ func Load(path string) (*Config, error) {
|
||||
Port: 8600,
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
},
|
||||
Database: DatabaseConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: 5432,
|
||||
Name: "gl_db",
|
||||
User: "postgres",
|
||||
SSLMode: "disable",
|
||||
},
|
||||
Database: defaultDatabaseConfig(),
|
||||
}
|
||||
|
||||
k := koanf.New(".")
|
||||
if err := k.Load(file.Provider(path), toml.Parser()); err != nil {
|
||||
return nil, fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
if err := k.Unmarshal("", cfg); err != nil {
|
||||
return nil, fmt.Errorf("decode config: %w", err)
|
||||
if err := load(path, cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
@@ -61,6 +64,47 @@ func Load(path string) (*Config, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func LoadDashboard(path string) (*DashboardConfig, error) {
|
||||
cfg := &DashboardConfig{
|
||||
Environment: "local",
|
||||
HTTP: HTTPConfig{
|
||||
Host: "0.0.0.0",
|
||||
Port: 8080,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
},
|
||||
Database: defaultDatabaseConfig(),
|
||||
}
|
||||
if err := load(path, cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func load(path string, target any) error {
|
||||
k := koanf.New(".")
|
||||
if err := k.Load(file.Provider(path), toml.Parser()); err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
if err := k.Unmarshal("", target); err != nil {
|
||||
return fmt.Errorf("decode config: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func defaultDatabaseConfig() DatabaseConfig {
|
||||
return DatabaseConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: 5432,
|
||||
Name: "gl_db",
|
||||
User: "postgres",
|
||||
SSLMode: "disable",
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if c.GRPC.Host == "" {
|
||||
return fmt.Errorf("grpc host is required")
|
||||
@@ -71,10 +115,27 @@ func (c *Config) Validate() error {
|
||||
if c.GRPC.ShutdownTimeout <= 0 {
|
||||
return fmt.Errorf("grpc shutdown timeout must be positive")
|
||||
}
|
||||
if c.Database.Host == "" || c.Database.Name == "" || c.Database.User == "" {
|
||||
return validateDatabase(c.Database)
|
||||
}
|
||||
|
||||
func (c *DashboardConfig) Validate() error {
|
||||
if c.HTTP.Host == "" {
|
||||
return fmt.Errorf("http host is required")
|
||||
}
|
||||
if c.HTTP.Port < 0 || c.HTTP.Port > 65535 {
|
||||
return fmt.Errorf("http port must be between 0 and 65535")
|
||||
}
|
||||
if c.HTTP.ReadHeaderTimeout <= 0 || c.HTTP.ShutdownTimeout <= 0 {
|
||||
return fmt.Errorf("http timeouts must be positive")
|
||||
}
|
||||
return validateDatabase(c.Database)
|
||||
}
|
||||
|
||||
func validateDatabase(database DatabaseConfig) error {
|
||||
if database.Host == "" || database.Name == "" || database.User == "" {
|
||||
return fmt.Errorf("database host, name, and user are required")
|
||||
}
|
||||
if c.Database.Port < 1 || c.Database.Port > 65535 {
|
||||
if database.Port < 1 || database.Port > 65535 {
|
||||
return fmt.Errorf("database port must be between 1 and 65535")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -27,6 +27,26 @@ func TestLoadUsesDefaultsAndOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDashboardUsesHTTPDefaultsAndOverrides(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "dashboard.toml")
|
||||
contents := []byte("[http]\nport = 0\nshutdown-timeout = \"3s\"\n")
|
||||
if err := os.WriteFile(path, contents, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cfg, err := LoadDashboard(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.HTTP.Port != 0 || cfg.HTTP.ShutdownTimeout != 3*time.Second || cfg.HTTP.ReadHeaderTimeout != 5*time.Second {
|
||||
t.Fatalf("unexpected http config: %+v", cfg.HTTP)
|
||||
}
|
||||
if cfg.Database.Name != "gl_db" || cfg.Database.Port != 5432 {
|
||||
t.Fatalf("unexpected database defaults: %+v", cfg.Database)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsInvalidConfiguration(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "gl.toml")
|
||||
|
||||
Reference in new issue
Block a user