Skip to content

Commit 0ce8894

Browse files
committed
move html text into a static file
instead of consuming memory with a big chunk of html in our code.
1 parent e7d5fe1 commit 0ce8894

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ install-examples: .lastinstall-examples
3535
done
3636
date > $@
3737

38-
.lastinstall-examples: $(EXOBJS)
38+
.lastinstall-examples: $(EXOBJS) examples/help.html
3939
for src in $?; do \
4040
$(AMPY) put $$src `basename $$src`; \
4141
done

examples/fileops.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,24 @@
1818

1919
app = Noggin()
2020

21-
helptext = '''<html>
22-
23-
<h1>Simple file operations</h1>
24-
25-
<ul>
26-
<li><a href="/disk"><code>GET /disk</code></a> to get filesystem
27-
information</li>
28-
<li><a href="/disk/free"><code>GET /disk/free</code></a> to get information
29-
about available disk space</li>
30-
<li><a href="/file"><code>GET /file</code></a> to list available files</li>
31-
<li><code>GET /file/&lt;path&gt;</code> to get a file (for example,
32-
<a href="/file/boot.py">boot.py</a>)</li>
33-
<li><code>PUT /file/&lt;path&gt;</code> to write a file</li>
34-
<li><code>POST /file/&lt;path&gt;</code> to rename a file (new name is
35-
<code>POST</code> body)</li>
36-
<li><code>DELETE /file/&lt;path&gt;</code> to delete a file</li>
37-
</ul>
38-
</html>'''
21+
22+
def chunked_iter(path):
23+
buf = bytearray(256)
24+
try:
25+
with open(path) as fd:
26+
while True:
27+
nb = fd.readinto(buf)
28+
if not nb:
29+
break
30+
yield buf[:nb]
31+
except OSError:
32+
raise HTTPError(404)
3933

4034

4135
@app.route('/')
4236
def index(req):
43-
return Response(content=helptext, mimetype='text/html')
37+
return Response(content=chunked_iter('help.html'),
38+
mimetype='text/html')
4439

4540

4641
def get_statvfs():

examples/help.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<html>
2+
3+
<h1>Simple file operations</h1>
4+
5+
<ul>
6+
<li><a href="/disk"><code>GET /disk</code></a> to get filesystem
7+
information</li>
8+
<li><a href="/disk/free"><code>GET /disk/free</code></a> to get information
9+
about available disk space</li>
10+
<li><a href="/file"><code>GET /file</code></a> to list available files</li>
11+
<li><code>GET /file/&lt;path&gt;</code> to get a file (for example,
12+
<a href="/file/boot.py">boot.py</a>)</li>
13+
<li><code>PUT /file/&lt;path&gt;</code> to write a file</li>
14+
<li><code>POST /file/&lt;path&gt;</code> to rename a file (new name is
15+
<code>POST</code> body)</li>
16+
<li><code>DELETE /file/&lt;path&gt;</code> to delete a file</li>
17+
</ul>
18+
19+
<h2>Non-file related</h2>
20+
21+
<ul>
22+
<li><a href="/mem/free"><code>GET /mem/free</code></a> for available
23+
memory</li>
24+
<li><a href="/net/eth0"><code>GET /net/eth0</code></a> for information about
25+
the wireless client interface</li>
26+
<li><a href="/net/eth1"><code>GET /net/eth1</code></a> for information about
27+
the wireless ap interface</li>
28+
<li><a href="/reset"><code>GET /reset</code></a> to reset the
29+
board by calling <code>machine.reset</code>
30+
</ul>
31+
32+
<p>For either of the <code>/net/...</code> enpoints above, you can
33+
request a specific key by adding another component to the URL. For
34+
example, <a href="/net/eth0/mac"><code>GET
35+
/net/eth0/mac</code></a>.</p>
36+
</html>
37+

0 commit comments

Comments
 (0)