parent
1b718dae86
commit
1888dbacc5
96
ngineer.ts
96
ngineer.ts
|
@ -1,96 +0,0 @@
|
|||
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`,
|
||||
]);
|
7
ngx.ts
7
ngx.ts
|
@ -1,3 +1,5 @@
|
|||
const NGX_VERSION = "0.1";
|
||||
|
||||
// #region Implementation
|
||||
type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile;
|
||||
|
||||
|
@ -106,8 +108,6 @@ export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode {
|
|||
throw new Error("unreachable");
|
||||
}
|
||||
|
||||
export const br = new ConfigBreak();
|
||||
|
||||
export const listen = (...extras: string[]) =>
|
||||
conform([
|
||||
`listen 443 ${["ssl", "http2", ...extras].join(" ")}`,
|
||||
|
@ -123,7 +123,8 @@ export const letsEncrypt = (
|
|||
`ssl_certificate_key ${liveDir}/${domain}/privkey.pem`,
|
||||
]);
|
||||
|
||||
// the default export is both the ngx function and a namespace:
|
||||
export default Object.assign(
|
||||
(value?: string, children?: LooseConfigNode[]) => ngx(value, children),
|
||||
{ br, listen, letsEncrypt }
|
||||
{ NGX_VERSION, listen, letsEncrypt }
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue