-- Example HTTP server with string-based routing, parameters, and wildcards print("Starting Moonshark HTTP server with string routing...") -- Start HTTP server http.listen(3000) -- Home page http.route("GET", "/", function(req) local visits = session.get("visits") or 0 visits = visits + 1 session.set("visits", visits) return http.html([[
You've visited this page ]] .. visits .. [[ times.
]]) end) -- User profile with dynamic parameter http.route("GET", "/users/:id", function(req) local userId = req.params.id return http.html([[User ID: ]] .. userId .. [[
]]) end) -- User posts with multiple parameters http.route("GET", "/users/:id/posts", function(req) local userId = req.params.id return http.json({ user_id = userId, posts = { {id = 1, title = "First Post", content = "Hello world!"}, {id = 2, title = "Second Post", content = "Learning Lua routing!"} } }) end) -- Blog post with slug parameter http.route("GET", "/blog/:slug", function(req) local slug = req.params.slug return http.html([[This is the content for blog post "]] .. slug .. [["
]]) end) -- Blog comments http.route("GET", "/blog/:slug/comments", function(req) local slug = req.params.slug return http.json({ blog_slug = slug, comments = { {author = "Alice", comment = "Great post!"}, {author = "Bob", comment = "Very informative."} } }) end) -- Wildcard route for file serving http.route("GET", "/files/*path", function(req) local filePath = req.params.path return http.html([[Requested file: ]] .. filePath .. [[
In a real application, this would serve the file content.
]]) end) -- API endpoints with parameters http.route("GET", "/api/users/:id", function(req) local userId = req.params.id return http.json({ id = tonumber(userId), name = "User " .. userId, email = "user" .. userId .. "@example.com", active = true }) end) http.route("PUT", "/api/users/:id", function(req) local userId = req.params.id local userData = req.form return http.json({ success = true, message = "User " .. userId .. " updated", data = userData }) end) http.route("DELETE", "/api/users/:id", function(req) local userId = req.params.id return http.json({ success = true, message = "User " .. userId .. " deleted" }) end) -- Login form with CSRF protection http.route("GET", "/login", function(req) return http.html([[" .. success .. "
") or "") .. [[ ]] .. (error and ("" .. error .. "
") or "") .. [[Welcome, ]] .. user .. [[!
]]) end) -- Logout http.route("GET", "/logout", function(req) session.set("user", nil) session.flash("info", "You have been logged out") return http.redirect("/") end) -- Catch-all route for 404s (must be last) http.route("GET", "*path", function(req) http.status(404) return http.html([[The requested path "]] .. req.params.path .. [[" was not found.
]]) end) print("Server configured with string routing. Listening on http://localhost:3000") print("Try these routes:") print(" GET /") print(" GET /users/123") print(" GET /users/456/posts") print(" GET /blog/my-first-post") print(" GET /blog/lua-tutorial/comments") print(" GET /files/docs/readme.txt") print(" GET /api/users/789") print(" GET /nonexistent (404 handler)")