From 7ea3f8e0c17815a6e52240c6daf7677853127201 Mon Sep 17 00:00:00 2001 From: Charlotte Som Date: Wed, 27 Nov 2024 05:34:18 +0200 Subject: [PATCH] v0.2: new http2 syntax, drop write(..), add serverName(..) --- deno.json | 2 +- ngx.ts | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/deno.json b/deno.json index 1febbed..9188c6a 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,5 @@ { "name": "@char/ngx", - "version": "0.1.1", + "version": "0.2.0", "exports": "./ngx.ts" } diff --git a/ngx.ts b/ngx.ts index 114e33e..7d1df7f 100644 --- a/ngx.ts +++ b/ngx.ts @@ -1,4 +1,4 @@ -const NGX_VERSION = "0.1.1"; +const NGX_VERSION = "0.2.0"; type ConfigNode = ConfigStatement | ConfigBlock | ConfigBreak | ConfigFile; @@ -6,11 +6,6 @@ class ConfigBuildable { build(): string { throw new Error("build(..) not implemented!"); } - - write(path: string) { - const contents = this.build(); - Deno.writeTextFileSync(path, contents); - } } class ConfigBlock extends ConfigBuildable { @@ -24,7 +19,7 @@ class ConfigBlock extends ConfigBuildable { this.children = children; } - build(): string { + override build(): string { let output = this.value; output += " {\n "; output += this.children @@ -43,13 +38,13 @@ class ConfigStatement extends ConfigBuildable { this.value = value; } - build(): string { + override build(): string { return this.value + ";"; } } class ConfigBreak extends ConfigBuildable { - build(): string { + override build(): string { return ""; } } @@ -62,7 +57,7 @@ class ConfigFile extends ConfigBuildable { this.nodes = nodes; } - build(): string { + override build(): string { return this.nodes.map((n) => n.build()).join("\n"); } } @@ -82,7 +77,7 @@ function conform(looseNode: LooseConfigNode): ConfigNode[] { return looseNode .map((n) => conform(n)) .reduceRight((b, a) => - a.length === 1 ? [...a, ...b] : [...a, new ConfigBreak(), ...b] + a.length === 1 ? [...a, ...b] : [...a, new ConfigBreak(), ...b], ); } } @@ -108,13 +103,16 @@ export function ngx(value?: string, children?: LooseConfigNode[]): ConfigNode { export const listen = (...extras: string[]) => conform([ - `listen 443 ${["ssl", "http2", ...extras].join(" ")}`, - `listen [::]:443 ${["ssl", "http2", ...extras].join(" ")}`, + `listen 443 ${["ssl", ...extras].join(" ")}`, + `listen [::]:443 ${["ssl", ...extras].join(" ")}`, + `http2 on`, ]); +export const serverName = (name: string) => conform([`server_name ${name}`]); + export const letsEncrypt = ( domain: string, - liveDir = "/etc/letsencrypt/live" + liveDir = "/etc/letsencrypt/live", ) => conform([ `ssl_certificate ${liveDir}/${domain}/fullchain.pem`, @@ -124,5 +122,5 @@ export const letsEncrypt = ( // the default export is both the ngx function and a namespace: export default Object.assign( (value?: string, children?: LooseConfigNode[]) => ngx(value, children), - { NGX_VERSION, listen, letsEncrypt } + { NGX_VERSION, listen, letsEncrypt, serverName }, );