2023-03-03 16:23:46 +01:00
|
|
|
import { inject, injectable } from "tsyringe";
|
|
|
|
|
2023-10-19 19:21:17 +02:00
|
|
|
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
|
|
|
import { IHttpConfig } from "@spt-aki/models/spt/config/IHttpConfig";
|
|
|
|
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class HttpServerHelper
|
|
|
|
{
|
|
|
|
protected httpConfig: IHttpConfig;
|
|
|
|
|
|
|
|
protected mime = {
|
2023-11-16 02:35:05 +01:00
|
|
|
css: "text/css",
|
|
|
|
bin: "application/octet-stream",
|
|
|
|
html: "text/html",
|
|
|
|
jpg: "image/jpeg",
|
|
|
|
js: "text/javascript",
|
|
|
|
json: "application/json",
|
|
|
|
png: "image/png",
|
|
|
|
svg: "image/svg+xml",
|
|
|
|
txt: "text/plain",
|
2023-03-03 16:23:46 +01:00
|
|
|
};
|
|
|
|
|
2023-11-16 02:35:05 +01:00
|
|
|
constructor(@inject("ConfigServer") protected configServer: ConfigServer)
|
2023-03-03 16:23:46 +01:00
|
|
|
{
|
|
|
|
this.httpConfig = this.configServer.getConfig(ConfigTypes.HTTP);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getMimeText(key: string): string
|
|
|
|
{
|
|
|
|
return this.mime[key];
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:52:55 +02:00
|
|
|
/**
|
|
|
|
* Combine ip and port into url
|
|
|
|
* @returns url
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public buildUrl(): string
|
|
|
|
{
|
|
|
|
return `${this.httpConfig.ip}:${this.httpConfig.port}`;
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:52:55 +02:00
|
|
|
/**
|
|
|
|
* Prepend http to the url:port
|
|
|
|
* @returns URI
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public getBackendUrl(): string
|
|
|
|
{
|
|
|
|
return `http://${this.buildUrl()}`;
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:52:55 +02:00
|
|
|
/** Get websocket url + port */
|
2023-03-03 16:23:46 +01:00
|
|
|
public getWebsocketUrl(): string
|
|
|
|
{
|
|
|
|
return `ws://${this.buildUrl()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public sendTextJson(resp: any, output: any): void
|
|
|
|
{
|
2023-11-16 16:09:01 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
2023-11-13 18:38:16 +01:00
|
|
|
resp.writeHead(200, "OK", { "Content-Type": this.mime.json });
|
2023-03-03 16:23:46 +01:00
|
|
|
resp.end(output);
|
|
|
|
}
|
2023-11-16 02:35:05 +01:00
|
|
|
}
|