|
1 | | -import * as fs from 'fs'; |
2 | | -import * as path from 'path'; |
3 | | - |
4 | | -interface Route { |
5 | | - method: string; |
6 | | - path: string; |
7 | | - target: string; |
8 | | -} |
9 | | - |
10 | | -interface PortConfig { |
11 | | - port: number; |
12 | | - gateway: string; |
13 | | - routes: Route[]; |
14 | | -} |
15 | | - |
16 | | -interface Config { |
17 | | - ports: PortConfig[]; |
18 | | -} |
19 | | - |
20 | | -const config: Config = { |
21 | | - ports: [ |
22 | | - { |
23 | | - port: 9004, |
24 | | - gateway: '127.0.0.1:2004', |
25 | | - routes: [ |
26 | | - { method: 'POST', path: '/user/login', target: '127.0.0.1:4561' }, |
27 | | - { method: 'PUT', path: '/user/update', target: '127.0.0.1:4561' }, |
28 | | - { method: 'GET', path: '/transaction/get', target: '127.0.0.1:9761' } |
29 | | - ] |
30 | | - } |
31 | | - ] |
32 | | -}; |
33 | | - |
34 | | -function generateNginxConfig(portConfig: PortConfig): string { |
35 | | - let config = ` |
36 | | -server { |
37 | | - listen ${portConfig.port}; |
38 | | - server_name localhost; |
39 | | -
|
40 | | - location / { |
41 | | - proxy_set_header Host $host; |
42 | | - proxy_pass http://${portConfig.gateway}; |
43 | | - proxy_http_version 1.1; |
44 | | - proxy_set_header Upgrade $http_upgrade; |
45 | | - proxy_set_header Connection "upgrade"; |
46 | | - } |
47 | | -`; |
48 | | - |
49 | | - portConfig.routes.forEach(route => { |
50 | | - config += ` |
51 | | - location ${route.path} { |
52 | | - if ($request_method = ${route.method}) { |
53 | | - proxy_set_header Host $host; |
54 | | - proxy_pass http://${route.target}; |
55 | | - proxy_http_version 1.1; |
56 | | - proxy_set_header Upgrade $http_upgrade; |
57 | | - proxy_set_header Connection "upgrade"; |
58 | | - } |
59 | | - }`; |
60 | | - }); |
61 | | - |
62 | | - config += ` |
63 | | -} |
64 | | -`; |
65 | | - |
66 | | - return config; |
67 | | -} |
68 | | - |
69 | | -function writeConfigToFile(port: number, configContent: string) { |
70 | | - const configPath = path.join('/etc/nginx/sites-enabled', `${port}`); |
71 | | - |
72 | | - if (fs.existsSync(configPath)) { |
73 | | - fs.unlinkSync(configPath); |
74 | | - } |
75 | | - |
76 | | - fs.writeFileSync(configPath, configContent, 'utf8'); |
77 | | - console.log(`Config for port ${port} written to ${configPath}`); |
78 | | -} |
79 | | - |
80 | | -function generateAllConfigs(config: Config) { |
81 | | - config.ports.forEach(portConfig => { |
82 | | - const nginxConfig = generateNginxConfig(portConfig); |
83 | | - writeConfigToFile(portConfig.port, nginxConfig); |
84 | | - }); |
85 | | -} |
| 1 | +import { config } from './config'; |
| 2 | +import { generateAllConfigs } from './generateNginxConfig'; |
86 | 3 |
|
87 | 4 | generateAllConfigs(config); |
0 commit comments