HTTP
Using a Reboot Application
you can not only run your Reboot
servicers but you can also provide standard HTTP route handlers
which do not involve gRPC. In Python, this is implemented using the FastAPI
framework. In Nodejs, this is
implemented with Express.js.
The /
route is currently used by Reboot itself to
show a helpful page explaining that this is a Reboot
application. If you have a use case where you'd like
to override /
please let us know!
Currently only GET
and POST
methods are supported, but we plan to add more
support as necessary, please reach out to us at team@reboot.dev if you have this
use case!
Here is an example of implementing a handler for an HTTP GET
:
- Python
- TypeScript
@application.http.get("/hello_world")
def hello_world():
return {"message": "Hello, world!"}
const application = new Application({ servicers: [GreeterServicer] });
application.http.get("/hello_world", async (context, req, res) => {
res.json({ message: `Hello, world!` });
});
To simplify calling directly into your Reboot application from your
application.http.get
and application.http.post
handlers we provide a
context
of
type
ExternalContext
because you are still "external" to your application from
an authorization perspective.
This context
includes any bearer token from
Authorization: Bearer <token>
so that any calls into your Reboot application
will be properly authorized. In TypeScript we inject the context for every
handler, but for Python we follow the FastAPI dependency injection strategy and,
if you want the context
, you must explicitly add it to your arguments as shown in
the following example:
- Python
- TypeScript
@application.http.post("/hello_greeter")
async def hello_greeter(
request: Request,
context: ExternalContext = InjectExternalContext,
):
body = await request.json()
greeter, _ = await Greeter.Create(
context,
title=body['title'],
name=body['name'],
adjective=body['adjective'],
)
response = await greeter.Greet(context, name="You")
return {"message": response.message}
const application = new Application({ servicers: [GreeterServicer] });
application.http.post("/hello_greeter", async (context, req, res) => {
const { title, name, adjective } = req.body as {
title: string;
name: string;
adjective: string;
};
const [greeter] = await Greeter.create(context, {
title,
name,
adjective,
});
const { message } = await greeter.greet(context, { name: "You" });
res.json({ message });
});