예제 #1
0
파일: api.py 프로젝트: rsneh/bocadillo
        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):
예제 #2
0
import bocadillo

api = bocadillo.API()


@api.route("/")
async def index(req, res):
    res.text = "Hello, world!"


if __name__ == "__main__":
    api.run()
예제 #3
0
파일: app.py 프로젝트: siloam/asgi-examples
import bocadillo

app = bocadillo.API()


@app.route("/")
async def index(req, res):
    res.text = "Hello, world!"