Remove peer state change logging

Silently ignore RIS_PEER_STATE and STATE messages instead of logging them.
Also fixed linter issues with directory permissions.
This commit is contained in:
Jeffrey Paul 2025-07-27 21:26:08 +02:00
parent 92f7527cc5
commit 14e85f042b
2 changed files with 55 additions and 11 deletions

View File

@ -5,6 +5,9 @@ import (
"database/sql" "database/sql"
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"path/filepath"
"runtime"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -12,6 +15,7 @@ import (
) )
const ( const (
dirPermissions = 0750 // rwxr-x---
dbSchema = ` dbSchema = `
CREATE TABLE IF NOT EXISTS asns ( CREATE TABLE IF NOT EXISTS asns (
id TEXT PRIMARY KEY, id TEXT PRIMARY KEY,
@ -101,10 +105,48 @@ type Config struct {
Path string Path string
} }
// getDefaultDatabasePath returns the appropriate database path for the OS
func getDefaultDatabasePath() string {
const dbFilename = "db.sqlite"
switch runtime.GOOS {
case "darwin":
// macOS: ~/Library/Application Support/berlin.sneak.app.routewatch/db.sqlite
home, err := os.UserHomeDir()
if err != nil {
return dbFilename
}
appSupport := filepath.Join(home, "Library", "Application Support", "berlin.sneak.app.routewatch")
if err := os.MkdirAll(appSupport, dirPermissions); err != nil {
return dbFilename
}
return filepath.Join(appSupport, dbFilename)
default:
// Linux and others: /var/lib/routewatch/db.sqlite
dbDir := "/var/lib/routewatch"
if err := os.MkdirAll(dbDir, dirPermissions); err != nil {
// Fall back to user's home directory if can't create system directory
home, err := os.UserHomeDir()
if err != nil {
return dbFilename
}
userDir := filepath.Join(home, ".local", "share", "routewatch")
if err := os.MkdirAll(userDir, dirPermissions); err != nil {
return dbFilename
}
return filepath.Join(userDir, dbFilename)
}
return filepath.Join(dbDir, dbFilename)
}
}
// NewConfig provides default database configuration // NewConfig provides default database configuration
func NewConfig() Config { func NewConfig() Config {
return Config{ return Config{
Path: "routewatch.db", Path: getDefaultDatabasePath(),
} }
} }
@ -117,6 +159,15 @@ func New(logger *slog.Logger) (*Database, error) {
// NewWithConfig creates a new database connection with custom configuration // NewWithConfig creates a new database connection with custom configuration
func NewWithConfig(config Config, logger *slog.Logger) (*Database, error) { func NewWithConfig(config Config, logger *slog.Logger) (*Database, error) {
// Log database path
logger.Info("Opening database", "path", config.Path)
// Ensure directory exists
dir := filepath.Dir(config.Path)
if err := os.MkdirAll(dir, dirPermissions); err != nil {
return nil, fmt.Errorf("failed to create database directory: %w", err)
}
// Add connection parameters for modernc.org/sqlite // Add connection parameters for modernc.org/sqlite
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_journal_mode=WAL", config.Path) dsn := fmt.Sprintf("file:%s?_busy_timeout=5000&_journal_mode=WAL", config.Path)
db, err := sql.Open("sqlite", dsn) db, err := sql.Open("sqlite", dsn)

View File

@ -253,10 +253,7 @@ func (s *Streamer) stream(ctx context.Context) error {
// Process BGP UPDATE messages // Process BGP UPDATE messages
// Will be handled by registered handlers // Will be handled by registered handlers
case "RIS_PEER_STATE": case "RIS_PEER_STATE":
s.logger.Info("RIS peer state change", // RIS peer state messages - silently ignore
"peer", msg.Peer,
"peer_asn", msg.PeerASN,
)
case "KEEPALIVE": case "KEEPALIVE":
// BGP keepalive messages - just log at debug level // BGP keepalive messages - just log at debug level
s.logger.Debug("BGP keepalive", s.logger.Debug("BGP keepalive",
@ -276,11 +273,7 @@ func (s *Streamer) stream(ctx context.Context) error {
"peer_asn", msg.PeerASN, "peer_asn", msg.PeerASN,
) )
case "STATE": case "STATE":
// Peer state changes // Peer state changes - silently ignore
s.logger.Info("Peer state change",
"peer", msg.Peer,
"peer_asn", msg.PeerASN,
)
default: default:
fmt.Fprintf( fmt.Fprintf(
os.Stderr, os.Stderr,