馃 An object-oriented Gemini server for Deno!
gemini-protocol deno typescript gemini
at main 86 lines 2.3 kB view raw
1// This file is part of Laurali <https://github.com/gemrest/laurali>. 2// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU General Public License as published by 6// the Free Software Foundation, version 3. 7// 8// This program is distributed in the hope that it will be useful, but 9// WITHOUT ANY WARRANTY; without even the implied warranty of 10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11// General Public License for more details. 12// 13// You should have received a copy of the GNU General Public License 14// along with this program. If not, see <http://www.gnu.org/licenses/>. 15// 16// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me> 17// SPDX-License-Identifier: GPL-3.0-only 18 19import { hook, route, Server } from "../mod.ts"; 20import * as optic from "https://deno.land/x/optic@1.3.5/mod.ts"; 21 22/** Implement a new Laurali server */ 23class MyCoolServer extends Server { 24 /** Track the number of total visits to our server */ 25 static clicks = 0; 26 /** A logger to give us nice looking logs */ 27 static logger = new optic.Logger(); 28 29 /** Visit `/` */ 30 @route("/") 31 index() { 32 return "Hello, world!"; 33 } 34 35 /** Visit `/test` */ 36 @route() 37 test(ctx: Deno.TcpConn) { 38 return 2 + ctx.localAddr.transport; 39 } 40 41 /** Visit `/random` */ 42 @route() 43 random() { 44 return Math.floor(Math.random() * 10); 45 } 46 47 /** Visit `/clicks` */ 48 @route("/clicks") 49 clicksRoute() { 50 return MyCoolServer.clicks; 51 } 52 53 @hook() 54 override onPreRoute(ctx: Deno.TlsConn) { 55 MyCoolServer.clicks += 1; 56 57 MyCoolServer.logger.info( 58 `Opened connection with ${ctx.remoteAddr.transport} and incremented ` + 59 `\`clicks\` to ${MyCoolServer.clicks}.`, 60 ); 61 } 62 63 @hook() 64 override onPostRoute() { 65 MyCoolServer.logger.info("Closed connection."); 66 } 67 68 @hook() 69 override onError() { 70 return "hi"; 71 } 72 73 override onListen() { 74 MyCoolServer.logger.info( 75 `Listening on ${MyCoolServer.hostname}:${MyCoolServer.port}.`, 76 ); 77 } 78} 79 80(new MyCoolServer(".laurali/public.pem", ".laurali/private.pem", { 81 proxy: { 82 enable: true, 83 baseURL: "https://fuwn.me/proxy/", 84 hostname: "fuwn.me", 85 }, 86})).listen();