Skip to content

Commit

Permalink
feat: 当使用nginx时,node进程监听unix sock
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Dec 21, 2022
1 parent f73d5e1 commit ddcb9e5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions nginx/nginx-http.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export async function bootstrap(version: string): Promise<void> {
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 {
Expand Down
16 changes: 11 additions & 5 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -56,7 +57,7 @@ export class Cluster {

private server: Server

public get port() {
public get port(): number | string {
return this._port
}

Expand Down Expand Up @@ -193,7 +194,8 @@ export class Cluster {
}

public async setupNginx(pwd: string, appPort: number, proto: string): Promise<void> {
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'
Expand Down Expand Up @@ -240,9 +242,13 @@ export class Cluster {
}

public async listen(): Promise<void> {
return new Promise((resolve) => {
await new Promise<void>((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<void> {
Expand Down

0 comments on commit ddcb9e5

Please sign in to comment.