Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port manager #42

Merged
merged 27 commits into from
Nov 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bcd10b7
start setting up port manager
camden11 Sep 26, 2023
d028563
detectPort first pass
camden11 Sep 26, 2023
82cd169
Fix port conflict bug
camden11 Sep 26, 2023
ecc19c8
Port manager get/set functionality
camden11 Sep 26, 2023
9b660ea
add delete endpoint + clean some things up
camden11 Sep 26, 2023
e151907
port manager module first pass
camden11 Sep 27, 2023
75726b0
export port manager
camden11 Sep 27, 2023
5766ea7
rename some things and add server index route
camden11 Sep 27, 2023
c2e3013
Fix some portmanager bugs
camden11 Sep 28, 2023
060b9f6
Fix some port manager bugs
camden11 Sep 28, 2023
d5ce5cd
use cors
camden11 Oct 3, 2023
61d81d4
Add logging/errors to PortManagerServer
camden11 Oct 4, 2023
b83750f
Merge branch 'main' into port-manager
camden11 Oct 18, 2023
73d05b1
Wait for PortManagerServer to start
camden11 Oct 18, 2023
90ae978
rename assignPortToServerInstance
camden11 Oct 18, 2023
e79df2a
Set up port manager for requesting multiple ports
camden11 Oct 18, 2023
1d30147
fix bug in requestPort
camden11 Oct 19, 2023
91a3228
Merge branch 'main' into port-manager
camden11 Oct 31, 2023
0bb5532
Update request ports to allow for specifying ports
camden11 Oct 31, 2023
d191ff3
port manager api tweaks
camden11 Oct 31, 2023
053dba6
Merge branch 'main' into port-manager
camden11 Nov 7, 2023
872304f
remove todo
camden11 Nov 7, 2023
2726eec
restore portmanager types file
camden11 Nov 7, 2023
de09bad
Merge branch 'main' into port-manager
camden11 Nov 8, 2023
25eb5ea
Add detectPort copy to lang file
camden11 Nov 8, 2023
825d86b
Add detect port license
camden11 Nov 8, 2023
bd1e9cb
update invalid port copy
camden11 Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/portManager.ts
Original file line number Diff line number Diff line change
@@ -27,11 +27,11 @@ export async function stopPortManagerServer(): Promise<void> {
}
}

export async function requestPortForServerInstance(
serverInstanceId: string
): Promise<number> {
export async function requestPorts(
serverInstanceIds: Array<string>
): Promise<Array<number>> {
const { data } = await axios.post(`${BASE_URL}/servers`, {
instanceId: serverInstanceId,
instanceIds: serverInstanceIds,
});

return data.port;
48 changes: 30 additions & 18 deletions utils/PortManagerServer.ts
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ class PortManagerServer {

this.app.get('/servers', this.getServers);
this.app.get('/servers/:instanceId', this.getServerPortByInstanceId);
this.app.post('/servers', this.assignPortToServer);
this.app.post('/servers', this.assignPortsToServers);
this.app.delete('/servers/:instanceId', this.deleteServerInstance);
this.app.post('/close', this.closeServer);
}
@@ -116,24 +116,36 @@ class PortManagerServer {
}
};

assignPortToServer = async (req: Request, res: Response): Promise<void> => {
const { instanceId, port } = req.body;

if (this.serverPortMap[instanceId]) {
res.status(409).send(
i18n(`errors.${i18nKey}.409`, {
instanceId,
port: this.serverPortMap[instanceId],
})
);
} else if (port && (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER)) {
res.status(400).send(i18n(`errors.${i18nKey}.400`));
} else {
const portToUse = await detectPort(port);
this.setPort(instanceId, portToUse);
assignPortsToServers = async (req: Request, res: Response): Promise<void> => {
const { instanceIds, port } = req.body;

res.send({ port: portToUse });
}
const portPromises: Array<Promise<number>> = [];

instanceIds.forEach((instanceId: number) => {
if (this.serverPortMap[instanceId]) {
res.status(409).send(
i18n(`errors.${i18nKey}.409`, {
instanceId,
port: this.serverPortMap[instanceId],
})
);
return;
} else if (port && (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER)) {
res.status(400).send(i18n(`errors.${i18nKey}.400`));
return;
} else {
portPromises.push(detectPort(port));
}
});

const ports = await Promise.all(portPromises);

ports.forEach((port: number, index: number) => {
const instanceId = instanceIds[index];
this.setPort(instanceId, port);
});

res.send({ ports });
};

deleteServerInstance = (req: Request, res: Response): void => {