-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
82 lines (66 loc) · 1.99 KB
/
app.go
File metadata and controls
82 lines (66 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"html/template"
"log"
"time"
"github.com/go-chi/chi/v5"
)
// App holds the application state and dependencies
type App struct {
// Gateway client for obsrvr-gateway API
gateway *GatewayClient
// In-memory cache for ledger queries (helps with remote catalog latency)
ledgerCache *LedgerCache
router *chi.Mux
templates *template.Template
config *Config
}
// NewApp initializes and returns a new App instance
func NewApp(config *Config) (*App, error) {
log.Println("Initializing Obsrvr Intelligence Console (Gateway Mode)...")
log.Printf("Default network: %s", config.DefaultNetwork)
// Initialize gateway client (required)
if config.GatewayAPIKey == "" {
return nil, fmt.Errorf("GATEWAY_API_KEY is required")
}
log.Printf("Using obsrvr-gateway at %s", config.GatewayBaseURL)
gateway := NewGatewayClient(config.GatewayBaseURL, config.GatewayAPIKey)
// Load templates
templates, err := template.ParseGlob("templates/*.html")
if err != nil {
return nil, fmt.Errorf("failed to load templates: %w", err)
}
_, err = templates.ParseGlob("templates/partials/*.html")
if err != nil {
return nil, fmt.Errorf("failed to load partial templates: %w", err)
}
_, err = templates.ParseGlob("templates/explorer/*.html")
if err != nil {
return nil, fmt.Errorf("failed to load explorer templates: %w", err)
}
// Initialize cache with 5 minute TTL
ledgerCache := NewLedgerCache(5 * time.Minute)
// Start cache cleanup goroutine
go func() {
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()
for range ticker.C {
ledgerCache.CleanExpired()
}
}()
app := &App{
gateway: gateway,
ledgerCache: ledgerCache,
templates: templates,
config: config,
}
app.setupRouter()
log.Println("✓ Gateway mode enabled")
log.Println("✓ Application initialized successfully")
return app, nil
}
// Close cleanly shuts down the application (no-op for gateway mode)
func (app *App) Close() {
// No database connections to close in gateway mode
}