spiderweb/example.py

46 lines
1004 B
Python
Raw Normal View History

2024-08-06 01:10:00 -04:00
from spiderweb import WebServer
from spiderweb.response import HttpResponse, JsonResponse, TemplateResponse, RedirectResponse
2024-08-09 12:02:46 -04:00
app = WebServer(
templates_dirs=["templates"],
middleware=[
"example_middleware.TestMiddleware",
"example_middleware.RedirectMiddleware"
],
append_slash=False # default
)
2024-08-06 01:10:00 -04:00
@app.route("/")
def index(request):
return TemplateResponse(request, "test.html", context={"value": "TEST!"})
@app.route("/redirect")
def redirect(request):
return RedirectResponse("/")
2024-08-09 12:02:46 -04:00
@app.route("/json")
def json(request):
return JsonResponse(data={"key": "value"})
@app.route("/error")
def error(request):
return HttpResponse(status_code=500, body="Internal Server Error")
@app.route("/middleware")
def middleware(request):
return HttpResponse(
body="We'll never hit this because it's redirected in middleware"
)
2024-08-06 01:10:00 -04:00
if __name__ == "__main__":
2024-08-09 12:02:46 -04:00
# can also add routes like this:
2024-08-06 01:10:00 -04:00
# app.add_route("/", index)
2024-08-09 12:02:46 -04:00
app.start()