Skip to content

Commit

Permalink
relay icons as files in the ./stuff folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Nov 27, 2023
1 parent 2fc52ac commit 62e3190
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ INF running on http://0.0.0.0:5577
This will create a `./data` and a `./stuff` directories under the current directory.

- `./data` is where your database will be placed, with all the Nostr events and indexes. By default it will be an SQLite database under `./data/sqlite`, but you can also specify `--db lmdb` or `--db badger` to use different storage mechanisms.
- `./stuff` is where you should define your custom rules for rejecting events or queries and subscriptions. Two example files will be created and are intended to be modified. They can be modified at runtime.
- `./stuff` is where you should define your custom rules for rejecting events or queries and subscriptions. 2 JavaScript files will be created with example code in them, they are intended to be modified without having to restart the server. Other files also be put in this directory. See the reference:
- `reject-event.js`: this should `export default` a function that takes every incoming `event` as a parameter and returns a string with an error message when that event should be rejected and returns `null` or `undefined` when the event should be accepted. It can also return a `Promise` that resolves to one of these things.
- `reject-filter.js`: this is the same, but takes a `filter` as a parameter and should return an error string if that filter should be rejected.
- `index.html` and other `.html` files: these will be served under the root of your relay HTTP server, if present, but they are not required.
- `icon.png`, `icon.jpg` or `icon.gif`, if present, will be used as the relay NIP-11 icon.

### Other options

Call `jingle --help` to see other possible options. All of these can also be set using environment variables. The most common ones will probably be `--name`, `--pubkey`, `--icon` and `--description`, used to set basic NIP-11 metadata for the relay.
Call `jingle --help` to see other possible options. All of these can also be set using environment variables. The most common ones will probably be `--name`, `--pubkey` and `--description`, used to set basic NIP-11 metadata for the relay.

### Trying it

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.21.4

