forked from sparckles/Robyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_routes.py
215 lines (144 loc) · 4.31 KB
/
base_routes.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from robyn import Robyn, static_file, jsonify, WS
from robyn.log_colors import Colors
import asyncio
import os
import pathlib
import logging
app = Robyn(__file__)
websocket = WS(app, "/web_socket")
i = -1
logger = logging.getLogger(__name__)
@websocket.on("message")
async def connect(websocket_id):
print(websocket_id)
global i
i += 1
if i == 0:
return "Whaaat??"
elif i == 1:
return "Whooo??"
elif i == 2:
i = -1
return "*chika* *chika* Slim Shady."
@websocket.on("close")
def close():
return "GoodBye world, from ws"
@websocket.on("connect")
def message():
return "Hello world, from ws"
callCount = 0
@app.get("/")
async def hello(request):
global callCount
callCount += 1
message = "Called " + str(callCount) + " times"
print(message, request)
return {"status_code": "200", "body": "hello", "type": "text"}
@app.get("/const_request", const=True)
async def const_request():
return "Hello world"
@app.get("/const_request_json", const=True)
async def const_request_json():
return jsonify({"hello": "world"})
@app.get('/404')
def return_404():
return {"status_code": "404", "body": "hello", "type": "text"}
@app.post('/404')
def return_404_post():
return {"status_code": "404", "body": "hello", "type": "text"}
@app.get('/int_status_code')
def return_int_status_code():
return {"status_code": 202, "body": "hello", "type": "text"}
@app.before_request("/post_with_body")
async def hello_before_request(request,response):
global callCount
callCount += 1
print("this is before request")
print(request)
print(response)
return ""
@app.after_request("/post_with_body")
async def hello_after_request(request, response):
global callCount
callCount += 1
print("this is after request")
print(request)
print(response)
return ""
@app.get("/test/:id")
async def test(request):
print(request)
current_file_path = pathlib.Path(__file__).parent.resolve()
html_file = os.path.join(current_file_path, "index.html")
return static_file(html_file)
@app.get("/jsonify")
async def json_get():
return jsonify({"hello": "world"})
@app.get("/query")
async def query_get(request):
query_data = request["queries"]
return jsonify(query_data)
@app.post("/jsonify/:id")
async def json(request):
return jsonify({"hello": "world"})
@app.post("/post")
async def post():
return "POST Request"
@app.post("/post_with_body")
async def postreq_with_body(request):
print("This is the main function")
print(request)
return bytearray(request["body"]).decode("utf-8")
@app.put("/put")
async def put(request):
return "PUT Request"
@app.put("/put_with_body")
async def putreq_with_body(request):
print(request)
return bytearray(request["body"]).decode("utf-8")
@app.post("/headers")
async def postreq_with_headers(request):
logger.info(f"{Colors.OKGREEN} {request['headers']} \n{Colors.ENDC}")
return jsonify(request["headers"])
@app.delete("/delete")
async def delete():
return "DELETE Request"
@app.delete("/delete_with_body")
async def deletereq_with_body(request):
return bytearray(request["body"]).decode("utf-8")
@app.patch("/patch")
async def patch():
return "PATCH Request"
@app.patch("/patch_with_body")
async def patchreq_with_body(request):
return bytearray(request["body"]).decode("utf-8")
@app.get("/sleep")
async def sleeper():
await asyncio.sleep(5)
return "sleep function"
@app.get("/blocker")
def blocker():
import time
time.sleep(10)
return "blocker function"
async def startup_handler():
print("Starting up")
@app.shutdown_handler
def shutdown_handler():
print("Shutting down")
@app.get("/redirect")
async def redirect(request):
return {"status_code": "307", "body": "", "type": "text", "headers": jsonify({"Location": "redirect_route"})}
@app.get("/redirect_route")
async def redirect_route(request):
return "This is the redirected route"
if __name__ == "__main__":
app.add_header("server", "robyn")
current_file_path = pathlib.Path(__file__).parent.resolve()
app.add_directory(
route="/test_dir",
directory_path=os.path.join(current_file_path, "build"),
index_file="index.html",
)
app.startup_handler(startup_handler)
app.start(port=5000)