chore: Added env configurations vars

This commit is contained in:
florian 2024-03-18 20:28:32 +01:00
parent 42808a9c77
commit d0991e3784
6 changed files with 44 additions and 28 deletions

View File

@ -2,3 +2,4 @@
node_modules node_modules
.github .github
build build
cache

2
.gitignore vendored
View File

@ -175,3 +175,5 @@ dist
.DS_Store .DS_Store
build build
cache

View File

@ -1,5 +1,6 @@
{ {
"printWidth": 120, "printWidth": 120,
"useTabs": false, "useTabs": false,
"tabWidth": 2 "tabWidth": 2,
"singleQuote": true
} }

View File

@ -3,25 +3,17 @@ import debug from 'debug';
import NodeCache from 'node-cache'; import NodeCache from 'node-cache';
import { Drive } from './types.js'; import { Drive } from './types.js';
import uniqBy from 'lodash/uniqBy.js'; import uniqBy from 'lodash/uniqBy.js';
import { SINGLE_SITE } from './env.js'; import { DRIVE_CACHE_TTL, DRIVE_REFRESH_TIME, NOSTR_DEFAULT_RELAYS, SINGLE_SITE } from './env.js';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { AddressPointer } from 'nostr-tools/nip19'; import { AddressPointer } from 'nostr-tools/nip19';
const driveCache = new NodeCache({ stdTTL: 60 * 60 }); // 5min for development const driveCache = new NodeCache({ stdTTL: DRIVE_CACHE_TTL });
const log = debug('web:drive:nostr'); const log = debug('web:drive:nostr');
export const defaultRelays = [
'wss://nostrue.com',
'wss://relay.damus.io',
//"wss://nostr.wine",
'wss://nos.lol',
// "wss://nostr-pub.wellorder.net",
];
// TODO use relays from naddr // TODO use relays from naddr
const ndk = new NDK({ const ndk = new NDK({
explicitRelayUrls: defaultRelays, explicitRelayUrls: NOSTR_DEFAULT_RELAYS,
}); });
ndk.connect(); ndk.connect();
@ -100,7 +92,7 @@ export const readDrive = async (
const event = await ndk.fetchEvent( const event = await ndk.fetchEvent(
filter, filter,
{}, {},
NDKRelaySet.fromRelayUrls(relays?.concat(defaultRelays) || defaultRelays, ndk), NDKRelaySet.fromRelayUrls(relays?.concat(NOSTR_DEFAULT_RELAYS) || NOSTR_DEFAULT_RELAYS, ndk),
); );
log('fetch finsihed'); log('fetch finsihed');
@ -124,5 +116,5 @@ if (SINGLE_SITE) {
} else { } else {
// Load all drives into the cache // Load all drives into the cache
fetchAllDrives(); fetchAllDrives();
setInterval(() => fetchAllDrives(), 60000); setInterval(() => fetchAllDrives(), DRIVE_REFRESH_TIME);
} }

View File

@ -1,2 +1,14 @@
export const SINGLE_SITE = process.env.SINGLE_SITE;
export const PORT = process.env.PORT || 3010; export const PORT = process.env.PORT || 3010;
export const SINGLE_SITE = process.env.SINGLE_SITE;
export const FOLDER_LISTING = (process.env.FOLDER_LISTING || 'true') === 'true';
export const DRIVE_PRELOAD = (process.env.DRIVE_PRELOAD || 'false') === 'true';
export const DRIVE_REFRESH_TIME = parseInt(process.env.DRIVE_REFRESH_TIME || '300', 10) * 1000; // 5min
export const DRIVE_CACHE_TTL = parseInt(process.env.DRIVE_CACHE_TTL || '3600', 10); // 1h
export const CDN_CACHE_DIR = process.env.CDN_CACHE_DIR || './cache';
export const CDN_MAX_LOCAL_CACHE_SIZE = parseInt(process.env.CDN_MAX_LOCAL_CACHE_SIZE || '', 100000); // 100KB
export const CDN_ADDITIONAL_SERVERS = (process.env.CDN_ADDITIONAL_SERVERS || 'https://cdn.hzrd149.com,https://cdn.satellite.earth').split(',');
export const NOSTR_DEFAULT_RELAYS = (process.env.NOSTR_DEFAULT_RELAYS || 'wss://nostrue.com,wss://relay.damus.io,wss://nos.lol').split(',');

View File

