breve/main.go

65 lines
2.2 KiB
Go
Raw Normal View History

2025-02-07 20:56:09 +01:00
package main
import (
"fmt"
"net/http"
2025-02-07 22:10:22 +01:00
"os"
2025-02-07 20:56:09 +01:00
"time"
2025-02-07 21:58:53 +01:00
"github.com/fiatjaf/eventstore/postgresql"
2025-02-07 20:56:09 +01:00
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/policies"
)
2025-02-07 22:10:22 +01:00
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
2025-02-07 20:56:09 +01:00
func main() {
2025-02-07 22:10:31 +01:00
// Print ASCII art banner
fmt.Print(`
`)
2025-02-07 20:56:09 +01:00
// create the relay instance
relay := khatru.NewRelay()
2025-02-07 22:10:22 +01:00
// set up relay properties with environment variable configuration
relay.Info.Name = getEnv("RELAY_NAME", "Breve Relay")
relay.Info.PubKey = getEnv("RELAY_PUBKEY", "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
relay.Info.Description = getEnv("RELAY_DESCRIPTION", "Breve 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")
2025-02-07 20:56:09 +01:00
2025-02-07 22:10:31 +01:00
// 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)
2025-02-07 22:10:22 +01:00
// Configure PostgreSQL connection with environment variable
postgresURL := getEnv("POSTGRES_URL", "postgres://postgres:postgres@postgres/postgres?sslmode=disable")
db := postgresql.PostgresBackend{DatabaseURL: postgresURL}
2025-02-07 20:56:09 +01:00
if err := db.Init(); err != nil {
panic(err)
}
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
2025-02-07 20:56:09 +01:00
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
relay.ReplaceEvent = append(relay.ReplaceEvent, db.ReplaceEvent)
relay.RejectEvent = append(
relay.RejectEvent,
policies.PreventLargeTags(120),
policies.PreventTimestampsInThePast(time.Hour*2),
policies.PreventTimestampsInTheFuture(time.Minute*30),
)
fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
}