fixed numerous linter issues
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
// Package main is the entry point for the API server.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/kjannette/koin-ping/backend-go/internal/config"
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/kjannette/koin-ping/backend-go/internal/models"
|
||||
)
|
||||
|
||||
//nolint:funlen
|
||||
func main() {
|
||||
_ = godotenv.Load() // .env is optional; env vars can also be set externally
|
||||
|
||||
@@ -27,12 +28,13 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
if err := firebase.Init(cfg.FirebaseProjectID); err != nil {
|
||||
log.Fatalf("Failed to initialize Firebase: %v", err)
|
||||
}
|
||||
|
||||
defer database.Close()
|
||||
|
||||
addressModel := models.NewAddressModel(pool)
|
||||
alertRuleModel := models.NewAlertRuleModel(pool)
|
||||
alertEventModel := models.NewAlertEventModel(pool)
|
||||
@@ -51,23 +53,34 @@ func main() {
|
||||
mux.HandleFunc("GET "+b+"/status", handlers.SystemStatus)
|
||||
|
||||
// Authenticated routes — addresses
|
||||
mux.Handle("POST "+b+"/addresses", middleware.Authenticate(http.HandlerFunc(addressHandler.Create)))
|
||||
mux.Handle("GET "+b+"/addresses", middleware.Authenticate(http.HandlerFunc(addressHandler.List)))
|
||||
mux.Handle("DELETE "+b+"/addresses/{addressId}", middleware.Authenticate(http.HandlerFunc(addressHandler.Remove)))
|
||||
mux.Handle("POST "+b+"/addresses",
|
||||
middleware.Authenticate(http.HandlerFunc(addressHandler.Create)))
|
||||
mux.Handle("GET "+b+"/addresses",
|
||||
middleware.Authenticate(http.HandlerFunc(addressHandler.List)))
|
||||
mux.Handle("DELETE "+b+"/addresses/{addressId}",
|
||||
middleware.Authenticate(http.HandlerFunc(addressHandler.Remove)))
|
||||
|
||||
// Authenticated routes — alert rules
|
||||
mux.Handle("POST "+b+"/addresses/{addressId}/alerts", middleware.Authenticate(http.HandlerFunc(alertRuleHandler.Create)))
|
||||
mux.Handle("GET "+b+"/addresses/{addressId}/alerts", middleware.Authenticate(http.HandlerFunc(alertRuleHandler.ListByAddress)))
|
||||
mux.Handle("PATCH "+b+"/alerts/{alertId}", middleware.Authenticate(http.HandlerFunc(alertRuleHandler.UpdateStatus)))
|
||||
mux.Handle("DELETE "+b+"/alerts/{alertId}", middleware.Authenticate(http.HandlerFunc(alertRuleHandler.Remove)))
|
||||
mux.Handle("POST "+b+"/addresses/{addressId}/alerts",
|
||||
middleware.Authenticate(http.HandlerFunc(alertRuleHandler.Create)))
|
||||
mux.Handle("GET "+b+"/addresses/{addressId}/alerts",
|
||||
middleware.Authenticate(http.HandlerFunc(alertRuleHandler.ListByAddress)))
|
||||
mux.Handle("PATCH "+b+"/alerts/{alertId}",
|
||||
middleware.Authenticate(http.HandlerFunc(alertRuleHandler.UpdateStatus)))
|
||||
mux.Handle("DELETE "+b+"/alerts/{alertId}",
|
||||
middleware.Authenticate(http.HandlerFunc(alertRuleHandler.Remove)))
|
||||
|
||||
// Authenticated routes — alert events
|
||||
mux.Handle("GET "+b+"/alert-events", middleware.Authenticate(http.HandlerFunc(alertEventHandler.List)))
|
||||
mux.Handle("GET "+b+"/alert-events",
|
||||
middleware.Authenticate(http.HandlerFunc(alertEventHandler.List)))
|
||||
|
||||
// Authenticated routes — notification config
|
||||
mux.Handle("GET "+b+"/notification-config", middleware.Authenticate(http.HandlerFunc(notifConfigHandler.GetConfig)))
|
||||
mux.Handle("PUT "+b+"/notification-config", middleware.Authenticate(http.HandlerFunc(notifConfigHandler.UpdateConfig)))
|
||||
mux.Handle("DELETE "+b+"/notification-config", middleware.Authenticate(http.HandlerFunc(notifConfigHandler.DeleteConfig)))
|
||||
mux.Handle("GET "+b+"/notification-config",
|
||||
middleware.Authenticate(http.HandlerFunc(notifConfigHandler.GetConfig)))
|
||||
mux.Handle("PUT "+b+"/notification-config",
|
||||
middleware.Authenticate(http.HandlerFunc(notifConfigHandler.UpdateConfig)))
|
||||
mux.Handle("DELETE "+b+"/notification-config",
|
||||
middleware.Authenticate(http.HandlerFunc(notifConfigHandler.DeleteConfig)))
|
||||
|
||||
handler := corsMiddleware(mux)
|
||||
|
||||
@@ -76,9 +89,12 @@ func main() {
|
||||
log.Printf("API base path: %s", cfg.APIBasePath)
|
||||
log.Printf("Environment: %s", cfg.NodeEnv)
|
||||
|
||||
if err := http.ListenAndServe(addr, handler); err != nil {
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: handler,
|
||||
}
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
log.Fatalf("Server failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +106,7 @@ func corsMiddleware(next http.Handler) http.Handler {
|
||||
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package main is the entry point for the blockchain observer poller.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -18,6 +19,14 @@ import (
|
||||
"github.com/kjannette/koin-ping/backend-go/internal/services"
|
||||
)
|
||||
|
||||
const (
|
||||
// separatorWidth is the number of characters in log separator lines.
|
||||
separatorWidth = 60
|
||||
// msPerSecond converts milliseconds to seconds.
|
||||
msPerSecond = 1000
|
||||
)
|
||||
|
||||
//nolint:funlen
|
||||
func main() {
|
||||
_ = godotenv.Load()
|
||||
|
||||
@@ -34,13 +43,14 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database: %v", err)
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
eth, err := ethereum.NewJsonRpcEthereum(cfg.EthRPCURL)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create Ethereum observer: %v", err)
|
||||
}
|
||||
|
||||
defer database.Close()
|
||||
|
||||
addressModel := models.NewAddressModel(pool)
|
||||
alertRuleModel := models.NewAlertRuleModel(pool)
|
||||
alertEventModel := models.NewAlertEventModel(pool)
|
||||
@@ -58,20 +68,20 @@ func main() {
|
||||
go func() {
|
||||
<-sigCh
|
||||
log.Println()
|
||||
log.Println(strings.Repeat("=", 60))
|
||||
log.Println(strings.Repeat("=", separatorWidth))
|
||||
log.Println("Shutting down poller gracefully...")
|
||||
log.Println(strings.Repeat("=", 60))
|
||||
log.Println(strings.Repeat("=", separatorWidth))
|
||||
cancel()
|
||||
}()
|
||||
|
||||
interval := time.Duration(cfg.PollIntervalMS) * time.Millisecond
|
||||
|
||||
log.Println(strings.Repeat("=", 60))
|
||||
log.Println(strings.Repeat("=", separatorWidth))
|
||||
log.Println("Koin Ping Observer Poller Starting")
|
||||
log.Println(strings.Repeat("=", 60))
|
||||
log.Println(strings.Repeat("=", separatorWidth))
|
||||
log.Printf("RPC URL: %s", cfg.EthRPCURL)
|
||||
log.Printf("Poll Interval: %dms (%ds)", cfg.PollIntervalMS, cfg.PollIntervalMS/1000)
|
||||
log.Println(strings.Repeat("=", 60))
|
||||
log.Printf("Poll Interval: %dms (%ds)", cfg.PollIntervalMS, cfg.PollIntervalMS/msPerSecond)
|
||||
log.Println(strings.Repeat("=", separatorWidth))
|
||||
|
||||
runCycle(ctx, observer, evaluator)
|
||||
|
||||
@@ -82,6 +92,7 @@ func main() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("Poller stopped")
|
||||
|
||||
return
|
||||
case <-ticker.C:
|
||||
runCycle(ctx, observer, evaluator)
|
||||
@@ -89,19 +100,25 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func runCycle(ctx context.Context, observer *services.ObserverService, evaluator *services.EvaluatorService) {
|
||||
func runCycle(
|
||||
ctx context.Context,
|
||||
observer *services.ObserverService,
|
||||
evaluator *services.EvaluatorService,
|
||||
) {
|
||||
startTime := time.Now()
|
||||
log.Printf("[%s] Starting observation cycle...", time.Now().UTC().Format(time.RFC3339))
|
||||
|
||||
observations, err := observer.RunOnce(ctx)
|
||||
if err != nil {
|
||||
log.Printf("[%s] Observation cycle failed: %v", time.Now().UTC().Format(time.RFC3339), err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
alertsFired, err := evaluator.Evaluate(ctx, observations)
|
||||
if err != nil {
|
||||
log.Printf("[%s] Evaluation failed: %v", time.Now().UTC().Format(time.RFC3339), err)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user