enh: remove top level proxy

This commit is contained in:
Ben Allfree 2023-10-03 00:11:40 -07:00
parent 94299d2db4
commit acbb13d146
7 changed files with 11 additions and 119 deletions

View File

@ -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

View File

@ -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"

View File

@ -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 = (() => {

View File

@ -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<typeof proxyService>
@ -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`)

View File

@ -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"
}
}

View File

@ -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`)

View File

@ -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"]
}