mirror of
https://github.com/aljazceru/khatru.git
synced 2026-01-21 14:14:27 +01:00
move example relay implementations to examples/ folder.
This commit is contained in:
1
examples/basic/.gitignore
vendored
Normal file
1
examples/basic/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
relayer-basic
|
||||
8
examples/basic/Dockerfile
Normal file
8
examples/basic/Dockerfile
Normal file
@@ -0,0 +1,8 @@
|
||||
FROM golang:1.18
|
||||
|
||||
WORKDIR /go/src/app
|
||||
COPY ./ .
|
||||
|
||||
RUN go get -d -v ./...
|
||||
RUN go install -v ./...
|
||||
RUN cd basic && make
|
||||
2
examples/basic/Makefile
Normal file
2
examples/basic/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
relayer-basic: $(shell find .. -name "*.go")
|
||||
CC=$$(which musl-gcc) go build -ldflags='-s -w -linkmode external -extldflags "-static"' -o ./relayer-basic
|
||||
24
examples/basic/README.md
Normal file
24
examples/basic/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
relayer basic
|
||||
=============
|
||||
|
||||
- a basic relay implementation based on relayer.
|
||||
- uses postgres, which I think must be over version 12 since it uses generated columns.
|
||||
- it has some antispam limits, tries to delete old stuff so things don't get out of control, and some other small optimizations.
|
||||
|
||||
running
|
||||
-------
|
||||
|
||||
grab a binary from the releases page and run it with the environment variable POSTGRESQL_DATABASE set to some postgres url:
|
||||
|
||||
POSTGRESQL_DATABASE=postgres://name:pass@localhost:5432/dbname ./relayer-basic
|
||||
|
||||
it also accepts a HOST and a PORT environment variables.
|
||||
|
||||
compiling
|
||||
---------
|
||||
|
||||
if you know Go you already know this:
|
||||
|
||||
go install github.com/fiatjaf/relayer/basic
|
||||
|
||||
or something like that.
|
||||
32
examples/basic/docker-compose.yml
Normal file
32
examples/basic/docker-compose.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
version: "3.8"
|
||||
services:
|
||||
|
||||
relay:
|
||||
build:
|
||||
context: ../
|
||||
dockerfile: ./basic/Dockerfile
|
||||
environment:
|
||||
PORT: 2700
|
||||
POSTGRESQL_DATABASE: postgres://nostr:nostr@postgres:5432/nostr?sslmode=disable
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
ports:
|
||||
- 2700:2700
|
||||
command: "./basic/relayer-basic"
|
||||
|
||||
postgres:
|
||||
image: postgres
|
||||
restart: always
|
||||
environment:
|
||||
POSTGRES_DB: nostr
|
||||
POSTGRES_USER: nostr
|
||||
POSTGRES_PASSWORD: nostr
|
||||
POSTGRES_HOST_AUTH_METHOD: trust # allow all connections without a password. This is *not* recommended for prod
|
||||
ports:
|
||||
- 5432:5432
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U nostr"] # database username here - nostr, should be changed if other user
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
73
examples/basic/main.go
Normal file
73
examples/basic/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/fiatjaf/relayer/v2"
|
||||
"github.com/fiatjaf/relayer/v2/storage/postgresql"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
)
|
||||
|
||||
type Relay struct {
|
||||
PostgresDatabase string `envconfig:"POSTGRESQL_DATABASE"`
|
||||
|
||||
storage *postgresql.PostgresBackend
|
||||
}
|
||||
|
||||
func (r *Relay) Name() string {
|
||||
return "BasicRelay"
|
||||
}
|
||||
|
||||
func (r *Relay) Storage(ctx context.Context) relayer.Storage {
|
||||
return r.storage
|
||||
}
|
||||
|
||||
func (r *Relay) Init() error {
|
||||
err := envconfig.Process("", r)
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't process envconfig: %w", err)
|
||||
}
|
||||
|
||||
// every hour, delete all very old events
|
||||
go func() {
|
||||
db := r.Storage(context.TODO()).(*postgresql.PostgresBackend)
|
||||
|
||||
for {
|
||||
time.Sleep(60 * time.Minute)
|
||||
db.DB.Exec(`DELETE FROM event WHERE created_at < $1`, time.Now().AddDate(0, -3, 0).Unix()) // 3 months
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool {
|
||||
// block events that are too large
|
||||
jsonb, _ := json.Marshal(evt)
|
||||
if len(jsonb) > 10000 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := Relay{}
|
||||
if err := envconfig.Process("", &r); err != nil {
|
||||
log.Fatalf("failed to read from env: %v", err)
|
||||
return
|
||||
}
|
||||
r.storage = &postgresql.PostgresBackend{DatabaseURL: r.PostgresDatabase}
|
||||
server, err := relayer.NewServer(&r)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create server: %v", err)
|
||||
}
|
||||
if err := server.Start("0.0.0.0", 7447); err != nil {
|
||||
log.Fatalf("server terminated: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user