@ -5,12 +5,19 @@ import Router from '@koa/router';
import { nip19 } from 'nostr-tools'; import { nip19 } from 'nostr-tools';
import { AddressPointer } from 'nostr-tools/nip19'; import { AddressPointer } from 'nostr-tools/nip19';
import { Drive, FileMeta } from './types.js'; import { Drive, FileMeta } from './types.js';
import { defaultRelays, readDrive } from './drive.js'; import { readDrive } from './drive.js';
import { searchCdn } from './cdn.js'; import { searchCdn } from './cdn.js';
import { PassThrough } from 'stream'; import { PassThrough } from 'stream';
import fs from 'node:fs'; import fs from 'node:fs';
import nodePath from 'path'; import nodePath from 'path';
import { PORT, SINGLE_SITE } from './env.js'; import {
CDN_ADDITIONAL_SERVERS,
CDN_CACHE_DIR,
CDN_MAX_LOCAL_CACHE_SIZE,
FOLDER_LISTING,
PORT,
SINGLE_SITE,
} from './env.js';
import { appendSlashIfMissing, prefixSlashIfMissing, removeLeadingSlashes } from './utils.js'; import { appendSlashIfMissing, prefixSlashIfMissing, removeLeadingSlashes } from './utils.js';
const log = debug('web'); const log = debug('web');
@ -18,7 +25,6 @@ const app = new Koa();
const router = new Router(); const router = new Router();
// const cacheAdapter = new NDKCacheAdapterDexie({ dbName: "ndk-cache" }); // const cacheAdapter = new NDKCacheAdapterDexie({ dbName: "ndk-cache" });
const additionalServers = ['https://cdn.hzrd149.com', 'https://cdn.satellite.earth'];
const findFile = (drive: Drive, filePathToSearch: string): FileMeta | undefined => { const findFile = (drive: Drive, filePathToSearch: string): FileMeta | undefined => {
const searchPath = prefixSlashIfMissing(filePathToSearch); const searchPath = prefixSlashIfMissing(filePathToSearch);
@ -30,9 +36,8 @@ const findFolderContents = (drive: Drive, filePathToSearch: string): FileMeta[]
return drive.files.filter((f) => f.path.startsWith(searchPath)); return drive.files.filter((f) => f.path.startsWith(searchPath));
}; };
const cacheDir = './cache'; if (!fs.existsSync(CDN_CACHE_DIR)) {
if (!fs.existsSync(cacheDir)) { fs.mkdirSync(CDN_CACHE_DIR, { recursive: true });
fs.mkdirSync(cacheDir, { recursive: true });
} }
// handle errors // handle errors
@ -85,17 +90,17 @@ const serveStream = (ctx: Koa.ParameterizedContext, mimeType: string, src: NodeJ
const serveDriveFile = async (ctx: Koa.ParameterizedContext, drive: Drive, fileMeta: FileMeta) => { const serveDriveFile = async (ctx: Koa.ParameterizedContext, drive: Drive, fileMeta: FileMeta) => {
const { hash, mimeType, size } = fileMeta; const { hash, mimeType, size } = fileMeta;
const cacheFile = nodePath.join(cacheDir, hash); const cacheFile = nodePath.join(CDN_CACHE_DIR, hash);
if (fs.existsSync(cacheFile)) { if (fs.existsSync(cacheFile)) {
log(`returning cached data for ${hash}`); log(`returning cached data for ${hash}`);
const src = fs.createReadStream(cacheFile); const src = fs.createReadStream(cacheFile);
serveStream(ctx, mimeType, src); serveStream(ctx, mimeType, src);
} else { } else {
// lookup media sevrers for user -> ndk (optional) // lookup media sevrers for user -> ndk (optional)
const cdnSource = await searchCdn([...drive.servers, ...additionalServers], hash); const cdnSource = await searchCdn([...drive.servers, ...CDN_ADDITIONAL_SERVERS], hash);
if (cdnSource) { if (cdnSource) {
if (size < 100000) { if (size < CDN_MAX_LOCAL_CACHE_SIZE) {
// if small file < 100KB, download and serve downloaded file // if small file < 100KB, download and serve downloaded file
await storeInCache(cdnSource, cacheFile); await storeInCache(cdnSource, cacheFile);
serveStream(ctx, mimeType, fs.createReadStream(cacheFile)); serveStream(ctx, mimeType, fs.createReadStream(cacheFile));
@ -167,11 +172,14 @@ router.get('(.*)', async (ctx, next) => {
const folder = findFolderContents(drive, searchPath); const folder = findFolderContents(drive, searchPath);
log('file not found in drive: ' + searchPath); log('file not found in drive: ' + searchPath);
ctx.status = 404; ctx.status = 404;
if (FOLDER_LISTING) {
ctx.set('Content-Type', 'text/html'); ctx.set('Content-Type', 'text/html');
const folderList = folder.map((f) => `<li><a href="${encodeURI(basePath + f.path)}">${f.path}</a></li>`); const folderList = folder.map((f) => `<li><a href="${encodeURI(basePath + f.path)}">${f.path}</a></li>`);
ctx.body = `<html><body><h2>Index of ${searchPath || '/'}</h2><ul>${folderList.join('')}</ul></body></html>`; ctx.body = `<html><body><h2>Index of ${searchPath || '/'}</h2><ul>${folderList.join('')}</ul></body></html>`;
} }
} }
}
}); });
app.use(router.routes()).use(router.allowedMethods()); app.use(router.routes()).use(router.allowedMethods());