Add CACHE_RELAY option to docker container

This commit is contained in:
hzrd149 2023-12-11 20:37:06 -06:00
parent c098feacd5
commit 1f77a48494
12 changed files with 161 additions and 27 deletions

View File

@ -0,0 +1,5 @@
---
"nostrudel": minor
---
Add CACHE_RELAY option to docker container

8
.dockerignore Normal file
View File

@ -0,0 +1,8 @@
node_modules
dist
.github
.vscode
.changeset
scripts
screenshots
relay

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
dist
node_modules
stats.html
relay

View File

@ -2,3 +2,4 @@ node_modules
dist
public/lib
stats.html
relay

18
docker-compose.yaml Normal file
View File

@ -0,0 +1,18 @@
version: "3.7"
volumes:
data: {}
services:
relay:
image: scsibug/nostr-rs-relay:0.8.13
volumes:
- data:/data
app:
build: .
depends_on:
- relay
environment:
CACHE_RELAY: relay:8080
ports:
- 8080:80

79
docker-entrypoint.sh Executable file
View File

@ -0,0 +1,79 @@
#!/bin/sh
set -e
CACHE_RELAY_PROXY=""
if [ -z ${CACHE_RELAY+x} ]; then
echo "No cache relay set"
else
echo "Cache relay set to $CACHE_RELAY"
sed -i 's/CACHE_RELAY_ENABLED = false/CACHE_RELAY_ENABLED = true/g' /usr/share/nginx/html/index.html
CACHE_RELAY_PROXY="
location /cache-relay {
proxy_pass http://$CACHE_RELAY/;
}
"
fi
CONF_FILE="/etc/nginx/conf.d/default.conf"
NGINX_CONF="
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
$CACHE_RELAY_PROXY
# Gzip settings
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/svg+xml
text/css
text/javascript
text/plain
text/xml;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
"
echo "$NGINX_CONF" > $CONF_FILE
_term() {
echo "Caught SIGTERM signal!"
kill -SIGTERM "$nginx_process" 2>/dev/null
}
nginx -g 'daemon off;' &
nginx_process=$!
trap _term SIGTERM
wait $nginx_process

View File

@ -11,3 +11,9 @@ RUN yarn install && yarn build
FROM nginx:stable-alpine-slim
EXPOSE 80
COPY --from=builder /app/dist /usr/share/nginx/html
ENV CACHE_RELAY=""
ADD ./docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod a+x /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT "/usr/local/bin/docker-entrypoint.sh"

View File

@ -20,6 +20,10 @@
content="https://repository-images.githubusercontent.com/581644549/d5eec580-ba3d-41e8-87db-58c313bf3f45"
/>
<script>
window.CACHE_RELAY_ENABLED = false;
</script>
%VITE_ANALYTICS_SCRIPT%
</head>
<body>

View File

@ -114,7 +114,7 @@ const log = logger.extend("DrawerRouter");
export function useRouterMarker(router: Router) {
const index = useRef<number | null>(null);
const set = useCallback((v=0) => (index.current = v), []);
const set = useCallback((v = 0) => (index.current = v), []);
const reset = useCallback(() => (index.current = null), []);
useEffect(() => {

View File

@ -3,8 +3,10 @@ import { NostrEvent } from "../types/nostr-event";
import relayPoolService from "./relay-pool";
import _throttle from "lodash.throttle";
const enabled = !!localStorage.getItem("enable-cache-relay");
export const LOCAL_CACHE_RELAY = "ws://localhost:7000";
const enabled = !!window.CACHE_RELAY_ENABLED;
const url = new URL("/cache-relay", location.href);
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
export const LOCAL_CACHE_RELAY = url.toString();
const wroteEvents = new Set<string>();
const writeQueue: NostrEvent[] = [];

3
src/types/window.d.ts vendored Normal file
View File

@ -0,0 +1,3 @@
interface Window {
CACHE_RELAY_ENABLED: boolean;
}

View File

@ -115,16 +115,12 @@ export default function PerformanceSettings() {
<FormLabel htmlFor="localCacheRelay" mb="0">
Local Cache Relay
</FormLabel>
<Switch
id="localCacheRelay"
isChecked={localCacheRelay}
onChange={(e) => setLocalCacheRelay(e.target.checked)}
/>
<Switch id="localCacheRelay" isChecked={!!window.CACHE_RELAY_ENABLED} />
<Button onClick={cacheDetails.onOpen} variant="link" ml="4">
Details
</Button>
</Flex>
<FormHelperText>Enabled: Use a local relay as a caching service</FormHelperText>
<FormHelperText>Enabled: Use a local relay as a cache</FormHelperText>
<Modal isOpen={cacheDetails.isOpen} onClose={cacheDetails.onClose} size="4xl">
<ModalOverlay />
@ -133,31 +129,42 @@ export default function PerformanceSettings() {
<ModalCloseButton />
<ModalBody px="4" pb="4" pt="0">
<Text>
When this option is enabled noStrudel will mirror every event it sees to the relay. It will also try
to load as much data from the relay first before reaching out to other relays.
When this is enabled noStrudel will connect to the relay at ws://{"<app domain>"}/cache-relay and
use it to cache all events it finds.
</Text>
<Text>
For security reasons noStrudel will only use <Code>ws://localhost:7000</Code> as the cache relay.
For security reasons this can only be enabled when running the docker container and setting the
CACHE_RELAY env variable
</Text>
<Heading size="md" mt="2">
Linux setup instructions
Docker compose example
</Heading>
<Text>
You can run a local relay using{" "}
<Link href="https://www.docker.com/get-started/" isExternal>
docker
</Link>{" "}
and{" "}
<Link href="https://hub.docker.com/r/scsibug/nostr-rs-relay" isExternal>
nostr-rs-relay
</Link>
<Text mt="2">
1. Create a docker-compose.yml file with nostr-rs-relay and noStrudel and set{" "}
<Code>CACHE_RELAY: relay:8080</Code>
</Text>
<Text mt="2">1. Create a folder for the data</Text>
<Code>mkdir ~/.nostr-relay/data -p -m 777</Code>
<Text mt="2">2. Start the relay</Text>
<Code>
docker run --rm -it -p 7000:8080 -v ~/.nostr-relay/data:/usr/src/app/db scsibug/nostr-rs-relay
<Code whiteSpace="pre" w="full">
{`version: "3.7"
volumes:
data: {}
services:
relay:
image: scsibug/nostr-rs-relay:0.8.13
volumes:
- data:/data
app:
image: ghcr.io/hzrd149/nostrudel:latest
depends_on:
- relay
environment:
CACHE_RELAY: relay:8080
ports:
- 8080:80
`.trim()}
</Code>
<Text mt="2">2. Start docker compose</Text>
<Code>docker compose up</Code>
</ModalBody>
</ModalContent>
</Modal>