-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
98 lines (92 loc) · 2.77 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { canonicalize } from "./utils.ts";
/// Interface for a server config block
interface IServerConfig {
/// The hostname for this virtual host
hostname: string;
/// Option port to listen on (default: 1965)
port?: number;
/// Server certificate
certFile: string;
/// Server private key
keyFile: string;
/// Filesystem location of the root for the server
documentRoot: string;
/// If true, reject requests ending in only '\n' (default: true)
requireCRLF?: boolean;
/// Log file location (default: ./<hostname>.log)
logFile?: string;
/// Redirects
redirects?: {
/// If true, this is a permanent redirect (default: false)
permanent?: boolean;
/// The source directory or file. Does not support wildcards
source: string;
/// The destination to redirect to
destination: string;
}[];
/// List of resources to mark as Gone
goners?: [string];
}
/**
* Interface for the config file.
*
* The IServerConfig members are duplicated here due to a typescript issue I
* couldn't figure out another way to work around.
*/
interface IConfig {
servers: {
hostname: string;
port?: number;
certFile: string;
keyFile: string;
documentRoot: string;
requireCRLF?: boolean;
logFile?: string;
redirects?: {
permanent?: boolean;
source: string;
destination: string;
}[];
goners?: [string];
}[];
}
/// Concrete server config
export class ServerConfig {
public hostname: string;
public port: number;
public certFile: string;
public keyFile: string;
public documentRoot: string;
public requireCRLF: boolean;
public logFile: string;
public redirects: Record<string, { permanent: boolean, destination: string }>;
public goners: string[];
constructor(c: IServerConfig) {
this.hostname = c.hostname;
this.port = c.port || 1965;
this.certFile = c.certFile;
this.keyFile = c.keyFile;
this.documentRoot = c.documentRoot;
this.requireCRLF = c.requireCRLF === false ? false : true;
this.logFile = c.logFile || `./${this.hostname}.log`;
this.redirects = {};
c.redirects?.forEach((r) => {
const permanent = r.permanent || false;
const destination = canonicalize(r.destination);
const source = canonicalize(r.source);
this.redirects[source] = { permanent, destination };
});
this.goners = c.goners ? c.goners.map((g) => canonicalize(g)) : [];
}
}
/// Concrete config file
export class Config {
public servers: Array<ServerConfig>;
constructor(c: IConfig) {
this.servers = c.servers.map((s: IServerConfig) => new ServerConfig(s));
}
static async fromConfigFile(configFile: string): Promise<Config> {
const config: IConfig = JSON.parse(new TextDecoder("utf-8").decode(await Deno.readFile(configFile)));
return new Config(config);
}
}