cosmopolitan/tool/net/demo/store-asset.lua
Justine Tunney 80b211e314 Add raw memory visualization tool to redbean
This change introduces a `-W /dev/pts/1` flag to redbean. What it does
is use the mincore() system call to create a dual-screen terminal
display that lets you troubleshoot the virtual address space. This is
useful since page faults are an important thing to consider when using a
forking web server. Now we have a colorful visualization of which pages
are going to fault and which ones are resident in memory.

The memory monitor, if enabled, spawns as a thread that just outputs
ANSI codes to the second terminal in a loop. In order to make this
happen using the new clone() polyfill, stdio is now thread safe.

This change also introduces some new demo pages to redbean. It also
polishes the demos we already have, to look a bit nicer and more
presentable for the upcoming release, with better explanations too.
2022-05-14 04:33:58 -07:00

80 lines
2.2 KiB
Lua

-- StoreAsset Demo
--
-- Redbean is able to store files into its own executable structure.
-- This is currently only supported on a Linux, Apple, and FreeBSD.
--
-- By loading this page, your redbean will insert an immediate file into
-- your redbean named `/hi`, which you can click the back button in the
-- browser and view it on the listing page right afterwards!
Write[[<!doctype html>
<title>redbean store asset demo</title>
<style>
body { padding: 1em; }
h1 a { color: inherit; text-decoration: none; }
h1 img { border: none; vertical-align: middle; }
input { margin: 1em; padding: .5em; }
p { word-break: break-word; max-width: 650px; }
dt { font-weight: bold; }
dd { margin-top: 1em; margin-bottom: 1em; }
.hdr { text-indent: -1em; padding-left: 1em; }
</style>
<h1>
<a href="/"><img src="/redbean.png"></a>
<a href="store-asset.lua">store asset demo</a>
</h1>
]]
if IsPublicIp(GetClientAddr()) then
Write[[
<p>
Bad request.
</p>
<p>
This HTTP endpoint self-modifies the web server. You're
communicating wtith this redbean over a public network.
Therefore redbean won't service this request.
</p>
]]
elseif GetHostOs() == "WINDOWS" or
GetHostOs() == "OPENBSD" or
GetHostOs() == "NETBSD" then
Write[[
<p>
Unsupported
</p>
<p>
Sorry! Redbean's Lua StoreAsset() function is only
supported on Linux, Apple, and FreeBSD right now.
</p>
]]
else
StoreAsset('/hi', [[
StoreAsset() worked!
This file was inserted into your redbean by the Lua StoreAsset() API
which was invoked by you browsing to the /store-asset.lua page.
Enjoy your self-modifying web server!
]])
Write[[
<p>
This Lua script has just stored a new file named <code>/hi</code>
to your redbean zip executable. This was accomplished while the
web server is running. It live updates, so if you click the back
button in your browser, you should see <code>/hi</code> in the
ZIP central directory listing, and you can send an HTTP message
requesting it.
</p>
]]
end
Write[[
<p>
<a href="/">go back</a>
</p>
]]