-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (45 loc) · 1.57 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 allnc.app import AllNc
from allnc.middleware import Middleware
app = AllNc()
@app.route("/home", allowed_methods=["get"])
def home(request, response):
response.text = "Hello from the Home Page"
@app.route("/about", allowed_methods=["put"])
def about(request, response):
response.text = "Hello from the About Page"
@app.route("/hello/{name}")
def greeting(request, response, name):
response.text = f"Hello {name}"
@app.route("/books")
class Books:
def get(self, request, response):
response.text = "Hello from the Books Page"
def post(self, request, response):
response.text = "Endpoint to create a book"
def new_handler(req, resp):
resp.text = "From new Handler"
app.add_route("/new-handler", new_handler)
@app.route("/template")
def template_handler(req, resp):
resp.html = app.template(
"home.html",
context={"new_title": "Best title", "new_body": "Best body"},
)
@app.route("/json")
def json_handler(req, resp):
response_data = {"name": "some name", "type": "json"}
resp.json = response_data
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 __init__(self, app):
super().__init__(app)
def process_request(self, req):
print("request is been called")
def process_response(self, req, resp):
print("response has been generated")
app.add_middleware(LoggingMiddleware)