0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/helpers/HttpServerHelper.ts
Refringe 5740774a46
Apply Biome Formatting
This is the result of running `npm run format` which applies the Biome formatting rules. Rejoice!
2024-07-23 11:12:53 -04:00

56 lines
1.5 KiB
TypeScript

import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
import { IHttpConfig } from "@spt/models/spt/config/IHttpConfig";
import { ConfigServer } from "@spt/servers/ConfigServer";
import { inject, injectable } from "tsyringe";
@injectable()
export class HttpServerHelper {
protected httpConfig: IHttpConfig;
protected mime = {
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",
};
constructor(@inject("ConfigServer") protected configServer: ConfigServer) {
this.httpConfig = this.configServer.getConfig(ConfigTypes.HTTP);
}
public getMimeText(key: string): string {
return this.mime[key];
}
/**
* Combine ip and port into address
* @returns url
*/
public buildUrl(): string {
return `${this.httpConfig.backendIp}:${this.httpConfig.backendPort}`;
}
/**
* Prepend http to the url:port
* @returns URI
*/
public getBackendUrl(): string {
return `http://${this.buildUrl()}`;
}
/** Get websocket url + port */
public getWebsocketUrl(): string {
return `ws://${this.buildUrl()}`;
}
public sendTextJson(resp: any, output: any): void {
resp.writeHead(200, "OK", { "Content-Type": this.mime.json });
resp.end(output);
}
}