astronomical-theater/example.js

81 lines
3.2 KiB
JavaScript

import GeminiServer from "./GeminiServer.js";
import StaticHandler from "./handlers/static.js";
import DefaultHandler from "./handlers/default.js";
import ReverseProxyHandler from "./handlers/revproxy.js";
// import {, staticFileHandler} from "./main.js";
// If you'r going to be reverse-proxying a server with a self-signed cert, you need
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
const server = new GeminiServer({
// port: 1965
//Option 1 (a single hostname):
hostname: "localhost",
key: "private-key.pem",
cert: "public-cert.pem",
//Option 2 (if you want virtual hosting)
/*host: [
{
hostnames: ["localhost", "127.0.0.1"],
key: "private-key.pem",
cert: "public-cert.pem"
},
{
hostnames: ["MacBookGamma.local"],
key: "mac-gamma-key.pem",
cert: "mac-gamma-pub-cert.pem",
},
{
hostnames: ["*.example.com"],
key: "private-key.pem",
cert: "ex-public-cert.pem"
}
]*/
});
//Registering a path with a string returns the string as Gemini text
server.registerPath("localhost/", "### Hello, world");
//Anything before the first slash is a domain name and is used in SNI matching if you passed the `host` option
// server.registerPath("MacBookGamma.local/", "### Hello from my Mac!");
// server.registerPath("127.0.0.1/", "Hello, world.");
//Wild-card domains are supported like they are by TLS, only one "*" and it must be first.
//Wild-card's aren't supported in paths or anywhere else
// server.registerPath("*.example.com/", "This is a catch-all domain");
//You can subclass DefaultHandler, which has a .handle method, that takes a url and a basePath
//You have to provide the entire response including content type
class ExampleHandler extends DefaultHandler {
constructor() {
super();
}
handle(url, basePath) {
return "20 text/txt\r\nHello, World\r\n";
}
}
server.registerPath("localhost/allFiles", new ExampleHandler());
// We will provide some convenient handlers for static, CGI, and reverse proxy
//if the passed file is a single file, it's a file. If it's a directory, all sub-files are auto-included
server.registerPath("localhost/static/about.gmi", new StaticHandler("README.gmi" /*, {options}*/));
server.registerPath("localhost/static", new StaticHandler("static/" /*, {options}*/));
// server.registerPath("localhost/static", new StaticHandler("/tmp/yourmom/" /*, {options}*/));
//The file passed to CGI handler must exist and be executable at run time
//You know what, CGIHandler hashes the file
//No dynamically generated or changed CGI files
//No enabling CGI running for a certain file extension
//No enabling CGI running for a certain path. This must take a single file
//500 CGI files, you better write 500 registerPath's (or write a custom handler)
// server.registerPath("localhost/run", CGIhandler("/path/to/test.py"));
//Kinda cursed but whatever
// server.registerPath("localhost/thoughts", proxyHandler("gemini://localhost:8888"));
server.registerPath("/blaseball", new ReverseProxyHandler("localhost:1973"));
server.registerPath("/thoughts", new ReverseProxyHandler("thoughts.learnerpages.com/about"));
server.run();