From 6841ef94bbe9ac5abb303f543f58ea60908e382d Mon Sep 17 00:00:00 2001 From: studiokaiji Date: Wed, 26 Nov 2025 00:24:29 +0900 Subject: [PATCH] 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. --- docker-compose.yml | 27 ++++++++++++++++++ hostr/Dockerfile | 57 ++++++++++++++++++++++++++++++++++++++ hostr/cmd/relays/relays.go | 19 +++++++++++++ hostr/cmd/server/server.go | 7 ++--- hostr/main.go | 12 ++++++++ 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 hostr/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml index cc253ad..9eeceb6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/hostr/Dockerfile b/hostr/Dockerfile new file mode 100644 index 0000000..cc51788 --- /dev/null +++ b/hostr/Dockerfile @@ -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 + diff --git a/hostr/cmd/relays/relays.go b/hostr/cmd/relays/relays.go index 5c60b56..5618d21 100644 --- a/hostr/cmd/relays/relays.go +++ b/hostr/cmd/relays/relays.go @@ -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 } diff --git a/hostr/cmd/server/server.go b/hostr/cmd/server/server.go index 17be517..162c18e 100644 --- a/hostr/cmd/server/server.go +++ b/hostr/cmd/server/server.go @@ -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 }) } diff --git a/hostr/main.go b/hostr/main.go index ede75e7..eec96ea 100644 --- a/hostr/main.go +++ b/hostr/main.go @@ -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",