97 lines
2.2 KiB
TypeScript
97 lines
2.2 KiB
TypeScript
type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile;
|
|
|
|
class ConfigBlock {
|
|
value: string;
|
|
children: ConfigNode[];
|
|
|
|
constructor(value: string, children: ConfigNode[]) {
|
|
this.value = value;
|
|
this.children = children;
|
|
}
|
|
|
|
build(): string {
|
|
let output = this.value;
|
|
output += " {\n ";
|
|
output += this.children
|
|
.map((child) => child.build().split("\n").join("\n "))
|
|
.join("\n ");
|
|
output += "\n}";
|
|
return output;
|
|
}
|
|
}
|
|
|
|
class ConfigStatement {
|
|
value: string;
|
|
constructor(value: string) {
|
|
this.value = value;
|
|
}
|
|
|
|
build(): string {
|
|
return this.value + ";";
|
|
}
|
|
}
|
|
|
|
class ConfigBreak {
|
|
build(): string {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
export const br = new ConfigBreak();
|
|
|
|
class ConfigFile {
|
|
nodes: ConfigNode[];
|
|
constructor(nodes: ConfigNode[]) {
|
|
this.nodes = nodes;
|
|
}
|
|
|
|
build(): string {
|
|
return this.nodes.map((n) => n.build()).join("\n");
|
|
}
|
|
}
|
|
|
|
type LooseConfigNode = ConfigNode | string;
|
|
|
|
function conformNode(looseNode: LooseConfigNode): ConfigNode {
|
|
if (typeof looseNode === "string") {
|
|
return new ConfigStatement(looseNode);
|
|
}
|
|
return looseNode;
|
|
}
|
|
|
|
function conformNodes(looseNodes: LooseConfigNode[]): ConfigNode[] {
|
|
return looseNodes.map((node) => conformNode(node));
|
|
}
|
|
|
|
export function ng(value?: string, children?: LooseConfigNode[]): ConfigNode {
|
|
const hasValue = value !== undefined && value !== "";
|
|
const hasChildren = children !== undefined;
|
|
|
|
if (!hasValue && !hasChildren) {
|
|
return new ConfigBreak();
|
|
} else if (hasValue && !hasChildren) {
|
|
return new ConfigStatement(value);
|
|
} else if (hasValue && hasChildren) {
|
|
return new ConfigBlock(value, conformNodes(children));
|
|
} else if (!hasValue && hasChildren) {
|
|
return new ConfigFile(conformNodes(children));
|
|
}
|
|
|
|
throw new Error("unreachable");
|
|
}
|
|
|
|
export const listenv4v6 = (...extras: string[]) =>
|
|
conformNodes([
|
|
`listen 443 ${["ssl", "http2", ...extras].join(" ")}`,
|
|
`listen [::]:443 ${["ssl", "http2", ...extras].join(" ")}`,
|
|
]);
|
|
|
|
export const letsEncrypt = (
|
|
domain: string,
|
|
liveDir = "/etc/letsencrypt/live"
|
|
) =>
|
|
conformNodes([
|
|
`ssl_certificate ${liveDir}/${domain}/fullchain.pem`,
|
|
`ssl_certificate_key ${liveDir}/${domain}/privkey.pem`,
|
|
]);
|