-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·129 lines (91 loc) · 3.43 KB
/
server.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import http_plus_purplelemons_dev as http_plus
from time import sleep
from datetime import datetime as dt
server = http_plus.Server(brython=True, debug=True)
auth = http_plus.Auth()
@server.log
def logger(r: http_plus.Handler):
print(
f"[{dt.now().strftime('%H:%M:%S')}] ({r.ip}) \"{r.method} {r.status} {r.path}\" {r.protocol_version}"
)
@server.get("/init")
def _(req: http_plus.Request, res: http_plus.Response):
print(f"{req.body=}")
return res.set_body("Hello, world!")
@server.get("/test")
def _(req: http_plus.Request, res: http_plus.Response):
return res.set_body("Hello, test!")
@server.get("/kwtest/:testnum")
def _(req: http_plus.Request, res: http_plus.Response):
# Both below work, except in the case of using Python syntax.
testnum = req.params.testnum
testnum = req.param("testnum")
return res.set_body(f"Hello, {testnum=}!")
@server.get("/kwtest/*/test")
def _(req: http_plus.Request, res: http_plus.Response):
return res.set_body("Hello, .../*/test!")
@server.post("/posty")
def _(req: http_plus.Request, res: http_plus.Response):
res.set_header("Content-Type", "text/html")
return res.set_body("\n".join(f"{k}: {v}" for k, v in req.json.items()))
@server.all("/all")
def _(req: http_plus.Request, res: http_plus.Response):
return res.set_body("Hello, all methods!")
@server.get("/error_test")
def _(req: http_plus.Request, res: http_plus.Response):
raise Exception(
"This is an error test, completely normal stuff dont worry lol why are you worrying i said stop worrying stop crying why are you crying this is normal."
)
@server.get("/google")
def _(req: http_plus.Request, res: http_plus.Response):
return res.redirect("https://google.com")
@server.stream("/stream_test")
def _(req: http_plus.Request, res: http_plus.StreamResponse):
for i in range(10):
yield res.event(str(i), "message")
sleep(1)
@server.get("/auth_test")
def _(req: http_plus.Request, res: http_plus.Response):
if req.authorization is not None:
scheme, token = req.authorization
user = auth[token]
return res.set_body(
f"Hello, authenticated user!\nName: {user.username}\nID: {user.id}"
)
return res.set_body("You are not authenticated.")
@server.get("/get_authed")
def _(req: http_plus.Request, res: http_plus.Response):
token = auth.generate(username="test", id=1234)
return res.set_body({"token": token})
@server.get("/send_file")
def _(req: http_plus.Request, res: http_plus.Response):
return res.send_file("test.txt")
@server.get("/prompt_download")
def _(req: http_plus.Request, res: http_plus.Response):
return res.prompt_download("test.txt")
@server.get("/error_test")
def _(req: http_plus.Request, res: http_plus.Response):
0 / 0
return res.set_body("Hello, error_test!")
products = {
"shirts": [
{"name": "Shirt 1", "price": 10, "product_id": "shirt1"},
{"name": "Shirt 2", "price": 20, "product_id": "shirt2"},
],
"pants": [{"name": "Pants 1", "price": 30, "product_id": "pants1"}],
}
@server.gql(
schema="""
type Query {
shirts: [Product]
pants: [Product]
}
type Product {
name: String
price: Int
}
"""
)
def _(req: http_plus.Request, res: http_plus.GQLResponse):
return res.set_database(products)
server.listen(8000)