Skip to content

Commit 45a9e6a

Browse files
authored
chore: update file and directory paths to use constants (#59)
* chore: update file and directory paths to use constants * fix: add network constants file
1 parent d681275 commit 45a9e6a

21 files changed

+99
-89
lines changed

cmd/install.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var installCmd = &cobra.Command{
2525
pterm.Println()
2626

2727
// Supported relay options
28-
options := []string{"Khatru Pyramid", "strfry", "Khatru29", "strfry29", "WoT Relay"}
28+
options := []string{khatru_pyramid.RelayName, strfry.RelayName, khatru29.RelayName, strfry29.RelayName, wot_relay.RelayName}
2929

3030
// Use PTerm's interactive select feature to present the options to the user and capture their selection
3131
// The Show() method displays the options and waits for the user's input
@@ -36,23 +36,23 @@ var installCmd = &cobra.Command{
3636

3737
var privKey string
3838
var pubKey string
39-
if selectedRelayOption == "Khatru Pyramid" || selectedRelayOption == "WoT Relay" {
39+
if selectedRelayOption == khatru_pyramid.RelayName || selectedRelayOption == wot_relay.RelayName {
4040
pterm.Println()
4141
pubKey, _ = pterm.DefaultInteractiveTextInput.Show("Public key (hex not npub)")
42-
} else if selectedRelayOption == "Khatru29" || selectedRelayOption == "strfry29" {
42+
} else if selectedRelayOption == khatru29.RelayName || selectedRelayOption == strfry29.RelayName {
4343
pterm.Println()
4444
privKeyInput := pterm.DefaultInteractiveTextInput.WithMask("*")
4545
privKey, _ = privKeyInput.Show("Private key (hex not nsec)")
4646
}
4747

4848
var relayContact string
49-
if selectedRelayOption == "Khatru Pyramid" || selectedRelayOption == "Khatru29" {
49+
if selectedRelayOption == khatru_pyramid.RelayName || selectedRelayOption == khatru29.RelayName {
5050
pterm.Println()
5151
pterm.Println(pterm.Yellow("Leave email empty if you don't want to provide relay contact information."))
5252

5353
pterm.Println()
5454
relayContact, _ = pterm.DefaultInteractiveTextInput.Show("Email address")
55-
} else if selectedRelayOption == "WoT Relay" {
55+
} else if selectedRelayOption == wot_relay.RelayName {
5656
pterm.Println()
5757
pterm.Println(pterm.Yellow("If you leave the relay contact information empty, then the relay's public key will be used."))
5858

@@ -70,7 +70,7 @@ var installCmd = &cobra.Command{
7070
// Step 2: Configure the firewall
7171
network.ConfigureFirewall()
7272

73-
if selectedRelayOption == "Khatru Pyramid" {
73+
if selectedRelayOption == khatru_pyramid.RelayName {
7474
// Step 3: Configure Nginx for HTTP
7575
khatru_pyramid.ConfigureNginxHttp(relayDomain)
7676

@@ -89,7 +89,7 @@ var installCmd = &cobra.Command{
8989

9090
// Step 8: Show success messages
9191
khatru_pyramid.SuccessMessages(relayDomain, httpsEnabled)
92-
} else if selectedRelayOption == "strfry" {
92+
} else if selectedRelayOption == strfry.RelayName {
9393
// Step 3: Configure Nginx for HTTP
9494
strfry.ConfigureNginxHttp(relayDomain)
9595

@@ -108,7 +108,7 @@ var installCmd = &cobra.Command{
108108

109109
// Step 8: Show success messages
110110
strfry.SuccessMessages(relayDomain, httpsEnabled)
111-
} else if selectedRelayOption == "Khatru29" {
111+
} else if selectedRelayOption == khatru29.RelayName {
112112
// Step 3: Configure Nginx for HTTP
113113
khatru29.ConfigureNginxHttp(relayDomain)
114114

@@ -127,7 +127,7 @@ var installCmd = &cobra.Command{
127127

128128
// Step 8: Show success messages
129129
khatru29.SuccessMessages(relayDomain, httpsEnabled)
130-
} else if selectedRelayOption == "strfry29" {
130+
} else if selectedRelayOption == strfry29.RelayName {
131131
// Step 3: Configure Nginx for HTTP
132132
strfry29.ConfigureNginxHttp(relayDomain)
133133

@@ -146,7 +146,7 @@ var installCmd = &cobra.Command{
146146

147147
// Step 8: Show success messages
148148
strfry29.SuccessMessages(relayDomain, httpsEnabled)
149-
} else if selectedRelayOption == "WoT Relay" {
149+
} else if selectedRelayOption == wot_relay.RelayName {
150150
// Step 3: Configure Nginx for HTTP
151151
wot_relay.ConfigureNginxHttp(relayDomain)
152152

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var rootCmd = &cobra.Command{
1111
Use: "rwz",
1212
Short: "A wizard for Nostr relay runners",
1313
Long: `rwz is a CLI tool for Nostr relay operators that
14-
helps install and configure your specified relay.`,
14+
helps install and configure your relays.`,
1515
}
1616

1717
// Execute adds all child commands to the root command and sets flags appropriately.

pkg/network/certbot.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,24 @@ func GetCertificates(domainName string) bool {
4343

4444
spinner, _ := pterm.DefaultSpinner.Start("Checking SSL/TLS certificates...")
4545

46-
certificatePath := fmt.Sprintf("/etc/letsencrypt/live/%s", domainName)
47-
4846
// Check if certificates already exist
49-
if files.FileExists(fmt.Sprintf("%s/fullchain.pem", certificatePath)) &&
50-
files.FileExists(fmt.Sprintf("%s/privkey.pem", certificatePath)) &&
51-
files.FileExists(fmt.Sprintf("%s/chain.pem", certificatePath)) {
47+
if files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, FullchainFile)) &&
48+
files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, PrivkeyFile)) &&
49+
files.FileExists(fmt.Sprintf("%s/%s/%s", CertificateDirPath, domainName, ChainFile)) {
5250
spinner.Info("SSL/TLS certificates already exist.")
5351
return true
5452
}
5553

5654
spinner.UpdateText("Obtaining SSL/TLS certificates...")
5755
if email == "" {
58-
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--agree-tos", "--no-eff-email", "-q", "--register-unsafely-without-email")
56+
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("%s/%s", WWWDirPath, domainName), "-d", domainName, "--agree-tos", "--no-eff-email", "-q", "--register-unsafely-without-email")
5957
err := cmd.Run()
6058
if err != nil {
6159
pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err))
6260
os.Exit(1)
6361
}
6462
} else {
65-
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("/var/www/%s", domainName), "-d", domainName, "--email", email, "--agree-tos", "--no-eff-email", "-q")
63+
cmd := exec.Command("certbot", "certonly", "--webroot", "-w", fmt.Sprintf("%s/%s", WWWDirPath, domainName), "-d", domainName, "--email", email, "--agree-tos", "--no-eff-email", "-q")
6664
err := cmd.Run()
6765
if err != nil {
6866
pterm.Error.Println(fmt.Sprintf("Certbot failed to obtain the certificate for %s: %v", domainName, err))

pkg/network/constants.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package network
2+
3+
const CertificateDirPath = "/etc/letsencrypt/live"
4+
const FullchainFile = "fullchain.pem"
5+
const PrivkeyFile = "privkey.pem"
6+
const ChainFile = "chain.pem"
7+
const WWWDirPath = "/var/www"
8+
const AcmeChallengeDirPath = ".well-known/acme-challenge"

pkg/relays/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package relays
22

33
const BinaryDestDir = "/usr/local/bin"
4+
const TmpDirPath = "/tmp"

pkg/relays/khatru29/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func InstallRelayBinary() {
1616
tmpFileName := filepath.Base(DownloadURL)
1717

1818
// Temporary file path
19-
tmpFilePath := fmt.Sprintf("/tmp/%s", tmpFileName)
19+
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
2020

2121
// Check if the temporary file exists and remove it if it does
2222
files.RemoveFile(tmpFilePath)

pkg/relays/khatru29/nginx_http.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package khatru29
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/utils/directories"
67
"github.com/nodetec/rwz/pkg/utils/files"
78
"github.com/nodetec/rwz/pkg/utils/systemd"
@@ -14,7 +15,7 @@ func ConfigureNginxHttp(domainName string) {
1415

1516
files.RemoveFile(NginxConfigFilePath)
1617

17-
directories.CreateDirectory(fmt.Sprintf("/var/www/%s/.well-known/acme-challenge/", domainName), 0755)
18+
directories.CreateDirectory(fmt.Sprintf("%s/%s/%s/", network.WWWDirPath, domainName, network.AcmeChallengeDirPath), 0755)
1819

1920
configContent := fmt.Sprintf(`map $http_upgrade $connection_upgrade {
2021
default upgrade;
@@ -25,14 +26,13 @@ upstream websocket_khatru29 {
2526
server 0.0.0.0:5577;
2627
}
2728
28-
# %s
2929
server {
3030
listen 80;
3131
listen [::]:80;
3232
server_name %s;
3333
34-
location /.well-known/acme-challenge/ {
35-
root /var/www/%s;
34+
location /%s/ {
35+
root %s/%s;
3636
allow all;
3737
}
3838
@@ -79,7 +79,7 @@ server {
7979
return 301 http://%s$request_uri;
8080
}
8181
}
82-
`, domainName, domainName, domainName, domainName, domainName)
82+
`, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName, domainName)
8383

8484
files.WriteFile(NginxConfigFilePath, configContent, 0644)
8585

pkg/relays/khatru29/nginx_https.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package khatru29
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/utils/files"
67
"github.com/nodetec/rwz/pkg/utils/systemd"
78
"github.com/pterm/pterm"
@@ -27,7 +28,7 @@ server {
2728
listen [::]:443 ssl http2;
2829
server_name %s;
2930
30-
root /var/www/%s;
31+
root %s/%s;
3132
3233
location / {
3334
# First attempt to serve request as file, then
@@ -48,10 +49,10 @@ server {
4849
# Test configuration:
4950
# https://www.ssllabs.com/ssltest/analyze.html
5051
# https://cryptcheck.fr/
51-
ssl_certificate /etc/letsencrypt/live/%s/fullchain.pem;
52-
ssl_certificate_key /etc/letsencrypt/live/%s/privkey.pem;
52+
ssl_certificate %s/%s/%s;
53+
ssl_certificate_key %s/%s/%s;
5354
# Verify chain of trust of OCSP response using Root CA and Intermediate certs
54-
ssl_trusted_certificate /etc/letsencrypt/live/%s/chain.pem;
55+
ssl_trusted_certificate %s/%s/%s;
5556
5657
# TODO
5758
# Add support to generate the file in the script
@@ -109,16 +110,16 @@ server {
109110
listen [::]:80;
110111
server_name %s;
111112
112-
location /.well-known/acme-challenge/ {
113-
root /var/www/%s;
113+
location /%s/ {
114+
root %s/%s;
114115
allow all;
115116
}
116117
117118
location / {
118119
return 301 https://%s$request_uri;
119120
}
120121
}
121-
`, domainName, domainName, domainName, domainName, domainName, domainName, domainName, domainName)
122+
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
122123

123124
files.WriteFile(NginxConfigFilePath, configContent, 0644)
124125

pkg/relays/khatru_pyramid/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func InstallRelayBinary() {
1616
tmpFileName := filepath.Base(DownloadURL)
1717

1818
// Temporary file path
19-
tmpFilePath := fmt.Sprintf("/tmp/%s", tmpFileName)
19+
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
2020

2121
// Check if the temporary file exists and remove it if it does
2222
files.RemoveFile(tmpFilePath)

pkg/relays/khatru_pyramid/nginx_http.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package khatru_pyramid
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/utils/directories"
67
"github.com/nodetec/rwz/pkg/utils/files"
78
"github.com/nodetec/rwz/pkg/utils/systemd"
@@ -14,7 +15,7 @@ func ConfigureNginxHttp(domainName string) {
1415

1516
files.RemoveFile(NginxConfigFilePath)
1617

17-
directories.CreateDirectory(fmt.Sprintf("/var/www/%s/.well-known/acme-challenge/", domainName), 0755)
18+
directories.CreateDirectory(fmt.Sprintf("%s/%s/%s/", network.WWWDirPath, domainName, network.AcmeChallengeDirPath), 0755)
1819

1920
configContent := fmt.Sprintf(`map $http_upgrade $connection_upgrade {
2021
default upgrade;
@@ -25,14 +26,13 @@ upstream websocket_khatru_pyramid {
2526
server 0.0.0.0:3335;
2627
}
2728
28-
# %s
2929
server {
3030
listen 80;
3131
listen [::]:80;
3232
server_name %s;
3333
34-
location /.well-known/acme-challenge/ {
35-
root /var/www/%s;
34+
location /%s/ {
35+
root %s/%s;
3636
allow all;
3737
}
3838
@@ -79,7 +79,7 @@ server {
7979
return 301 http://%s$request_uri;
8080
}
8181
}
82-
`, domainName, domainName, domainName, domainName, domainName)
82+
`, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName, domainName)
8383

8484
files.WriteFile(NginxConfigFilePath, configContent, 0644)
8585

pkg/relays/khatru_pyramid/nginx_https.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package khatru_pyramid
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/utils/files"
67
"github.com/nodetec/rwz/pkg/utils/systemd"
78
"github.com/pterm/pterm"
@@ -27,7 +28,7 @@ server {
2728
listen [::]:443 ssl http2;
2829
server_name %s;
2930
30-
root /var/www/%s;
31+
root %s/%s;
3132
3233
location / {
3334
# First attempt to serve request as file, then
@@ -48,10 +49,10 @@ server {
4849
# Test configuration:
4950
# https://www.ssllabs.com/ssltest/analyze.html
5051
# https://cryptcheck.fr/
51-
ssl_certificate /etc/letsencrypt/live/%s/fullchain.pem;
52-
ssl_certificate_key /etc/letsencrypt/live/%s/privkey.pem;
52+
ssl_certificate %s/%s/%s;
53+
ssl_certificate_key %s/%s/%s;
5354
# Verify chain of trust of OCSP response using Root CA and Intermediate certs
54-
ssl_trusted_certificate /etc/letsencrypt/live/%s/chain.pem;
55+
ssl_trusted_certificate %s/%s/%s;
5556
5657
# TODO
5758
# Add support to generate the file in the script
@@ -109,16 +110,16 @@ server {
109110
listen [::]:80;
110111
server_name %s;
111112
112-
location /.well-known/acme-challenge/ {
113-
root /var/www/%s;
113+
location /%s/ {
114+
root %s/%s;
114115
allow all;
115116
}
116117
117118
location / {
118119
return 301 https://%s$request_uri;
119120
}
120121
}
121-
`, domainName, domainName, domainName, domainName, domainName, domainName, domainName, domainName)
122+
`, domainName, network.WWWDirPath, domainName, network.CertificateDirPath, domainName, network.FullchainFile, network.CertificateDirPath, domainName, network.PrivkeyFile, network.CertificateDirPath, domainName, network.ChainFile, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName)
122123

123124
files.WriteFile(NginxConfigFilePath, configContent, 0644)
124125

pkg/relays/strfry/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func InstallRelayBinary() {
2525
tmpFileName := filepath.Base(DownloadURL)
2626

2727
// Temporary file path
28-
tmpFilePath := fmt.Sprintf("/tmp/%s", tmpFileName)
28+
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
2929

3030
// Check if the temporary file exists and remove it if it does
3131
files.RemoveFile(tmpFilePath)

pkg/relays/strfry/nginx_http.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package strfry
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/network"
56
"github.com/nodetec/rwz/pkg/utils/directories"
67
"github.com/nodetec/rwz/pkg/utils/files"
78
"github.com/nodetec/rwz/pkg/utils/systemd"
@@ -14,16 +15,15 @@ func ConfigureNginxHttp(domainName string) {
1415

1516
files.RemoveFile(NginxConfigFilePath)
1617

17-
directories.CreateDirectory(fmt.Sprintf("/var/www/%s/.well-known/acme-challenge/", domainName), 0755)
18+
directories.CreateDirectory(fmt.Sprintf("%s/%s/%s/", network.WWWDirPath, domainName, network.AcmeChallengeDirPath), 0755)
1819

19-
configContent := fmt.Sprintf(`# %s
20-
server {
20+
configContent := fmt.Sprintf(`server {
2121
listen 80;
2222
listen [::]:80;
2323
server_name %s;
2424
25-
location /.well-known/acme-challenge/ {
26-
root /var/www/%s;
25+
location /%s/ {
26+
root %s/%s;
2727
allow all;
2828
}
2929
@@ -70,7 +70,7 @@ server {
7070
return 301 http://%s$request_uri;
7171
}
7272
}
73-
`, domainName, domainName, domainName, domainName, domainName)
73+
`, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName, domainName)
7474

7575
files.WriteFile(NginxConfigFilePath, configContent, 0644)
7676

0 commit comments

Comments
 (0)