package main import ( "encoding/json" "fmt" "net/http" "os" "strconv" "time" "git.highperfocused.tech/highperfocused/lumina-relay/relay/cache" "git.highperfocused.tech/highperfocused/lumina-relay/relay/trending" "github.com/fiatjaf/eventstore/postgresql" "github.com/fiatjaf/khatru" "github.com/fiatjaf/khatru/policies" ) // Cache for storing generic data like event counts var dataCache = cache.New() const eventCountCacheKey = "total_event_count" const eventCountCacheDuration = 1 * time.Minute func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { return value } return fallback } // Gets total event count, using cache if available func getTotalEventCount(db *postgresql.PostgresBackend) (int, error) { // Try getting from cache first if cachedCount, ok := dataCache.Get(eventCountCacheKey); ok { return cachedCount.(int), nil } // If not in cache, query the database count := 0 row := db.DB.QueryRow("SELECT COUNT(*) FROM event") if err := row.Scan(&count); err != nil { return 0, err } // Update the cache dataCache.Set(eventCountCacheKey, count, eventCountCacheDuration) return count, nil } // Updates event count in the background periodically func startEventCountUpdater(db *postgresql.PostgresBackend) { go func() { ticker := time.NewTicker(eventCountCacheDuration) defer ticker.Stop() for range ticker.C { count := 0 row := db.DB.QueryRow("SELECT COUNT(*) FROM event") if err := row.Scan(&count); err != nil { fmt.Printf("Error updating event count: %v\n", err) continue } dataCache.Set(eventCountCacheKey, count, eventCountCacheDuration) fmt.Printf("Updated event count cache: %d events\n", count) } }() } func main() { fmt.Print(` LUMINA RELAY `) // create the relay instance relay := khatru.NewRelay() // set up relay properties with environment variable configuration relay.Info.Name = getEnv("RELAY_NAME", "LUMINA Relay") relay.Info.PubKey = getEnv("RELAY_PUBKEY", "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798") relay.Info.Description = getEnv("RELAY_DESCRIPTION", "LUMINA Relay") relay.Info.Icon = getEnv("RELAY_ICON", "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images") relay.Info.Software = "lumina-relay" relay.Info.Version = "0.1.0" // Print relay information fmt.Printf("Name: %s\n", relay.Info.Name) fmt.Printf("Public Key: %s\n", relay.Info.PubKey) fmt.Printf("Description: %s\n\n", relay.Info.Description) // Configure PostgreSQL connection with environment variable postgresURL := getEnv("POSTGRES_URL", "postgres://postgres:postgres@postgres/postgres?sslmode=disable") db := postgresql.PostgresBackend{DatabaseURL: postgresURL} if err := db.Init(); err != nil { panic(err) } // Initialize trending system to start background calculations fmt.Println("Initializing trending system...") if err := trending.Initialize(db.DB.DB); err != nil { fmt.Printf("Warning: Error initializing trending system: %v\n", err) } // Initialize event count cache and start periodic updates fmt.Println("Initializing event count cache...") _, err := getTotalEventCount(&db) if err != nil { fmt.Printf("Warning: Error initializing event count cache: %v\n", err) } startEventCountUpdater(&db) relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent) relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents) relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent) relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent) relay.CountEvents = append(relay.CountEvents, db.CountEvents) relay.RejectEvent = append( relay.RejectEvent, policies.PreventLargeTags(120), policies.PreventTimestampsInThePast(time.Hour*2), policies.PreventTimestampsInTheFuture(time.Minute*30), ) mux := relay.Router() // set up other http handlers mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/html") // Get event count from cache count, err := getTotalEventCount(&db) if err != nil { fmt.Printf("Error getting event count: %v\n", err) count = 0 // Fall back to zero if there's an error } // Improved HTML content with link to stats page fmt.Fprintf(w, `
Kind | Count |
---|---|
%s | %d |
Archive of trending posts calculations