Files
wren/example/web_demo.wren
T
retoor 5a4054ec66 feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 22:40:22 +00:00

59 lines
1.3 KiB
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "web" for Application, Response, View
import "json" for Json
class HelloView is View {
get(request) {
return Response.text("Hello, World!")
}
}
class ApiView is View {
get(request) {
var data = {"message": "API response", "method": "GET"}
return Response.json(data)
}
post(request) {
var body = request.json
var data = {"message": "Data received", "received": body}
return Response.json(data)
}
}
class UserView is View {
get(request) {
var userId = request.params["id"]
return Response.json({"user_id": userId, "action": "get"})
}
}
var app = Application.new()
app.get("/", Fn.new { |req|
return Response.html("<h1>Welcome to Wren Web!</h1><p>A simple web framework.</p>")
})
app.addView("/hello", HelloView)
app.addView("/api", ApiView)
app.addView("/users/:id", UserView)
app.get("/greet/:name", Fn.new { |req|
var name = req.params["name"]
return Response.text("Hello, " + name + "!")
})
app.get("/session", Fn.new { |req|
var count = req.session["count"]
if (count == null) count = 0
count = count + 1
req.session["count"] = count
return Response.json({"visit_count": count})
})
app.static_("/static", "./static")
System.print("Starting server...")
app.run("0.0.0.0", 8080)