Skip to content

Commit 9ee237f

Browse files
committed
clean
1 parent 66a0358 commit 9ee237f

File tree

6 files changed

+88
-85
lines changed

6 files changed

+88
-85
lines changed

src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { PortConfig } from './type';
2+
3+
export const config: { ports: PortConfig[] } = {
4+
ports: [
5+
{
6+
port: 9004,
7+
gateway: '127.0.0.1:2004',
8+
routes: [
9+
{ method: 'POST', path: '/user/login', target: '127.0.0.1:4561' },
10+
{ method: 'PUT', path: '/user/update', target: '127.0.0.1:4561' },
11+
{ method: 'GET', path: '/transaction/get', target: '127.0.0.1:9761' }
12+
]
13+
}
14+
]
15+
};

src/generateNginxConfig.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { writeConfigToFile } from './nginxPath';
2+
import { generateRoutesConfig } from './routes';
3+
4+
export function generateNginxConfig(portConfig: { port: number, gateway: string, routes: any[] }): string {
5+
let config = `
6+
server {
7+
listen ${portConfig.port};
8+
server_name localhost;
9+
10+
location / {
11+
proxy_set_header Host $host;
12+
proxy_pass http://${portConfig.gateway};
13+
proxy_http_version 1.1;
14+
proxy_set_header Upgrade $http_upgrade;
15+
proxy_set_header Connection "upgrade";
16+
}
17+
`;
18+
19+
config += generateRoutesConfig(portConfig.routes);
20+
21+
config += `
22+
}
23+
`;
24+
25+
return config;
26+
}
27+
28+
export function generateAllConfigs(config: { ports: any[] }) {
29+
config.ports.forEach(portConfig => {
30+
const nginxConfig = generateNginxConfig(portConfig);
31+
writeConfigToFile(portConfig.port, nginxConfig);
32+
});
33+
}

src/index.ts

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,4 @@
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';
863

874
generateAllConfigs(config);

src/nginxPath.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import path from 'path';
2+
import { existsSync, unlinkSync, writeFileSync } from 'fs';
3+
4+
export function writeConfigToFile(port: number, configContent: string) {
5+
const configPath = path.join('/etc/nginx/sites-enabled', `${port}`);
6+
7+
if (existsSync(configPath)) {
8+
unlinkSync(configPath);
9+
}
10+
11+
writeFileSync(configPath, configContent, 'utf8');
12+
console.log(`Config for port ${port} written to ${configPath}`);
13+
}

src/routes.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Route } from './type';
2+
3+
export function generateRoutesConfig(routes: Route[]): string {
4+
return routes.map(route => `
5+
location ${route.path} {
6+
if ($request_method = ${route.method}) {
7+
proxy_set_header Host $host;
8+
proxy_pass http://${route.target};
9+
proxy_http_version 1.1;
10+
proxy_set_header Upgrade $http_upgrade;
11+
proxy_set_header Connection "upgrade";
12+
}
13+
}`).join('');
14+
}

src/type.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export interface Route {
2+
method: string;
3+
path: string;
4+
target: string;
5+
}
6+
7+
export interface PortConfig {
8+
port: number;
9+
gateway: string;
10+
routes: Route[];
11+
}

0 commit comments

Comments
 (0)