raise HTTPError(400) # NOTE: you're free to integrate a third-party # validation library such as Marshmallow or jsonschema. for field in "name", "teacher": if field not in data: print(field, "is a required field") raise HTTPError(400) # Now, let's assemble the actual application, shall we? api = bocadillo.API( # Built-in CORS, HSTS and GZip! enable_cors=True, enable_hsts=False, # the default enable_gzip=True, gzip_min_size=1024, # the default ) # Register the token middleware. api.add_middleware(TokenMiddleware) # Instanciate helper backends. storage = Storage() analytics = Analytics() # Routes! Views! Jinja templates! Static files! @api.route("/") async def index(req, res):
import bocadillo api = bocadillo.API() @api.route("/") async def index(req, res): res.text = "Hello, world!" if __name__ == "__main__": api.run()
import bocadillo app = bocadillo.API() @app.route("/") async def index(req, res): res.text = "Hello, world!"