Server/project/src/helpers/HttpServerHelper.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
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
{
// eslint-disable-next-line @typescript-eslint/naming-convention
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
}