Skip to content

Commit fcf7a2d

Browse files
author
Waffles32
committed
Update README.md
1 parent d9b2ac5 commit fcf7a2d

File tree

1 file changed

+161
-1
lines changed

1 file changed

+161
-1
lines changed

README.md

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,161 @@
1-
# madness
1+
# madness
2+
3+
4+
use methods for your madness
5+
6+
## Application
7+
8+
```python
9+
from madness import Madness
10+
11+
app = Madness()
12+
13+
# add routes/context here
14+
15+
if __name__ == '__main__':
16+
app.run()
17+
```
18+
19+
20+
## Routing
21+
22+
### Decorators
23+
24+
`@route`
25+
26+
option | description
27+
------------ | -------------
28+
`*paths` | relative paths
29+
`methods` | list of allowed http methods
30+
`context` | list of extra context functions
31+
32+
#### Single-Method
33+
34+
`@get, @post, @put, @delete, @patch, @options`
35+
36+
#### RESTful
37+
38+
decorator | path | methods
39+
------------ | ------------- | -------------
40+
`@index` | {path} | GET
41+
`@new` | new{path} | GET
42+
`@create` | | POST
43+
`@show` | /<int:id>{path} | GET
44+
`@edit` | /<int:id>/edit{path} | GET
45+
`@update` | /<int:id>{path} | PUT
46+
`@destroy` | /<int:id>{path} | DELETE
47+
48+
#### AWS
49+
50+
`@lambda_handler`
51+
52+
[usage](https://github.com/Waffles32/madness/blob/development/examples/api-gateway.py)
53+
54+
#### CORS
55+
56+
if 'OPTIONS' is present in methods the following options are used
57+
58+
option | description
59+
------------ | -------------
60+
`origin` | allowed origin: \* or list of urls
61+
`headers` | allowed request headers: list of header names
62+
63+
64+
### Modules
65+
66+
```python
67+
68+
app = Madness()
69+
70+
module = Madness()
71+
72+
@module.route
73+
def thing():
74+
return response(['hello!'])
75+
76+
app.extend(module) # now app has /thing
77+
78+
app.extend(module, 'prefix') # now app has /prefix/thing
79+
80+
app.extend(module, context:bool=True) # choose if module inherits entire app.context
81+
82+
```
83+
84+
85+
## Context
86+
87+
rules are added to context e.g. /path/<myvar> creates context.myvar
88+
89+
90+
```python
91+
92+
93+
from madness import context, request, json
94+
95+
@app.context
96+
def before_request():
97+
# add a variable to the context
98+
context.x = 1
99+
100+
@app.context
101+
def uses_x(x):
102+
print('a previous context defined', x)
103+
104+
105+
@app.context
106+
def around_request():
107+
# before_request
108+
try:
109+
yield
110+
finally:
111+
# after_request
112+
pass
113+
114+
115+
@app.context
116+
def handle_exception():
117+
try:
118+
response = yield
119+
except MyException as exception:
120+
# MyException error occured while generating the response
121+
122+
# we can do one of 3 things with it:
123+
124+
# ignore the exception, continue processing the request
125+
pass
126+
127+
# re-raise the exception to the parent context
128+
raise
129+
130+
# convert the exception to a response
131+
yield json.response({'foo': exception.bar})
132+
133+
134+
@app.context
135+
def filter_request():
136+
# abort the request before the route, route is never called
137+
if request.headers['x-header'] != 'some-value':
138+
yield json.response('aborted')
139+
140+
141+
@app.context
142+
def jwt():
143+
encoded = request.headers['x-jwt']
144+
data = jwt.decode(encoded)
145+
context.username = data['username']
146+
try:
147+
yield
148+
else:
149+
response.headers['x-jwt'] = jwt.encode({
150+
'username': context.username
151+
})
152+
153+
154+
@app.route
155+
def test(x, username):
156+
print('x is', 1)
157+
print('jwt is', username)
158+
return response(['body'])
159+
160+
161+
```

0 commit comments

Comments
 (0)