Skip to content

Commit

Permalink
支持byoc
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jun 15, 2022
1 parent 0770721 commit 811a8d1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
54 changes: 54 additions & 0 deletions nginx/nginx-http.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
daemon off;
events {
worker_connections 10240;
accept_mutex on;
}
pid <%= root %>/nginx.pid;
worker_rlimit_nofile 65535;
http {
include mime.types;
default_type application/octet-stream;
access_log <%= root %>/access.log;
error_log stderr;

map $arg_name $name {
~.+ $arg_name;
default $prefix;
}

server {
listen <%= port %> default;
root <%= root %>/cache;

if ($uri ~ ^/download/(..)) {
set $prefix $1;
}

location @be {
proxy_pass http://localhost:<%= port+1 %>;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location ~ /download/(.*) {
set $hash $1;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
add_header x-bmclapi-hash $hash;
try_files /$prefix/$hash @be;
add_header content-disposition "attachment; filename=\"$arg_name\"";

}

location / {
proxy_pass http://localhost:<%= port+1 %>;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
11 changes: 7 additions & 4 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ export async function bootstrap(version: string): Promise<void> {
await cluster.syncFiles(files)

await cluster.connect()
console.log('请求证书')
await cluster.requestCert()
const proto = process.env.CLUSTER_BYOC !== 'true' ? 'https' : 'http'
if (proto === 'https') {
console.log('请求证书')
await cluster.requestCert()
}
if (process.env.ENABLE_NGINX) {
await cluster.setupNginx(join(__dirname, '..'), cluster.port)
await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto)
}
const server = cluster.setupExpress(!process.env.ENABLE_NGINX)
const server = cluster.setupExpress(proto === 'https')
try {
await cluster.listen()
await cluster.enable()
Expand Down
4 changes: 2 additions & 2 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ export class Cluster {
return this.server
}

public async setupNginx(pwd: string, appPort: number): Promise<void> {
public async setupNginx(pwd: string, appPort: number, proto: string): Promise<void> {
this._port++
const dir = await mkdtemp(join(tmpdir(), 'openbmclapi'))
const confFile = `${dir}/nginx/nginx.conf`
const confFile = proto === 'https' ? `${dir}/nginx/nginx.conf` : `${dir}/nginx/nginx-http.conf`
const confTemplate = await readFile(join(__dirname, '..', 'nginx', 'nginx.conf'), 'utf8')
console.log('nginx conf', confFile)

Expand Down

0 comments on commit 811a8d1

Please sign in to comment.