Algernon Web Server

repository·main·Indexed 25 days ago

https://github.com/xyproto/algernon

A high-performance, self-contained web server written in Go. It serves as an all-in-one application server with native support for React (JSX, TypeScript), Lua, Teal, and multiple templating engines like Pongo2, Amber, and Markdown. It features built-in support for various database backends (BoltDB, PostgreSQL, SQLite, MySQL, etc.), WebAuthn/FIDO2 passwordless authentication, and multiple protocols including HTTP/2 and HTTP/3/QUIC. Applications can be packaged as .alg files and deployed via Docker using the alg2docker utility.

Tokens
15.2K
Snippets
47
Records
83
Agent score
84%

What's inside algernon

  1. Overview of Algernon features

    main

    Algernon is a self-contained web server written in Go that supports multiple protocols (HTTP, HTTP/2, HTTP/3/QUIC) and a wide array of templating and scripting languages.

    Key capabilities include:

    • Scripting: Built-in Lua and Teal support.
    • Templating/Frontend: Markdown, Pongo2, Amber, Sass/SCSS, GCSS, JSX, and TypeScript (React 19).
    • Databases: Built-in BoltDB (file-based), with support for Redis/Valkey, PostgreSQL, SQLite, MariaDB, MySQL, and MSSQL.
    • Advanced Features: Rate limiting, graceful shutdown, user permissions, and plugin support.
  2. Build and run an Algernon Docker image

    main

    After generating a Dockerfile with alg2docker, follow these steps to build and run the container:

    1. Build the image:

      docker build -t <image_name> .
    2. Run the container with HTTPS support: To serve the application over HTTPS, you must mount your local SSL certificates (cert.pem and key.pem) into the container at /etc/algernon using a volume.

      Ensure your certificates are located in $PWD/config before running:

      docker run -v "$PWD/config":/etc/algernon --publish 80:80 --publish 443:443 --rm <image_name>
  3. Understand Moonscript handler modes in Algernon

    main

    When using Moonscript-compiled Lua files with Algernon, the behavior depends on how the Lua file is served:

    1. Server Configuration Mode: If hello.lua (compiled from hello.moon) is served as a server configuration script, it sets up a simple handle.
    2. Regular Handler Mode: If index.lua (compiled from index.moon) is served as a regular Algernon handler, it functions as a standard request handler.

    Note that different Lua functions are available depending on which of these two modes is active.

  4. Run Algernon using Docker

    main

    You can run a lightweight Algernon instance using Docker. This example creates a local directory, adds a Markdown file, and mounts it to the container to serve it on port 4000.

    mkdir localhost
    echo 'hi!' > localhost/index.md
    docker run -it -p4000:4000 -v .:/srv/algernon xyproto/algernon
  5. Serve a directory of files

    main

    To serve a directory as a static file server, create an index.lua file within that directory. Algernon will treat the directory as the root and use index.lua as the default handler for the directory path.

    To serve the current directory:

    # Create index.lua with content
    echo 'print("the light")' > index.lua
    
    # Run Algernon on the current directory
    algernon .
    algernon .
  6. Enable auto-refresh feature

    main

    Algernon includes an auto-refresh feature that watches files for changes and uses Server-Sent Events (SSE) to trigger updates. It works by injecting a small amount of JavaScript into the served pages.

    To enable auto-refresh, you must use the -a flag. It is often used in conjunction with development mode (-e).

    Example command to serve the current directory with auto-refresh, development mode, and no security headers:

    algernon -a -e --no-headers .