Add web server and relay configuration support

- Introduced a new Dockerfile for building the web server.
- Updated docker-compose.yml to include the web server service with environment variable support for relay URLs.
- Enhanced relay management in relays.go to read from environment variables.
- Updated main.go to provide descriptions for relay configuration in CLI commands.
This commit is contained in:
studiokaiji
2025-11-26 00:24:29 +09:00
parent 535d36bab9
commit 6841ef94bb
5 changed files with 118 additions and 4 deletions

View File

@@ -9,3 +9,30 @@ services:
- ./nostr-rs-relay/config.toml:/usr/src/app/config.toml
user: root
container_name: nostr-webhost-relay
networks:
- nostr-network
web_server:
build:
context: ./hostr
dockerfile: Dockerfile
ports:
- "3000:3000"
container_name: nostr-webhost-server
environment:
- RELAY_URLS=ws://relay:8080
depends_on:
- relay
volumes:
- ./hostr/data:/app/data
- hostr_config:/app/.config
restart: unless-stopped
networks:
- nostr-network
command: ["./hostr", "start", "--port", "3000", "--mode", "normal"]
networks:
nostr-network:
driver: bridge
volumes:
hostr_config:

57
hostr/Dockerfile Normal file
View File

@@ -0,0 +1,57 @@
# Build stage
# Go 1.23 is the latest stable version available in Docker Hub
# The project requires Go 1.25 but we'll use 1.23 for now
FROM golang:1.23-alpine AS builder
# Accept GOTOOLCHAIN as build argument
ARG GOTOOLCHAIN=auto
ENV GOTOOLCHAIN=${GOTOOLCHAIN}
# Install necessary build dependencies
RUN apk add --no-cache git
# Set working directory
WORKDIR /app
# Copy go mod files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o hostr .
# Final stage
FROM alpine:latest
# Install ca-certificates for HTTPS support and netcat for connectivity checks
RUN apk --no-cache add ca-certificates netcat-openbsd
# Create a non-root user
RUN addgroup -g 1000 hostr && \
adduser -D -u 1000 -G hostr hostr
# Set working directory
WORKDIR /app
# Copy the binary from builder
COPY --from=builder /app/hostr .
# Copy entrypoint script
COPY docker-entrypoint.sh /app/docker-entrypoint.sh
# Create directories for data and config
RUN mkdir -p /app/data /home/hostr/.nostr-webhost && \
chmod +x /app/docker-entrypoint.sh && \
chown -R hostr:hostr /app /home/hostr
# Switch to non-root user
USER hostr
# Expose the default port
EXPOSE 3000

View File

@@ -56,6 +56,24 @@ func RemoveRelay(targetURL string) error {
}
func GetAllRelays() ([]string, error) {
// Check if relays are provided via environment variable
envRelays := os.Getenv("RELAY_URLS")
if envRelays != "" {
// Parse comma-separated relay URLs from environment variable
relayList := strings.Split(envRelays, ",")
cleanedRelays := []string{}
for _, relay := range relayList {
trimmed := strings.TrimSpace(relay)
if len(trimmed) > 0 {
cleanedRelays = append(cleanedRelays, trimmed)
}
}
if len(cleanedRelays) > 0 {
return cleanedRelays, nil
}
}
// Fall back to reading from file if environment variable is not set
dir, err := paths.GetSettingsDirectory()
if err != nil {
return nil, err
@@ -75,5 +93,6 @@ func GetAllRelays() ([]string, error) {
lines = append(lines, line)
}
}
return lines, nil
}

View File

@@ -2,6 +2,7 @@ package server
import (
"context"
"fmt"
"net/http"
"strings"
@@ -25,6 +26,8 @@ func Start(port string, mode string) {
r := gin.Default()
fmt.Println("[Hostr] Using relays:", strings.Join(allRelays, ", "))
r.GET("/e/:hex_or_nevent", func(ctx *gin.Context) {
hexOrNevent := ctx.Param("hex_or_nevent")
@@ -93,8 +96,6 @@ func Start(port string, mode string) {
} else {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
return
})
if mode != "secure" {
@@ -197,8 +198,6 @@ func Start(port string, mode string) {
} else {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
return
})
}

View File

@@ -23,6 +23,12 @@ func main() {
{
Name: "deploy",
Usage: "🌐 Deploy nostr website",
Description: `Deploy your website to Nostr relays.
Relay configuration:
- Set RELAY_URLS environment variable with comma-separated relay URLs to bypass .nostr_relays file
- Example: RELAY_URLS="wss://relay1.com,wss://relay2.com" hostr deploy
- If RELAY_URLS is not set, will read from .nostr_relays file`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
@@ -160,6 +166,12 @@ func main() {
{
Name: "start",
Usage: "🕺 Wake up web server",
Description: `Start the web server to serve content from Nostr relays.
Relay configuration:
- Set RELAY_URLS environment variable with comma-separated relay URLs to bypass .nostr_relays file
- Example: RELAY_URLS="wss://relay1.com,wss://relay2.com" hostr start
- If RELAY_URLS is not set, will read from .nostr_relays file`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",