-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (46 loc) · 1.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from pylord.app import PyLordApp
from pylord.middleware import Middleware
app = PyLordApp()
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "hi this is home page"
@app.route("/about")
def about(request, response):
response.text = 'hi this is about page'
@app.route("/hello/{name}")
def generating(request, response, name):
response.text = f"hello {name}"
@app.route("/book")
class Books:
def get(self, request, response):
response.text = "this is get method"
def post(self, request, response):
response.text = "endpoint to create a book"
def new_handler(req, resp):
resp.text = "New Handler"
app.add_route("/new_handler", new_handler)
@app.route("/template")
def template_handler(req, resp):
resp.body = app.template(
"home.html",
context={
"new_title": "Best_title213",
"new_body": "Best_body123"
}
)
def on_exception(req, resp, exc):
resp.text = str(exc)
app.add_exception_handler(on_exception)
@app.route("/exception")
def exception_throwing_handler(req, resp):
raise AttributeError("some exception")
class LoggingMiddleware(Middleware):
def process_request(self, req):
print("request is being called", req.url)
def process_response(self, req, resp):
print("response has been generated", req.url)
app.add_middleware(LoggingMiddleware)
@app.route("/json")
def json_handler(req, resp):
response_data = {"name": "asasa"}
resp.json = response_data