diff --git a/.env-template b/.env-template index 61a684c1..4f52b85b 100644 --- a/.env-template +++ b/.env-template @@ -23,6 +23,9 @@ DAEMON_PB_HOOKS_DIR=`pwd`/packages/daemon/pb_hooks DAEMON_PB_USERNAME=admin@pockethost.test DAEMON_PB_PASSWORD=admin@pockethost.test +# The port the daemon listens on +DAEMON_PORT=80 + # The port the mothership listens on DAEMON_PB_PORT=8090 diff --git a/package.json b/package.json index 601a1e18..523100c8 100644 --- a/package.json +++ b/package.json @@ -12,15 +12,10 @@ "build:daemon": "cd packages/daemon && yarn build", "build:www": "cd packages/www && yarn build", "dev": "NODE_ENV=development concurrently 'yarn:dev:*'", - "dev:proxy": "cd packages/proxy && yarn dev", "dev:daemon": "cd packages/daemon && yarn dev", - "dev:www": "cd packages/www && yarn start", - "dev:dashboard": "cd packages/dashboard && yarn dev", "start": "concurrently 'yarn:start:*'", - "start:proxy": "cd packages/proxy && yarn start", "start:daemon": "cd packages/daemon && yarn start", "pm2": "concurrently 'yarn:pm2:*'", - "pm2:proxy": "cd packages/proxy && yarn pm2", "pm2:daemon": "cd packages/daemon && yarn pm2", "postinstall": "patch-package", "prepare": "husky install" diff --git a/packages/daemon/src/constants.ts b/packages/daemon/src/constants.ts index c0ab33a1..b161e84e 100644 --- a/packages/daemon/src/constants.ts +++ b/packages/daemon/src/constants.ts @@ -19,6 +19,7 @@ export const DAEMON_PB_PASSWORD = (() => { } return v })() +export const DAEMON_PORT = envi('DAEMON_PORT', 3000) export const DAEMON_PB_PORT_BASE = envi('DAEMON_PB_PORT_BASE', 8090) export const DAEMON_PB_IDLE_TTL = envi('DAEMON_PB_IDLE_TTL', 5000) export const DAEMON_PB_MIGRATIONS_DIR = (() => { diff --git a/packages/daemon/src/services/ProxyService.ts b/packages/daemon/src/services/ProxyService.ts index 37e43c66..fd9dc1ee 100644 --- a/packages/daemon/src/services/ProxyService.ts +++ b/packages/daemon/src/services/ProxyService.ts @@ -1,14 +1,14 @@ -import { PUBLIC_APP_DOMAIN } from '$constants' -import { Logger, mkSingleton, SingletonBaseConfig } from '@pockethost/common' +import { DAEMON_PORT, PUBLIC_APP_DOMAIN } from '$constants' +import { Logger, SingletonBaseConfig, mkSingleton } from '@pockethost/common' import { isFunction } from '@s-libs/micro-dash' import { - createServer, IncomingMessage, RequestListener, ServerResponse, + createServer, } from 'http' -import { default as httpProxy, default as Server } from 'http-proxy' -import { Asyncify, AsyncReturnType } from 'type-fest' +import { default as Server, default as httpProxy } from 'http-proxy' +import { AsyncReturnType, Asyncify } from 'type-fest' import UrlPattern from 'url-pattern' export type ProxyServiceApi = AsyncReturnType @@ -74,8 +74,8 @@ export const proxyService = mkSingleton(async (config: ProxyServiceConfig) => { } }) - info('daemon on port 3000') - server.listen(3000) + info(`daemon on port ${DAEMON_PORT}`) + server.listen(DAEMON_PORT) const shutdown = async () => { info(`Shutting down proxy server`) diff --git a/packages/proxy/package.json b/packages/proxy/package.json deleted file mode 100644 index b1f2c9e3..00000000 --- a/packages/proxy/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "@pockethost/proxy", - "version": "0.0.1", - "main": "index.js", - "license": "MIT", - "scripts": { - "dev": "tsx watch src/index.ts", - "start": "tsx src/index.ts", - "pm2": "pm2 del proxy ; pm2 start \"yarn start\" --name=proxy -o ~/logs/proxy.log -e ~/logs/proxy.log" - }, - "dependencies": { - "@types/node": "^18.11.17", - "http-proxy": "^1.18.1" - } -} \ No newline at end of file diff --git a/packages/proxy/src/index.ts b/packages/proxy/src/index.ts deleted file mode 100644 index a96936ac..00000000 --- a/packages/proxy/src/index.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { readFileSync } from 'fs' -import http from 'http' -import httpProxy from 'http-proxy' -import { createServer } from 'https' - -const options = { - key: readFileSync(process.env.SSL_KEY || ''), - cert: readFileSync(process.env.SSL_CERT || ''), -} - -const proxy = httpProxy.createProxyServer({}) -proxy.on('error', (e) => { - console.error(e) -}) - -const server = createServer(options, async (req, res) => { - const headers = { - 'Access-Control-Allow-Origin': '*' /* @dev First, read about security */, - 'Access-Control-Allow-Methods': 'OPTIONS, POST, GET, PATCH, DELETE, PUT', - 'Access-Control-Max-Age': 2592000, // 30 days - 'Access-Control-Allow-Headers': `authorization,content-type,cache-control`, - } - - if (req.method === 'OPTIONS') { - res.setHeader('Access-Control-Allow-Headers', '*') - res.writeHead(204, headers) - res.end() - return - } - res.setHeader('Access-Control-Allow-Origin', '*') - - const { host } = req.headers - - try { - if (host === process.env.PUBLIC_APP_DOMAIN) { - proxy.web(req, res, { target: `http://localhost:5173` }) - } else { - proxy.web(req, res, { target: `http://localhost:3000` }) - } - } catch (e) { - console.error(`Got an error ${e}`) - res.statusCode = 500 - res.end() - } -}) - -// -// Listen to the `upgrade` event and proxy the -// WebSocket requests as well. -// -server.on('upgrade', function (req, socket, head) { - console.log(req.headers.host) - const { host } = req.headers - - try { - if (host === process.env.PUBLIC_APP_DOMAIN) { - proxy.ws(req, socket, head, { target: `http://localhost:5173` }) - } else { - proxy.ws(req, socket, head, { target: `http://localhost:3000` }) - } - } catch (e) { - console.error(`Got an error ${e}`) - } -}) - -server.listen(443) - -const httpServer = http.createServer((req, res) => { - res.writeHead(301, { Location: `https://${req.headers.host}${req.url}` }) - res.end() -}) - -httpServer.listen(80) -console.log(`Listening 80->443`) diff --git a/packages/proxy/tsconfig.json b/packages/proxy/tsconfig.json deleted file mode 100644 index 860d586d..00000000 --- a/packages/proxy/tsconfig.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "compilerOptions": { - "allowJs": true, - "checkJs": true, - "esModuleInterop": true, - "allowSyntheticDefaultImports": true, - "forceConsistentCasingInFileNames": true, - "resolveJsonModule": true, - "skipLibCheck": true, - "strict": true, - "module": "ESNext", - "target": "ESNext", - "moduleResolution": "node", - "noUncheckedIndexedAccess": true, - "strictNullChecks": true - }, - "include": ["./src"] -}