Skip to content

Commit

Permalink
feat: use wot-relay index file and static directory, update env file,…
Browse files Browse the repository at this point in the history
… and remove data directory if using new pubkey (#69)
  • Loading branch information
jchiarulli authored Nov 9, 2024
1 parent f2b3c4d commit 47b43bc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 100 deletions.
2 changes: 1 addition & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var installCmd = &cobra.Command{
}

// Step 8: Download and install the relay binary
wot_relay.InstallRelayBinary()
wot_relay.InstallRelayBinary(pubKey)

// Step 9: Set up the relay service
wot_relay.SetupRelayService(relayDomain, pubKey, relayContact, httpsEnabled)
Expand Down
52 changes: 16 additions & 36 deletions pkg/relays/wot_relay/constants.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,39 @@
package wot_relay

const GitRepoBranch = "v0.1.12"
const GitRepoURL = "https://github.com/bitvora/wot-relay.git"
const GitRepoTmpDirPath = "/tmp/wot-relay"
const DownloadURL = "https://github.com/nodetec/relays/releases/download/v0.4.0/wot-relay-0.1.12-x86_64-linux-gnu.tar.gz"
const BinaryName = "wot-relay"
const BinaryFilePath = "/usr/local/bin/wot-relay"
const NginxConfigFilePath = "/etc/nginx/conf.d/wot_relay.conf"
const DataDirPath = "/var/lib/wot-relay"
const ConfigDirPath = "/etc/wot-relay"
const TemplatesDirPath = "/etc/wot-relay/templates"
const IndexFilePath = "/etc/wot-relay/templates/index.html"
const IndexFileTemplate = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WoT Relay</title>
<meta name="description" content="WoT Relay" />
<link href="{{.HTTPScheme}}://{{.Domain}}" rel="canonical" />
</head>
<body>
<main>
<div>
<div>
<span>WoT Relay</span>
</div>
<div>
<span>Domain: {{.Domain}}</span>
</div>
<div>
<span>Pubkey: {{.PubKey}}</span>
</div>
</div>
</main>
</body>
</html>
`
const TmpIndexFilePath = "/tmp/wot-relay/templates/index.html"
const StaticDirPath = "/etc/wot-relay/templates/static"
const DataDirPath = "/var/lib/wot-relay"
const TmpStaticDirPath = "/tmp/wot-relay/templates/static"
const ServiceName = "wot-relay"
const EnvFilePath = "/etc/systemd/system/wot-relay.env"
const EnvFileTemplate = `RELAY_NAME="WoT Relay"
RELAY_DESCRIPTION="WoT Nostr Relay"
RELAY_ICON="https://pfp.nostr.build/56306a93a88d4c657d8a3dfa57b55a4ed65b709eee927b5dafaab4d5330db21f.png"
RELAY_URL="{{.WSScheme}}://{{.Domain}}"
RELAY_PUBKEY="{{.PubKey}}"
RELAY_DESCRIPTION="Stores only notes in your WoT"
RELAY_URL="{{.WSScheme}}://{{.Domain}}"
RELAY_ICON="https://pfp.nostr.build/56306a93a88d4c657d8a3dfa57b55a4ed65b709eee927b5dafaab4d5330db21f.png"
RELAY_CONTACT="{{.RelayContact}}"
DB_PATH="/var/lib/wot-relay/db"
INDEX_PATH="/etc/wot-relay/templates/index.html"
STATIC_PATH="/etc/wot-relay/templates/static"
DB_PATH="/var/lib/wot-relay/db"
REFRESH_INTERVAL_HOURS=24
MINIMUM_FOLLOWERS=3
REFRESH_INTERVAL_HOURS=3
MINIMUM_FOLLOWERS=1
ARCHIVAL_SYNC="FALSE"
ARCHIVE_REACTIONS="FALSE"
MAX_AGE_DAYS=0
`
const ServiceFilePath = "/etc/systemd/system/wot-relay.service"
const ServiceFileTemplate = `[Unit]
Description=WoT Nostr Relay Service
Description=WoT Relay Service
After=network.target
[Service]
Expand All @@ -63,8 +44,7 @@ WorkingDirectory=/home/nostr
EnvironmentFile={{.EnvFilePath}}
ExecStart={{.BinaryFilePath}}
Restart=on-failure
MemoryHigh=512M
MemoryMax=1G
MemoryMax=2G
[Install]
WantedBy=multi-user.target
Expand Down
25 changes: 24 additions & 1 deletion pkg/relays/wot_relay/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package wot_relay
import (
"fmt"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/git"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/pterm/pterm"
"path/filepath"
)

// Function to download and make the binary executable
func InstallRelayBinary() {
func InstallRelayBinary(pubKey string) {
spinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s...", RelayName))

// Check for and remove existing git repository
directories.RemoveDirectory(GitRepoTmpDirPath)

// Download git repository
git.Clone(GitRepoBranch, GitRepoURL, GitRepoTmpDirPath)

// Determine the file name from the URL
tmpFileName := filepath.Base(DownloadURL)

Expand All @@ -35,6 +43,21 @@ func InstallRelayBinary() {
spinner.UpdateText("Service file not found...")
}

// Check if environment file exists
if files.FileExists(EnvFilePath) {
// Check if the pubKey exists in the environment file
spinner.UpdateText(fmt.Sprintf("Checking for public key in the %s file...", EnvFilePath))
lineExists := files.LineExists(fmt.Sprintf(`RELAY_PUBKEY="%s"`, pubKey), EnvFilePath)

// If false remove data directory
if !lineExists {
spinner.UpdateText("Public key not found, removing data directory...")
directories.RemoveDirectory(DataDirPath)
} else {
spinner.UpdateText("Public key found, keeping data directory.")
}
}

// Extract binary
files.ExtractFile(tmpFilePath, relays.BinaryDestDir)

Expand Down
51 changes: 30 additions & 21 deletions pkg/relays/wot_relay/service.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
package wot_relay

import (
"fmt"
"github.com/nodetec/rwz/pkg/relays"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/nodetec/rwz/pkg/utils/templates"
"github.com/pterm/pterm"
)

// Function to set up the relay service
func SetupRelayService(domain, pubKey, relayContact string, httpsEnabled bool) {
spinner, _ := pterm.DefaultSpinner.Start("Configuring relay service...")

// Ensure the templates directory exists and set ownership
spinner.UpdateText("Creating templates directory...")
directories.CreateDirectory(TemplatesDirPath, 0755)

// Use chown command to set ownership of the templates directory to the nostr user
directories.SetOwnerAndGroup(relays.User, relays.User, TemplatesDirPath)

// Ensure the static directory exists and set ownership
spinner.UpdateText("Creating static directory...")
directories.CreateDirectory(StaticDirPath, 0755)

// Use chown command to set ownership of the static directory to the nostr user
directories.SetOwnerAndGroup(relays.User, relays.User, StaticDirPath)

// Ensure the data directory exists and set ownership
spinner.UpdateText("Creating data directory...")
directories.CreateDirectory(DataDirPath, 0755)
directories.CreateDirectory(fmt.Sprintf("%s/db", DataDirPath), 0755)

// Use chown command to set ownership of the data directory to the nostr user
// Use chown command to set ownership of the data directory and its content to the nostr user
directories.SetOwnerAndGroup(relays.User, relays.User, DataDirPath)

// Ensure the config directory exists and set permissions
spinner.UpdateText("Creating config directory...")
directories.CreateDirectory(ConfigDirPath, 0755)

// Ensure the templates directory exists and set permissions
spinner.UpdateText("Creating templates directory...")
directories.CreateDirectory(TemplatesDirPath, 0755)

// Use chown command to set ownership of the config directory and its content to the nostr user
directories.SetOwnerAndGroup(relays.User, relays.User, ConfigDirPath)

// Check if the index.html file exists and remove it if it does
files.RemoveFile(IndexFilePath)

// Copy the index.html file to templates directory
files.CopyFile(TmpIndexFilePath, TemplatesDirPath)

// Use chown command to set ownership of the index.html file to the nostr user
files.SetOwnerAndGroup(relays.User, relays.User, IndexFilePath)

// Remove the static directory and all of its content if it exists
spinner.UpdateText("Removing static directory...")
directories.RemoveDirectory(StaticDirPath)

// Copy the static directory and all of its content to the templates directory
directories.CopyDirectory(TmpStaticDirPath, TemplatesDirPath)

// Use chown command to set ownership of the static directory and its content to the nostr user
directories.SetOwnerAndGroup(relays.User, relays.User, StaticDirPath)

// Check if the environment file exists and remove it if it does
files.RemoveFile(EnvFilePath)

// Check if the service file exists and remove it if it does
files.RemoveFile(ServiceFilePath)

// Create the index.html file
spinner.UpdateText("Creating index.html file...")
indexFileParams := templates.IndexFileParams{Domain: domain, HTTPSEnabled: httpsEnabled, PubKey: pubKey}
templates.CreateIndexFile(IndexFilePath, IndexFileTemplate, &indexFileParams)

// Create the environment file
spinner.UpdateText("Creating environment file...")
envFileParams := systemd.EnvFileParams{Domain: domain, HTTPSEnabled: httpsEnabled, PubKey: pubKey, RelayContact: relayContact}
Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/directories/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ func CreateDirectory(path string, permissions FileMode) {
}
}

// Function to copy a directory and all of its content
func CopyDirectory(dirToCopyPath, destDirPath string) {
err := exec.Command("cp", "-R", dirToCopyPath, destDirPath).Run()
if err != nil {
pterm.Println()
pterm.Error.Println(fmt.Sprintf("Failed to copy the %s directory to the %s directory: %v", dirToCopyPath, destDirPath, err))
os.Exit(1)
}
}

// Function to set owner and group of a directory
func SetOwnerAndGroup(owner, group, dir string) {
err := exec.Command("chown", "-R", fmt.Sprintf("%s:%s", owner, group), dir).Run()
Expand Down
41 changes: 0 additions & 41 deletions pkg/utils/templates/utils.go

This file was deleted.

0 comments on commit 47b43bc

Please sign in to comment.