Skip to content

Commit

Permalink
docs: add khatru29 guide and update khatru29 and strfry29 links (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jchiarulli authored Oct 12, 2024
1 parent 585c940 commit 92b4d4d
Show file tree
Hide file tree
Showing 16 changed files with 520 additions and 15 deletions.
171 changes: 171 additions & 0 deletions docs/relay/khatru29/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Build

We're now going to discuss how to build the binary for the [groups.fiatjaf.com](https://github.com/fiatjaf/relay29/tree/master/examples/groups.fiatjaf.com "groups.fiatjaf.com") example provided by the relay29 repository which uses the [Khatru29](https://pkg.go.dev/github.com/fiatjaf/relay29/khatru29 "Khatru29") wrapper.

## Dependencies

Before building the binary, we need to install Go and Git.

We need to install Git to be able to clone the relay29 repository:

```bash
apt install git
```

We're now going to discuss how to install Go on your relay.

First, you need to determine which version of Go you want to install which you can do by going to the [All releases](https://go.dev/dl "All releases") page on the Go website.

Make sure to choose the appropriate download file for your relay's operating system (OS) and architecture. Since we're using Debian with an AMD processor, we're going to download the `linux-amd64` file.

At the time of writing the current version of Go is `1.23.2`, so we'll be downloading the `go1.23.2.linux-amd64.tar.gz` file.

We'll be downloading the file to the `/tmp` directory which is cleared when the system reboots. We’ll be downloading the file here because we won’t need this file after the installation process is completed.

Since we're using a headless server, we can use the `wget` command to download Go:

```bash
wget -P /tmp https://go.dev/dl/go1.23.2.linux-amd64.tar.gz
```

Be sure to replace `go1.23.2.linux-amd64.tar.gz` with whatever version of Go you're downloading and with whatever architecture you're using.

Before extracting the contents of the file, be sure to first remove any previously installed version of Go:

```bash
rm -rf /usr/local/go
```

You can now extract the contents of the file into the `/usr/local` directory:

```bash
tar -C /usr/local -xzf /tmp/go1.23.2.linux-amd64.tar.gz
```

Be sure to not extract the contents into an already existing `/usr/local/go` directory since this can break the Go installation.

After successfully extracting the contents, you should see a `go` directory in the `/usr/local` directory:

```bash
ls /usr/local
```

You can now add `/usr/local/go/bin` to the `PATH` environment variable by adding `export PATH=$PATH:/usr/local/go/bin` to your `$HOME/.bashrc` file.

First open the file with a text editor of your choice, e.g., `nano`:

```bash
nano $HOME/.bashrc
```

Then add the following to the end of the file:

```bash
export PATH=$PATH:/usr/local/go/bin
```

Save the changes made to the `.bashrc` file and exit the editor.

The changes may not take effect until after you log back into the server.

To apply the changes immediately run:

```bash
source $HOME/.bashrc
```

Verify the installation was successful by running:

```bash
go version
```

The command should output:

```bash
go version go1.23.2 linux/amd64
```

## Clone the Repository

We'll be downloading the repository to the `/tmp` directory since it's cleared when the system reboots, and we don't need the files associated with the relay29 repository after the installation process is completed.

Navigate to the `/tmp` directory:

```bash
cd /tmp
```

If you want to keep the relay29 repository on your relay, then you can download the repository in a persistent directory, e.g., your `$HOME` directory.

We're now ready to clone the relay29 repository:

```bash
git clone https://github.com/fiatjaf/relay29.git
```

After cloning the repository, navigate to the `relay29/examples/groups.fiatjaf.com` directory:

```bash
cd relay29/examples/groups.fiatjaf.com
```

## Build Khatru29 Example

We're now ready to build the `groups.fiatjaf.com` example which uses the Khatru29 wrapper:

```bash
go build
```

The compilation could take a while depending on your server's specs, so be patient while the binary is being built.

When the compilation is finished the binary will be located in the `relay29/examples/groups.fiatjaf.com` directory.

You can list out the contents of that directory to see the binary:

```bash
ls
```

You should see the binary file which should be named `groups.fiatjaf.com`.

We're going to use a more general name for the binary by renaming it to `khatru29`:

```bash
mv groups.fiatjaf.com khatru29
```

## Version

At the time of writing, the latest version of the relay29 library is `v0.4.0` which is what the rest of the guide is currently based on.

## Run Binary

To run Khatru29 you need to provide a secret key which will be used as the relay's private key.

Be sure to input your secret key securely, i.e., don't let anyone, any camera, etc. see your secret key, clear your clipboard if you're copying and pasting the value, turn off your terminal history on your server, etc.

The server terminal is using `bash`, so you can temporarily turn off the history for the current shell session by running:

```bash
set +o history
```

You can now run the binary:

```bash
DOMAIN="relay.relayrunner.xyz" RELAY_NAME="Relay Runner's Khatru29 Relay" RELAY_PRIVKEY="<your-secret-key>" ./khatru29
```

Be sure to replace `<your-secret-key>` with your secret key. The secret key can be passed as a 32-byte hex. Also, be sure to replace `relay.relayrunner.xyz` with the domain you set up with your relay.

Khatru29 should now be listening on `0.0.0.0:5577`. A relay database will also be created in your current directory called `db`.

To stop the relay press `Ctrl+C`.

If you were able to successfully run the binary, you can turn on the history for the current shell session by running:

```bash
set -o history
```
70 changes: 70 additions & 0 deletions docs/relay/khatru29/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Configuration

We're now going to discuss how to configure the Khatru29 example relay environment file which allows us to set various relay settings. We're also going to discuss how to create the data directory which will be owned by the `nostr` user.

## Environment File

We're going to create the Khatru29 example relay environment file in the `/etc/systemd/system` directory:

```bash
touch /etc/systemd/system/khatru29.env
```

## Data Directory

We're going to create a data directory for the Khatru29 example relay in the `/var/lib` directory:

```bash
mkdir -p /var/lib/khatru29
```

We can change the permissions of the data directory and all of its content, i.e., the files and subdirectories inside of it:

```bash
chmod 0755 -R /var/lib/khatru29
```

We can then change the ownership of the data directory to use the `nostr` user:

```bash
chown -R nostr:nostr /var/lib/khatru29
```

## Edit Environment File

To change the relay settings you can open the `khatru29.env` file:

```bash
nano /etc/systemd/system/khatru29.env
```

We're now going to add the following lines to the environment file:

```ini
DOMAIN="relay.relayrunner.xyz"
PORT="5577"
DATABASE_PATH="/var/lib/khatru29/db"
RELAY_NAME="Relay Runner's Khatru29 Relay"
RELAY_PRIVKEY="<your-secret-key>"
RELAY_DESCRIPTION="Khatru29 Nostr Relay"
RELAY_CONTACT="[email protected]"
RELAY_ICON="https://example.com/your-relay-icon.png"
```

Here's a description of the relay settings:

- `DOMAIN` - The domain name of your server, e.g., `relay.relayrunner.xyz`.

- `PORT` - The port your relay will run on. This setting is optional and the default value is `5577`.

- `DATABASE_PATH` - The directory where your relay will store data. This setting is optional and the default value is set to `./db`. We'll set this to `/var/lib/khatru29/db`.

- `RELAY_NAME` - The name of your relay, e.g., `Relay Runner's Khatru29 Relay`.

- `RELAY_PRIVKEY` - Your Nostr private key (32-byte hex, not nsec) that you're using as the relay's private key. Be sure to input your secret key securely, i.e., don't let anyone, any camera, etc. see your secret key, clear your clipboard if you're copying and pasting the value, etc. Be sure to replace `<your-secret-key>` with your secret key.

- `RELAY_DESCRIPTION` - A description of your relay. This setting is optional and the default value is set to `""`. We'll set this to `Khatru29 Nostr Relay`.

- `RELAY_CONTACT` - Your email address used for administrative requests, e.g., `[email protected]`. This setting is optional and the default value is `""`.

- `RELAY_ICON` - URL to your relay's icon, e.g., `https://example.com/your-relay-icon.png`. This setting is optional and the default value is `""`.
35 changes: 35 additions & 0 deletions docs/relay/khatru29/install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Install

After building the binary, we're now going to "install" it by using the `install` command.

We’re actually not installing the binary though since the `install` command isn’t used to install software packages despite its name.

The `install` command is a way to copy files to a target location similar to the copy command, i.e., `cp`, but it gives us more control by allowing us to use advanced features when copying the files.

Some of these advanced features include the abilities to adjust permission modes like when using the `chmod` command, adjust ownership permissions like when using the `chown` command, to make backups of the files, and to preserve the metadata of the files, e.g., the access and modifications times of the files.

We're going to be "installing" the `khatru29` binary to the `/usr/local/bin` directory which allows us to run the binary from any directory. This makes running the binary easier since we don't have to navigate to or specify the path to the `khatru29` binary every time we want to run it.

First, navigate to the directory where you installed the binary, e.g.,

```bash
cd /tmp/relay29/examples/groups.fiatjaf.com
```

To "install" the binary we can run:

```bash
install -v -m 0755 -o root -g root -t /usr/local/bin khatru29
```

Here’s an explanation of the options we passed to the `install` command:

`-v`: This option specifies that we would like to see the verbose output which means we’ll be displayed with the details of the process.

`-m`: This option specifies the file permissions in octal notation for the files we’re copying, i.e., `0755`.

`-o`: This option allows us to set the owner for the files we’re copying, i.e., `root`.

`-g`: This option allows us to set the group for the files we’re copying, i.e., `root`.

`-t`: This option specifies the target directory that we want to copy the specified files into, i.e., `/usr/local/bin`.
7 changes: 7 additions & 0 deletions docs/relay/khatru29/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Intro

The repository [relay29](https://github.com/fiatjaf/relay29 "relay29") is a library for creating [NIP-29](https://github.com/nostr-protocol/nips/blob/master/29.md "NIP-29") relay-based groups. Since NIP-29 requires relays to have more of an active role in making the groups work with the rules, the relay29 library can be used to handle these requirements.

The relay29 library currently supports [Khatru](https://github.com/fiatjaf/khatru "Khatru") using the [Khatru29](https://pkg.go.dev/github.com/fiatjaf/relay29/khatru29 "Khatru29") wrapper, [strfry](https://github.com/hoytech/strfry "strfry") using the [strfry29](https://github.com/fiatjaf/relay29/tree/master/strfry29 "strfry29") plugin, and [relayer](https://github.com/fiatjaf/relayer "relayer") using [relayer29](https://github.com/fiatjaf/relay29/blob/master/relayer29 "relayer29").

In this guide we'll be demonstrating how to use the Khatru29 wrapper to set up a Khatru relay with support for relay-based groups. We'll specifically be implementing the `groups.fiatjaf.com` example provided by the relay29 repository. The steps provided here should work for other Khatru29 examples as well.
83 changes: 83 additions & 0 deletions docs/relay/khatru29/service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Service

A service is a long-running process that can be started and stopped manually as well as set to automatically start when the system boots. It can be used to run background tasks, handle network requests, and interact with the system.

## Create Unit File

We're going to create a [systemd](https://systemd.io "systemd") service for the Khatru29 example relay which will allow us to automatically start the relay on boot.

To do this we're going to create and open the following systemd unit file:

```bash
nano /etc/systemd/system/khatru29.service
```

## Edit Unit File

We're now going to add the following lines to the `khatru29.service` unit file:

```ini
[Unit]
Description=Khatru29 Service
After=network.target

[Service]
Type=simple
User=nostr
Group=nostr
WorkingDirectory=/home/nostr
EnvironmentFile=/etc/systemd/system/khatru29.env
ExecStart=/usr/local/bin/khatru29
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

This service tells systemd to start the Khatru29 example relay as the `nostr` user after the network has been properly configured using our specified environment file and to attempt to restart the service if it fails.

## Reload systemd

To apply the newly created Khatru29 example relay service we're going to reload systemd:

```bash
systemctl daemon-reload
```

## Enable Service

We can enable the Khatru29 example relay service to automatically start on boot:

```bash
systemctl enable khatru29.service
```

## Start Service

To start the Khatru29 example relay service:

```bash
systemctl start khatru29.service
```

## Check Status

The relay service should now be running and set to automatically start on boot.

You can check the status of the service using:

```bash
systemctl status khatru29.service
```

If the service is running, you should see the following in the output:

```bash
Active: active (running)
```

If the service is enabled to automatically start on boot, you should see the following in the output:

```bash
Loaded: loaded (/etc/systemd/system/khatru29.service; enabled; preset: enabled)
```
3 changes: 3 additions & 0 deletions docs/relay/khatru29/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test your Relay

🚧 Under Construction 🚧
Loading

0 comments on commit 92b4d4d

Please sign in to comment.