aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: c628c07de3c3e5b45e77c57a87c9c9897b2b17a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<p align="center"><img src="assets/logo.svg" /></p>

# Reluax

![Crates.io Version](https://img.shields.io/crates/v/reluax)

`reluax` lets you easily create dynamic websites in a custom dialect of Lua,
inspired by JSX. The service has been written in Rust, and uses `luajit` as
its Lua runtime for performance and ease of integration of other libraries.

## Requirements
- `luajit`

## Installation
Currently, the project is only supported on Linux and MacOS.

Reluax is available through GitHub releases as a prebuilt binary, or
from `crates.io` through `cargo install reluax`, if you have the Rust
toolchain installed. Alternatively, it is packaged as a Nix flake:
if you have Nix installed, you can `nix run github:Duckonaut/reluax`.

## Usage
```
Commands:
  serve  Serve a directory of LuaX files in production mode
  build  Build a directory of LuaX files
  dev    Serve a directory of LuaX files in development mode
  new    Create a new project
  init   Initialize a new project in the current directory
  help   Print this message or the help of the given subcommand(s)

Options:
  -h, --help  Print help

Examples:
  reluax serve
  reluax serve --port 4000
  reluax dev -P public/ -C luax/
```

To create an example project, run `reluax new my-first-project`.

## Inspiration
The project was heavily inspired by Ben Visness' blog post,
[I made JSX for Lua (because I hate static sites)](https://bvisness.me/luax/),
which I highly recommend reading.

## API
A Reluax project is very simple. Reluax expects to run a directory of files,
with the entry point being `reluax.luax` or `reluax.lua`. This module needs
to return a table containing the member function `route`, and optionally a
project name under the key `name`. For code examples, check the
[examples](https://github.com/Duckonaut/reluax/tree/main/example) directory.

The `route` function will be called with the path and, optionally, method and
body of a request, and can return a variety of responses, by returning two
values: the status code, and the response body.

The response body will usually be a table, and by default will be treated as a HTML
page (see `example/basic/`). It can be optionally wrapped using the functions
`reluax.html` or `reluax.json`, the first of which will make sure the HTML is returned
as is, without a `<!DOCTYPE html>` tag, and the second returning the table as a JSON
object.

With this you can build a rather powerful backend, handling templating, routing, and
anything else through LuaX code.

The `reluax` global table contains several utility functions, described below:
- `reluax.json`: wrap the table to be interpreted as a JSON response,
- `reluax.html_page`: wrap the table to be interpreted as a full HTML page (default behavior),
- `reluax.html`: wrap the table to be interpreted as a HTML excerpt (for e.g. use with
  [htmx](https://htmx.org)),
- `reluax.path_matches`: check if a path string matches the template,
- `reluax.path_extract`: extract named path parameters from the path.