diff options
| author | unwox <me@unwox.com> | 2024-12-27 17:10:01 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-12-27 17:42:07 +0600 |
| commit | 7db107d070ff7c853dda50201954e8c40c07b6e9 (patch) | |
| tree | ed59fcd69cc353d1941f73c3945475e41efc63a8 | |
| parent | 3601f923be9012a50a5d53f2a3ae5cfcde58ca7b (diff) | |
add an example site code
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | example/main.lua | 103 | ||||
| -rwxr-xr-x | example/run.sh | 23 | ||||
| -rw-r--r-- | example/static/style.css | 17 |
5 files changed, 146 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a4eeca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +example/db.sqlite* @@ -9,6 +9,8 @@ following key features: * small and maintainable codebase * handles relatively high number of requests per second +See an [example site](https://git.sr.ht/~unwox/luna/tree/master/item/example). + ## Build diff --git a/example/main.lua b/example/main.lua new file mode 100644 index 0000000..98e3ea8 --- /dev/null +++ b/example/main.lua @@ -0,0 +1,103 @@ +-- a helper for handling API results: throws an error if the call +-- was unsuccessful, otherwise returns the result +function must(...) + local ok, result = ... + if not ok then + error(result) + end + return result +end + +-- open a database connection +local db = must(luna.db.open("file:db.sqlite?_journal=WAL&_sync=NORMAL")) +must(luna.db.exec(db, [[ + CREATE TABLE IF NOT EXISTS hits(creation_time DATETIME NOT NULL); +]], {})) + +-- register a GET / route that displays a home page for the example site +luna.router.route("GET /", function (request) + local response = "{\"method\": \"" .. request.method .. "\"" + response = response .. ", \"path\": \"" .. request.path .. "\"}" + + return 200, {["Content-Type"] = "text/html"}, [[ + <!DOCTYPE html> + <html> + <head> + <title>A simple example of website made for luna</title> + <link rel="stylesheet" href="/static/style.css"> + </head> + <body> + <main class="container"> + <h1>Wow! A simple website made for luna?</h1> + <nav> + <div class="menu-link"> + <a href="/json">JSON example</a> — print basic request data + in JSON format + </div> + <div class="menu-link"> + <a href="/db">DB example</a> — write a hit into the database + and return overall number of hits + </div> + <div class="menu-link"> + <a href="/request">HTTP request</a> — send an HTTP-request + to ifconfig.me and return the result + </div> + </nav> + </main> + </body> + </html>]] +end) + +-- register a GET /json route and return some request data in response +luna.router.route("GET /json", function (request) + local response = "{\"method\": \"" .. request.method .. "\"" + response = response .. ", \"path\": \"" .. request.path .. "\"}" + + return 200, {["Content-Type"] = "application/json"}, response +end) + +-- register a GET /db route. visiting it writes a hit to the database and +-- displays visits count +luna.router.route("GET /db", function (request) + local ok, err = luna.db.exec( + db, + "INSERT INTO hits VALUES (?)", + {os.date("%Y-%m-%d %H:%M:%S")} + ) + if not ok then + return 500, + {["Content-Type"] = "application/json"}, + "error while saving to db: " .. err + end + local ok, res = luna.db.query(db, "SELECT COUNT(*) FROM hits", {}) + if not ok then + return 500, + {["Content-Type"] = "application/json"}, + "error while querying db: " .. res + end + return 200, {["Content-Type"] = "text/html"}, "Hits: " .. tostring(res[1][1]) +end) + +-- register a GET /request route. when visited makes an HTTP-request to +-- https://ifconfig.me service and displays the result +luna.router.route("GET /request", function () + local ok, response = luna.http.request("GET", "https://ifconfig.me", {}, "") + if not ok then + return 500, + {["Content-Type"] = "application/json"}, + "error while fetching data from ifconfig.me: " .. response + end + return 200, {}, "Your IP is: " .. response.body +end) + +-- serve static files from static/ directory +luna.router.static("GET /static/", "static/") + +-- register a custom eval handler that will evaluate whatever is passed into +-- luna stdio when it's run with -D flag. in this case we print the result of +-- evaluation. eval handler is evaluated once for each worker so the result +-- may be printed more than once +luna.evalfn(function (text) + local res = loadstring(text) + print(res) +end) diff --git a/example/run.sh b/example/run.sh new file mode 100755 index 0000000..1ccff3a --- /dev/null +++ b/example/run.sh @@ -0,0 +1,23 @@ +#!/bin/sh +set -e + +usage () { + echo "Usage: + serve [--jit] Serve the example" +} + +serve () { + variant="$1" + if [ "$variant" = "--jit" ]; then + echo "running jit" + go run -tags jit ../. -n 1 -D main.lua + else + echo "running puc" + go run -tags puc ../. -n 1 -D main.lua + fi +} + +cmd="$1" +[ -z "$cmd" ] || [ "$cmd" = "-h" ] || [ "$cmd" = "--help" ] && usage && exit 1 +shift +"$cmd" "$@" diff --git a/example/static/style.css b/example/static/style.css new file mode 100644 index 0000000..5b6ceca --- /dev/null +++ b/example/static/style.css @@ -0,0 +1,17 @@ +body { + margin: 2rem; +} + +h1 { + margin-top: 0; +} + +.container { + border: 2px dashed currentColor; + padding: 2rem; + max-width: 50rem; +} + +.menu-link { + margin-bottom: 0.5rem; +} |
