diff options
| author | Stanisław Zagórowski <hasohasacz@gmail.com> | 2024-01-19 22:38:58 +0100 |
|---|---|---|
| committer | Stanisław Zagórowski <hasohasacz@gmail.com> | 2024-01-19 22:38:58 +0100 |
| commit | bae8d91990f7c1abb98b2b640513984a0e592db1 (patch) | |
| tree | 6fafa6b2e78ea5fc2a1809c81f3984833a071c30 | |
| parent | 90d05fd9911596db33a20a164f92a51ffd067053 (diff) | |
example: add api and htmx example
| -rw-r--r-- | example/api/reluax.luax | 21 | ||||
| -rw-r--r-- | example/htmx/reluax.luax | 39 |
2 files changed, 60 insertions, 0 deletions
diff --git a/example/api/reluax.luax b/example/api/reluax.luax new file mode 100644 index 0000000..cae3d33d --- /dev/null +++ b/example/api/reluax.luax @@ -0,0 +1,21 @@ +local message = "default" + +local route = function(path, method, body) + if path ~= "/api/example" then + return 404, reluax.json { message = "Not Found" } + end + + if method == "GET" then + return 200, reluax.json { message = message } + elseif method == "POST" then + message = body + return 200, reluax.json { message = message } + end + + return 400, reluax.json { message = "Bad Request" } +end + +return { + name = "api-example", + route = route +} diff --git a/example/htmx/reluax.luax b/example/htmx/reluax.luax new file mode 100644 index 0000000..8ab1cb8 --- /dev/null +++ b/example/htmx/reluax.luax @@ -0,0 +1,39 @@ +local page_wrapper = function(content) + return reluax.html_page( + <html> + <head> + <title>reluax</title> + <script src="https://unpkg.com/htmx.org@1.9.10"></script> + </head> + <body> + {$ content $} + </body> + </html> + ) +end + +local route = function(path, method, body) + if path == "/" and method == "GET" then + return 200, page_wrapper( + <div> + <h1>reluax</h1> + <p>Click the button to get a random number.</p> + <button hx-post="/random" hx-swap="outerHTML">Random</button> + </div> + ) + elseif path == "/random" and method == "POST" then + return 200, reluax.html(<h3>{$ math.random(1, 100) $}</h3>) + end + + return 404, page_wrapper( + <div> + <h1>reluax</h1> + <p>404 Not Found</p> + </div> + ) +end + +return { + name = "htmx-example", + route = route +} |
