Skip to content

Commit 1ef2ff2

Browse files
committed
docs: add info about static serving and add blog example
1 parent c4ad43b commit 1ef2ff2

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

docs/_layouts/default.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ <h3>Features & docs</h3>
5555
<li><a href="/scheduler">Scheduler</a></li>
5656
<li><a href="/security">Security</a></li>
5757
</ul>
58+
<ul>
59+
<li><a href="/static">Static files</a></li>
60+
</ul>
5861
<h3>API</h3>
5962
<ul>
6063
<li><a href="/api/lambda_api">Lambda</a></li>

docs/examples/blog.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Example
2+
3+
This is basic public comment board example:
4+
5+
* anyone can post comment with any name
6+
* only last N post will be displayed
7+
* comments stored in SQLite
8+
9+
**Do not use the example in production!** Comments board without authorization it's a bad, very bad idea.
10+
However, the example should be safe for XSS attacks due to user data escaping.
11+
12+
* Create new project based on python template (UI-> Dashboard -> Python)
13+
* Click files, then click on app.py file. Copy following content:
14+
15+
```python
16+
import os
17+
import sqlite3
18+
import html
19+
20+
# how many last comments to show
21+
last_num = 20
22+
# db location
23+
db_name = 'comments.db'
24+
# define out database schema
25+
schema = '''
26+
CREATE TABLE IF NOT EXISTS comment(
27+
id INT PRIMARY KEY,
28+
name VARCHAR(100) NOT NULL,
29+
comment VARCHAR(255) NOT NULL
30+
)
31+
'''
32+
# html template
33+
html_template = '''<html><body>
34+
<div>
35+
<form method="post" enctype="application/x-www-form-urlencoded">
36+
<input type="text" placeholder="your name" name="name" maxlength="100"/><br/>
37+
<textarea placeholder="comment" maxlength="255" name="comment"></textarea><br/>
38+
<button type="submit">send</button>
39+
</form>
40+
</div>
41+
{comments}
42+
</body></html>'''
43+
44+
# form values
45+
name = os.getenv('NAME')
46+
comment = os.getenv('COMMENT')
47+
48+
# connect to local database
49+
with sqlite3.connect(db_name) as conn:
50+
cur = conn.cursor()
51+
cur.execute(schema)
52+
cur.execute('INSERT INTO comment (name, comment) VALUES (?, ?)', (name, comment))
53+
conn.commit()
54+
latest = cur.execute(f'SELECT id, name, comment FROM comment ORDER BY id DESC LIMIT {last_num}')
55+
# re-render main page comment block
56+
blocks = []
57+
for (id, name, comment) in latest:
58+
blocks.append(f'<div id="{id}"><p>From: <b>{html.escape(name)}</b></p><p>{html.escape(comment)}</p></div><hr/>')
59+
page = html_template.replace('{comments}', "\n".join(blocks))
60+
with open('static/index.html', 'wt') as f:
61+
f.write(page)
62+
```
63+
64+
* In UI create directory `static`, click on it, create file `index.html` and put following content:
65+
66+
```html
67+
<html><body>
68+
<div>
69+
<form method="post" enctype="application/x-www-form-urlencoded">
70+
<input type="text" placeholder="your name" name="name" maxlength="100"/><br/>
71+
<textarea placeholder="comment" maxlength="255" name="comment"></textarea><br/>
72+
<button type="submit">send</button>
73+
</form>
74+
</div>
75+
</body></html>
76+
```
77+
78+
* Setup mapping for form values: click on Mapping tab and add:
79+
* output headers: `Content-Type: text/html`
80+
* query params should be mapped as: comment to `COMMENT` and name to `NAME`
81+
* set static dir to `static`
82+
* click 'save'
83+
84+
![image](https://user-images.githubusercontent.com/6597086/83414645-6e1db100-a450-11ea-9ded-2e5d93ac00f7.png)
85+
86+
87+
Done!
88+
89+
Now open URL from overview section by browser and try to post something.
90+

docs/manifest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Example: for python with virtualenv with main script `app.py` it will look like:
4444
* **public** (optional, boolean): if false, check all requests against `tokens`
4545
* **aliases** (optional, array of string): aliases/links for the lambda, useful to make permanent URL, [see aliases doc](aliases.md)
4646
* **cron** (option, array of `Cron`): scheduled actions
47+
* **static** (optional, string): path to directory inside lambda to serve static files; if defined the GET and HEAD methods will not be available for handler
4748

4849
### Cron
4950

docs/static.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Static files
2+
3+
Static files can be served by `GET` request from the specified folder.
4+
Using static serving together with lambda allows you to create dynamic service with UI (like blog or comments).
5+
6+
To enable the feature, set in [manifest](manifest.md) field `static` to relative path to the directory with static files
7+
(must be subfolder of lambda directory).
8+
9+
If the feature enabled the GET and HEAD methods will not be available for handler (lambda).
10+
Same security restrictions applied to static files as to lambda (security checks performed before file handling).
11+
12+
UI:
13+
14+
1. Click to already create app
15+
2. Select mapping, enter static directory name, relative to the application root, or clean ir to remove
16+
3. Click save
17+
18+
Related examples:
19+
20+
* [blog](examples/blog.md)

0 commit comments

Comments
 (0)