import os from app import app from handler import * from chocs import serve if __name__ == "__main__": print("Running server on port 8080") serve(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)), debug=True)
from chocs import Application, HttpRequest, HttpResponse, HttpStatus, serve app = Application() @app.get("/hello/{name}") def hello(request: HttpRequest) -> HttpResponse: return HttpResponse(body=f"Hello {request.attributes['name']}!") @app.get("*") def default(request: HttpRequest) -> HttpResponse: return HttpResponse("Not found", HttpStatus.NOT_FOUND) serve(app)
from datetime import datetime from chocs import Application, HttpCookie, HttpRequest, HttpResponse, HttpStatus, serve app = Application() @app.get("/") def default(request: HttpRequest) -> HttpResponse: """Shows how cookie work""" client_cookies = request.cookies response = HttpResponse("Not found", HttpStatus.NOT_FOUND) response.cookies["simple_cookie"] = "value" response.cookies.append( HttpCookie( "advanced_cookie", "Advanced value {test 1}", expires=datetime(2021, 1, 1) ) ) return response serve(app, host="127.0.0.1", port=8080)
from chocs import Application, HttpRequest, HttpResponse, HttpStatus, serve from chocs.wsgi import WsgiServers app = Application() @app.get("/hello/{name}") def hello(request: HttpRequest) -> HttpResponse: return HttpResponse(body=f"Hello {request.path_parameters['name']}!") @app.get("*") def default(request: HttpRequest) -> HttpResponse: return HttpResponse("Not found", HttpStatus.NOT_FOUND) serve(app, port=8080, debug=True, wsgi_server=WsgiServers.BJOERN)
from examples.loading_modules.app import app from chocs import serve app.use("examples.loading_modules.routes") app.use("examples.loading_modules.*.routes") serve(app, port=8080, debug=True)
import time from chocs import HttpRequest from chocs import HttpResponse from chocs import serve from chocs.middleware import MiddlewareHandler @router.get("/hello/{name}") def hello(request: HttpRequest) -> HttpResponse: return HttpResponse(body=f"Hello {request.attributes['name']}!") def time_load_middleware(request: HttpRequest, next: MiddlewareHandler) -> HttpResponse: started_at = time.time() response = next(request) total_time = (time.time() - started_at) * 100 response.write(f"\nTotal time taken for generating response: {total_time}") return response serve(time_load_middleware, router)
from chocs import HttpRequest from chocs import HttpStatus from chocs import HttpResponse from chocs import serve from chocs import router @router.get("/hello/{name}", name=r"\w+") def hello(request: HttpRequest) -> HttpResponse: return HttpResponse(body=f"Hello {request.attributes['name']}!") @router.get("*") def default(request: HttpRequest) -> HttpResponse: return HttpResponse(HttpStatus.NOT_FOUND, "Not found") serve()