Cuipod experimenting to add new fucntionality
This repository has been archived on 2023-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
Egidijus Lileika b74707aac8 Added logging support and refactored some code
Log4J vuln inspired me to add some logging to this project :)))
2021-12-15 16:46:43 +02:00
Cuipod Added logging support and refactored some code 2021-12-15 16:46:43 +02:00
CuipodExample Added logging support and refactored some code 2021-12-15 16:46:43 +02:00
.gitignore ignore any Hg repository info 2021-07-16 20:19:54 +01:00
Cuipod.sln committing initial code. It was a fun day! 2021-02-06 17:59:11 +02:00
LICENSE Initial commit 2021-02-06 17:40:20 +02:00
README.md Added logging support and refactored some code 2021-12-15 16:46:43 +02:00

cuipod

Simple yet flexible framework for Gemini protocol servers written in C# (.NET 5.0)

Example

For testing purposes you can generate certificate with this command

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privatekey.key -out certificate.crt
using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Extensions.Logging;
using Cuipod;

namespace CuipodExample
{
    class Server
    {
        static int Main(string[] args)
        {
            X509Certificate2 cert = CertificateUtils.LoadCertificate(
                "<dir_with_cert>/certificate.crt",  // Path to certificate
                "<dir_with_cert>/privatekey.key"    // Path to private Pkcs8 RSA key
            );

            using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
                builder
                .AddSimpleConsole(options =>
                {
                    options.SingleLine = true;
                    options.TimestampFormat = "hh:mm:ss ";
                })
                .SetMinimumLevel(LogLevel.Debug)
            );
            ILogger<App> logger = loggerFactory.CreateLogger<App>();

            App app = new App(
                "pages/", // Directory to serve
                certificate,
                logger
            );

            // Serve files
            app.OnRequest("/", (request, response, logger) => {
                response.RenderFileContent("index.gmi");
            });

            // Input example
            app.OnRequest("/input", (request, response, logger) => {
                if (request.Parameters == null)
                {
                    response.SetInputHint("Please enter something: ");
                    response.Status = StatusCode.Input;
                }
                else
                {
                    // redirect to show/ route with input parameters
                    response.SetRedirectURL(request.BaseURL + "/show?" + request.Parameters);
                    response.Status = StatusCode.RedirectTemp;
                }
            });

            app.OnRequest("/show", (request, response, logger) => {
                if (request.Parameters == null)
                {
                    // redirect to input
                    response.SetRedirectURL(request.BaseURL + "/input");
                    response.Status = StatusCode.RedirectTemp;
                }
                else
                {
                    // show what has been entered
                    response.RenderPlainTextLine("# " + request.Parameters);
                }
            });

            // Or dynamically render content
            app.OnRequest("/dynamic/content", (request, response, logger) => {
                response.RenderPlainTextLine("# woah much dynamic content!");
            });

            // Optional but nice. In case it is specified and client will do a bad route 
            // request we will respond with Success status and render result from this lambda
            app.OnBadRequest((request, response, logger) => {
                response.RenderPlainTextLine("# Ohh No!!! Request is bad :(");
            });

            app.Run();

            return 0;
        }
    }
}

Full example project is in CuipodExample directory

Contribution

Feel free to raise an issue ticket or even raise a pull request.