Nawa is a simple Python web framework that designed for our ecological scripting life. Nawa runs application fast, using a trick that apache returns cache file to client directly. So if cache exists, script not awakes, apache returns response faster than FCGI environment.
Quickly:
1. Install from PyPI.
easy_install Nawa
On any places,
2. Copy template files.
nawa_admin.py start_project
3. Generate nawa environment.
nawa_admin.py generate
4. Complete.
Application that was generated will work!
demo /test/foo/1/1/
demo /test/nawa_cache_clean_ep.py
Edit config:
vi nawa_config.yaml
define API:
API
API has a type, "nokey" or "cacheable" or "uncacheable".
API "nokey" has no keys,
APIs:
foo:"cacheable" and "uncacheable" has some keys.
Type of first key of "cacheable" is int(not minus),
APIs:
bar:
keys:
- name: key1
type:
name: int
min: 0that of "uncacheable" isn't int(not minus).
APIs:
baz:
keys:
- name: key1
type:
name: stringkey
key has a type, "int" or "string" or "regexp".
We can set max and min to int key,
APIs:
bar:
keys:
- name: key1
type:
name: int
min: 0
max: 10max and min to string key,
APIs:
baz:
keys:
- name: key1
type:
name: string
min: 5
max: 16and pattern to regexp key.
APIs:
boo:
keys:
- name: key1
type:
name: regexp
pattern: ^hogehoge$
Edit generated files:
nawaapi.py
For example,
APIs:
bar:
keys:
- name: key1
type:
name: int
min: 1if config defines API so, Nawa generates this file like this:
...
class api_bar(core.api_cacheable):
"""API bar,
- is cacheable.
- has 1 keys.
- key1(int)
min: 1
"""
name = 'bar'
def GET(self):
key1 = self.key.values[0]
content = 'api_path: %s<br>' % cgi.escape(core.get_url(self.name), True)
content += 'api: %s<br>' % cgi.escape(self.name, True)
content += 'key(raw): %s<br>' % cgi.escape(str(self.key.raws), True)
content += 'key(validated): %s<br>' % cgi.escape(str(self.key.values), True)
content += 'generated: %s<br>' % datetime.datetime.now().strftime("%y/%m/%d %H:%M:%S")
self.response(content)
...Class receives validated values of keys, generates html contains received values and finally, class responses html.
And this API is "nokey" / "cacheable", so response will be cached automatically. On next request, this script is not called, apache returns cache to client.
Edit this file, and make your application.
nawaapi_ep.py
Entry point of application.
If you use virtual-python, rewrite shebang:
#!/virtual/foouser/bin/python
.htaccess
Most important component of Nawa.
Be careful, if you need to edit this file.
Trick for direct response.
1. normalize path
RewriteRule ^(bar)/([^/&])/(.*)$ $1/00$2/$3 [N] RewriteRule ^(bar)/([^/&][^/&])/(.*)$ $1/0$2/$3 [N]
2. rewrite path to cache path
RewriteRule ^bar/([^/&]*([^/&])([^/&])([^/&]))/$ cache/$2/$3/$4/bar&$1
3. set mimetype to cache
RewriteRule ^cache - "[T=text/html;charset=utf-8]"
4. call script, if cache not exists
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-s
RewriteRule ^cache/[^/&]/[^/&]/[^/&]/bar&([^/&]+)$ nawaapi_ep.py?apiname=bar&key1=$1 [L]5. return cache, if exists
Rapid development:
Use local server.
nawa_cgi_serv.py
This script is very coarse, but works as a simple http-server that can emulate section of path rewrite of .htaccess.
Run this script, open "http://localhost:8086/" with your browser, and test your application behavior.