From ddcb9e53d7fe4152ebe207dcef5fbd0968e1372c Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Wed, 21 Dec 2022 10:31:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BD=93=E4=BD=BF=E7=94=A8nginx?= =?UTF-8?q?=E6=97=B6=EF=BC=8Cnode=E8=BF=9B=E7=A8=8B=E7=9B=91=E5=90=ACunix?= =?UTF-8?q?=20sock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nginx/nginx-http.conf | 4 ++-- nginx/nginx.conf | 4 ++-- package-lock.json | 14 +++++++------- package.json | 2 +- src/bootstrap.ts | 6 +++++- src/cluster.ts | 16 +++++++++++----- 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/nginx/nginx-http.conf b/nginx/nginx-http.conf index 786f1bb..3f242f1 100644 --- a/nginx/nginx-http.conf +++ b/nginx/nginx-http.conf @@ -25,7 +25,7 @@ http { } location @be { - proxy_pass http://localhost:<%= port+1 %>; + proxy_pass http://unix:<%= root %>/cache/openbmclapi.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -44,7 +44,7 @@ http { } location / { - proxy_pass http://localhost:<%= port+1 %>; + proxy_pass http://unix:<%= root %>/cache/openbmclapi.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 1ca5aa5..24d9b28 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -29,7 +29,7 @@ http { } location @be { - proxy_pass http://localhost:<%= port+1 %>; + proxy_pass http://unix:<%= root %>/cache/openbmclapi.sock; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -48,7 +48,7 @@ http { } location / { - proxy_pass http://localhost:<%= port+1 %>; + proxy_pass http://unix:<%= root %>/cache/openbmclapi.sock;; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; diff --git a/package-lock.json b/package-lock.json index d44a46d..05942fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,7 @@ "@types/lodash": "^4.14.182", "@types/morgan": "^1.7.36", "@types/ms": "^0.7.30", - "@types/node": "^12.6.9", + "@types/node": "^18.11.17", "@types/progress": "^2.0.5", "@typescript-eslint/eslint-plugin": "^4.28.5", "@typescript-eslint/parser": "^4.28.5", @@ -424,9 +424,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "12.20.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.18.tgz", - "integrity": "sha512-YoTiIwdKxM3VLiY2sM05x4iGuTveYiCcDaUVmo1L5ndrXxPGW/NEoZu+pGcBirziomizcZsnsQoemikKcB2fRA==" + "version": "18.11.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz", + "integrity": "sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==" }, "node_modules/@types/progress": { "version": "2.0.5", @@ -4846,9 +4846,9 @@ "dev": true }, "@types/node": { - "version": "12.20.18", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.20.18.tgz", - "integrity": "sha512-YoTiIwdKxM3VLiY2sM05x4iGuTveYiCcDaUVmo1L5ndrXxPGW/NEoZu+pGcBirziomizcZsnsQoemikKcB2fRA==" + "version": "18.11.17", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.17.tgz", + "integrity": "sha512-HJSUJmni4BeDHhfzn6nF0sVmd1SMezP7/4F0Lq+aXzmp2xm9O7WXrUtHW/CHlYVtZUbByEvWidHqRtcJXGF2Ng==" }, "@types/progress": { "version": "2.0.5", diff --git a/package.json b/package.json index bfe1f98..f7e6120 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "@types/lodash": "^4.14.182", "@types/morgan": "^1.7.36", "@types/ms": "^0.7.30", - "@types/node": "^12.6.9", + "@types/node": "^18.11.17", "@types/progress": "^2.0.5", "@typescript-eslint/eslint-plugin": "^4.28.5", "@typescript-eslint/parser": "^4.28.5", diff --git a/src/bootstrap.ts b/src/bootstrap.ts index b537b42..282431b 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -26,7 +26,11 @@ export async function bootstrap(version: string): Promise { await cluster.requestCert() } if (process.env.ENABLE_NGINX) { - await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto) + if (typeof cluster.port === 'number') { + await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto) + } else { + throw new Error('cluster.port is not a number') + } } const server = cluster.setupExpress(proto === 'https' && !process.env.ENABLE_NGINX) try { diff --git a/src/cluster.ts b/src/cluster.ts index 65aab9e..78bb47f 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -5,7 +5,8 @@ import {ChildProcess, spawn} from 'child_process' import * as colors from 'colors/safe' import * as express from 'express' import {readFileSync} from 'fs' -import {copy, ftruncate, mkdtemp, open, outputFile, pathExists, readdir, readFile, stat, unlink} from 'fs-extra' +import {rm} from 'fs/promises' +import {chmod, copy, ftruncate, mkdtemp, open, outputFile, pathExists, readdir, readFile, stat, unlink} from 'fs-extra' import got, {Got, HTTPError} from 'got' import {Server} from 'http' import {clone, template} from 'lodash' @@ -47,7 +48,7 @@ export class Cluster { private readonly prefixUrl = process.env.CLUSTER_BMCLAPI || 'https://openbmclapi.bangbang93.com' private readonly cacheDir = join(cwd(), 'cache') private readonly host: string - private _port: number + private _port: number | string private readonly publicPort: number private readonly ua: string private readonly got: Got @@ -56,7 +57,7 @@ export class Cluster { private server: Server - public get port() { + public get port(): number | string { return this._port } @@ -193,7 +194,8 @@ export class Cluster { } public async setupNginx(pwd: string, appPort: number, proto: string): Promise { - this._port++ + this._port = join(this.cacheDir, 'openbmclapi.sock') + await rm(this._port, {force: true}) const dir = await mkdtemp(join(tmpdir(), 'openbmclapi')) const confFile = `${dir}/nginx/nginx.conf` const templateFile = proto === 'https' ? 'nginx.conf' : 'nginx-http.conf' @@ -240,9 +242,13 @@ export class Cluster { } public async listen(): Promise { - return new Promise((resolve) => { + await new Promise((resolve) => { this.server.listen(this._port, resolve) }) + if (typeof this._port === 'string') { + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + await chmod(this._port, 0o777) + } } public async connect(): Promise {