Skip to content

Commit 7c11a0e

Browse files
committed
chore: wip
1 parent 9e76597 commit 7c11a0e

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

storage/framework/core/reverse-proxy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A modern reverse proxy. Pretty dev URLs for your local projects, with simplicity
88
- SSL Support
99
- Custom Domains
1010
- Auto HTTP to HTTPS Redirection
11+
- /etc/hosts Management
1112
- Dependency-free Binary
1213

1314
## 🤖 Usage

storage/framework/core/reverse-proxy/bin/cli.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import process from 'node:process'
21
import { cli as command } from '@stacksjs/cli'
32
import { startProxy } from '../src/start'
43
import { config } from '../src/config'
@@ -19,26 +18,35 @@ cli
1918
.option('--to <to>', 'The URL to proxy to')
2019
.option('--keyPath <path>', 'Absolute path to the SSL key')
2120
.option('--certPath <path>', 'Absolute path to the SSL certificate')
22-
.option('--all', 'Start all proxies', { default: true })
2321
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost')
2422
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
2523
.example('reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert')
2624
.action(async (options?: Options) => {
27-
const from = options?.from
28-
29-
if (!config) {
30-
console.error('No config found')
31-
process.exit(1)
32-
}
33-
34-
// Assuming config is an object where each key-value pair represents a proxy mapping
35-
for (const [from, to] of Object.entries(config)) {
25+
if (options?.from || options?.to) {
3626
startProxy({
37-
from: options?.from ?? from,
38-
to: options?.to ?? to,
27+
from: options?.from ?? 'localhost:3000',
28+
to: options?.to ?? 'stacks.localhost',
3929
keyPath: options?.keyPath,
4030
certPath: options?.certPath,
4131
})
32+
33+
return
34+
}
35+
36+
// loop over the config and start all the proxies
37+
if (config) {
38+
for (const [from, to] of Object.entries(config)) {
39+
startProxy({
40+
from,
41+
to,
42+
keyPath: options?.keyPath,
43+
certPath: options?.certPath,
44+
})
45+
}
46+
}
47+
else {
48+
// eslint-disable-next-line no-console
49+
console.log('No proxies found in the config')
4250
}
4351
})
4452

storage/framework/core/reverse-proxy/src/start.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Buffer } from 'node:buffer'
66
import { bold, green, log } from '@stacksjs/cli'
77
import { path } from '@stacksjs/path'
88
import { version } from '../package.json'
9-
import { config } from './config'
109

1110
interface Option {
1211
from?: string // domain to proxy from, defaults to localhost:3000
@@ -17,25 +16,7 @@ interface Option {
1716

1817
type Options = Option | Option[]
1918

20-
export function startProxy(option?: Option): void {
21-
startProxies(option)
22-
}
23-
24-
export function startProxies(options?: Options): void {
25-
if (Array.isArray(options)) {
26-
options.forEach((option: Option) => {
27-
startServer(option)
28-
})
29-
}
30-
else {
31-
startServer(options)
32-
}
33-
}
34-
35-
export function startServer(option: Option = {
36-
from: 'localhost:3000',
37-
to: 'stacks.localhost',
38-
}): void {
19+
export function startServer(option: Option = { from: 'localhost:3000', to: 'stacks.localhost' }): void {
3920
log.debug('Starting Reverse Proxy Server')
4021

4122
const key = fs.readFileSync(option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`))
@@ -109,3 +90,18 @@ function setupReverseProxy({ key, cert, hostname, port, option }: { key: Buffer,
10990
res.end()
11091
}).listen(80)
11192
}
93+
94+
export function startProxy(option?: Option): void {
95+
startProxies(option)
96+
}
97+
98+
export function startProxies(options?: Options): void {
99+
if (Array.isArray(options)) {
100+
options.forEach((option: Option) => {
101+
startServer(option)
102+
})
103+
}
104+
else {
105+
startServer(options)
106+
}
107+
}

0 commit comments

Comments
 (0)