aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-05-17 11:43:10 +0000
committerFuwn <[email protected]>2022-05-17 11:43:17 +0000
commit630cb6f89a8519036fdd31eb891348aebcbf01eb (patch)
tree032cefae757baf9ab41ada474f5830af9eb5c9b0
parentfix(decorators): assumed route format (diff)
downloadlaurali-main.tar.xz
laurali-main.zip
feat(server): optional http proxy redirectHEAD0.1.2main
-rw-r--r--examples/my_cool_server.ts8
-rw-r--r--laurali/configuration.ts43
-rw-r--r--laurali/mod.ts1
-rw-r--r--laurali/server.ts27
4 files changed, 70 insertions, 9 deletions
diff --git a/examples/my_cool_server.ts b/examples/my_cool_server.ts
index 793e01f..d633e10 100644
--- a/examples/my_cool_server.ts
+++ b/examples/my_cool_server.ts
@@ -77,4 +77,10 @@ class MyCoolServer extends Server {
}
}
-(new MyCoolServer(".laurali/public.pem", ".laurali/private.pem")).listen();
+(new MyCoolServer(".laurali/public.pem", ".laurali/private.pem", {
+ proxy: {
+ enable: true,
+ baseURL: "https://fuwn.me/proxy/",
+ hostname: "fuwn.me",
+ },
+})).listen();
diff --git a/laurali/configuration.ts b/laurali/configuration.ts
new file mode 100644
index 0000000..4904310
--- /dev/null
+++ b/laurali/configuration.ts
@@ -0,0 +1,43 @@
+// This file is part of Laurali <https://github.com/gemrest/laurali>.
+// Copyright (C) 2022-2022 Fuwn <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3.
+//
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+//
+// Copyright (C) 2022-2022 Fuwn <[email protected]>
+// SPDX-License-Identifier: GPL-3.0-only
+
+/** Configuration options of a `Server`'s HTTP proxy */
+export interface ProxyConfiguration {
+ /** Allow Laurali to proxy */
+ enable?: boolean;
+ /**
+ * The base URL of the HTTP proxy server
+ * @default "https://fuwn/me/proxy/"
+ */
+ baseURL?: string;
+ /**
+ * The hostname of **this** Laurali server
+ * @default ServerConfiguration.hostname
+ */
+ hostname?: string;
+}
+
+/** Configuration options of a `Server` */
+export interface ServerConfiguration {
+ /** The port a `Server` will listen on */
+ port?: number;
+ /** The hostname a `Server` will identify as */
+ hostname?: string;
+ /** Proxy your Gemini content by specifying an HTTP proxy */
+ proxy?: ProxyConfiguration;
+}
diff --git a/laurali/mod.ts b/laurali/mod.ts
index 39c6389..6a5c854 100644
--- a/laurali/mod.ts
+++ b/laurali/mod.ts
@@ -16,6 +16,7 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+export * from "./configuration.ts";
export * from "./decorators.ts";
export * from "./hooks.ts";
export * from "./server.ts";
diff --git a/laurali/server.ts b/laurali/server.ts
index b647dc2..f8c641f 100644
--- a/laurali/server.ts
+++ b/laurali/server.ts
@@ -16,16 +16,9 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+import { ServerConfiguration } from "./configuration.ts";
import { Hook } from "./hooks.ts";
-/** Configuration options of a `Server` */
-export interface ServerConfiguration {
- /** The port a `Server` will listen on */
- port?: number;
- /** The hostname a `Server` will identify as */
- hostname?: string;
-}
-
/** The base Laurali server to be extended upon */
export abstract class Server {
/**
@@ -65,6 +58,24 @@ export abstract class Server {
certFile,
keyFile,
});
+
+ if (config?.proxy?.enable) {
+ if (config.proxy.baseURL === undefined) {
+ throw new Error("ProxyConfiguration is missing proxy baseURL");
+ }
+
+ this.#proxy(config.proxy.baseURL, config.proxy.hostname || hostname);
+ }
+ }
+
+ async #proxy(baseURL: string, host: string) {
+ const server = Deno.listen({ port: 8080 });
+
+ for await (const c of server) {
+ for await (const r of Deno.serveHttp(c)) {
+ r.respondWith(Response.redirect(baseURL + host));
+ }
+ }
}
/** Add a route function to the `Server` */