require (
github.com/fiatjaf/eventstore v0.2.11
github.com/fiatjaf/khatru v0.0.13
github.com/fiatjaf/khatru v0.0.14
github.com/fiatjaf/quickjs-go v0.3.0
github.com/hoisie/mustache v0.0.0-20160804235033-6375acf62c69
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/fasthttp/websocket v1.5.3 h1:TPpQuLwJYfd4LJPXvHDYPMFWbLjsT91n3GpWtCQt
github.com/fasthttp/websocket v1.5.3/go.mod h1:46gg/UBmTU1kUaTcwQXpUxtRwG2PvIZYeA8oL6vF3Fs=
github.com/fiatjaf/eventstore v0.2.11 h1:hb3ImjJAX+aNvMd19sttbZD2PzgohRuZtOgVQLsBXZs=
github.com/fiatjaf/eventstore v0.2.11/go.mod h1:Zx1XqwICh7RxxKLkgc0aXlVo298ABs4W5awP/1/bYYs=
github.com/fiatjaf/khatru v0.0.13 h1:yWKGnAvgNsxqG1OX0K60+yDd0nrcnGZrAPw14cgA+Tg=
github.com/fiatjaf/khatru v0.0.13/go.mod h1:ShdaRrzoZpCKjillsH7OS1O8xnxQcJP4bA5niBPUb/o=
github.com/fiatjaf/khatru v0.0.14 h1:hHHYH5uV7ANBxumxV2aBGFbZ4c/f1qMOrAMPNln7P74=
github.com/fiatjaf/khatru v0.0.14/go.mod h1:ShdaRrzoZpCKjillsH7OS1O8xnxQcJP4bA5niBPUb/o=
github.com/fiatjaf/quickjs-go v0.3.0 h1:wLvvummPLBrH3JDO4wHCC+uYVIDEHuUT2Jz1Ha65i3A=
github.com/fiatjaf/quickjs-go v0.3.0/go.mod h1:lYXCC+EmJ6YxXs128amkCmMXnimlO4dFCqY6fjwGC0M=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down
28 changes: 16 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"
"os"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/fiatjaf/khatru"
"github.com/hoisie/mustache"
"github.com/kelseyhightower/envconfig"
"github.com/nbd-wtf/go-nostr/nip11"
"github.com/rs/zerolog"

"github.com/urfave/cli/v2"
Expand All @@ -21,11 +23,10 @@ import (
type Settings struct {
Host string `envconfig:"HOST" default:""`
Port string `envconfig:"PORT" default:"5577"`
Domain string `envconfig:"DOMAIN"`
ServiceURL string `envconfig:"SERVICE_URL"`
RelayName string `envconfig:"RELAY_NAME" default:"jinglebells"`
RelayPubkey string `envconfig:"RELAY_PUBKEY" default:"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"`
RelayDescription string `envconfig:"RELAY_DESCRIPTION" default:"an experimental relay"`
RelayIcon string `envconfig:"RELAY_ICON" default:"http://icons.iconarchive.com/icons/paomedia/small-n-flat/512/bell-icon.png"`
DatabaseBackend string `envconfig:"DATABASE" default:"sqlite"`
DatabaseURL string `envconfig:"DATABASE_URL"`
CustomDirectory string `envconfig:"DATA_DIRECTORY" default:"stuff"`
Expand Down Expand Up @@ -70,6 +71,12 @@ func main() {
Destination: &s.Port,
Category: CATEGORY_NETWORK,
},
&cli.StringFlag{
Name: "service-url",
Usage: "base url of the relay, with http(s):// prefix",
Destination: &s.ServiceURL,
Category: CATEGORY_NETWORK,
},
&cli.StringFlag{
Name: "name",
Usage: "relay name",
Expand All @@ -84,13 +91,6 @@ func main() {
Destination: &s.RelayDescription,
Category: CATEGORY_COMMON,
},
&cli.StringFlag{
Name: "icon",
Usage: "relay icon image",
Value: s.RelayIcon,
Destination: &s.RelayIcon,
Category: CATEGORY_COMMON,
},
&cli.StringFlag{
Name: "pubkey",
Usage: "relay owner pubkey",
Expand Down Expand Up @@ -156,7 +156,12 @@ func main() {
relay.Info.Name = s.RelayName
relay.Info.PubKey = s.RelayPubkey
relay.Info.Description = s.RelayDescription
relay.Info.Icon = s.RelayIcon
relay.OverwriteRelayInformation = append(relay.OverwriteRelayInformation,
func(ctx context.Context, r *http.Request, info nip11.RelayInformationDocument) nip11.RelayInformationDocument {
info.Icon = getIconURL(r)
return info
},
)

// basic relay methods with custom stores
if err := os.MkdirAll(s.DataDirectory, 0700); err != nil {
Expand Down Expand Up @@ -206,8 +211,6 @@ func main() {
rejectFilter,
)

// nip11

// other http handlers
log.Info().Msgf("checking for html and assets under ./%s/", s.CustomDirectory)
homePath := filepath.Join(s.CustomDirectory, "index.html")
Expand Down Expand Up @@ -239,6 +242,7 @@ description:

if filepath.Ext(filePath) == ".html" {
w.Header().Set("content-type", "text/html")
relay.Info.Icon = getIconURL(r)
fmt.Fprint(w, mustache.RenderFile(filePath, relay.Info))
} else {
http.ServeFile(w, r, filePath)
Expand Down
43 changes: 43 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)

func getServiceBaseURL(r *http.Request) string {
if s.ServiceURL != "" {
return s.ServiceURL
}
host := r.Header.Get("X-Forwarded-Host")
if host == "" {
host = r.Host
}
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
if host == "localhost" {
proto = "http"
} else if strings.Index(host, ":") != -1 {
// has a port number
proto = "http"
} else if _, err := strconv.Atoi(strings.ReplaceAll(host, ".", "")); err == nil {
// it's a naked IP
proto = "http"
} else {
proto = "https"
}
}
return proto + "://" + host
}

func getIconURL(r *http.Request) string {
for _, possibleIcon := range []string{"icon.png", "icon.jpg", "icon.jpeg", "icon.gif"} {
if _, err := os.Stat(filepath.Join(s.CustomDirectory, possibleIcon)); err == nil {
return getServiceBaseURL(r) + "/" + possibleIcon
}
}
return ""
}

0 comments on commit 62e3190

Please sign in to comment.