Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Fastify #474

Merged
merged 1 commit into from
Feb 20, 2023
Merged

Add support for Fastify #474

merged 1 commit into from
Feb 20, 2023

Conversation

timostamm
Copy link
Member

This adds support for the web framework fastify.

With the help of ConnectRouter introduced in #473:

// connect.ts
import { ConnectRouter } from "@bufbuild/connect";

export default function(router: ConnectRouter) {
  // implement rpc Say(SayRequest) returns (SayResponse)
  router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({
    sentence: `you said: ${req.sentence}`,
  }));
}

... plugging services into a fastify server becomes really straight-forward:

// server.ts
import {fastify} from "fastify";
+ import routes from "connect.js";
+ import { fastifyConnectPlugin } from "@bufbuild/connect-fastify";

const server = fastify({
  http2: true,
});

+ await server.register(fastifyConnectPlugin, { 
+  routes 
+ });

await server.listen();

With this, we are also changing the approach taken for @bufbuild/connect-node, and provide a single adapter function for vanilla Node.js servers, that also makes use of ConnectRouter, similar to the example for fastify above:

// server.ts
import * as http2 from "http2";
+ import routes from "connect.js";
+ import { connectNodeAdapter } from "@bufbuild/connect-node";

http2.createServer(
+ connectNodeAdapter({ routes }) // responds with 404 for other requests
).listen(8080);

Other changes:

  • updates all package-level READMEs
  • deprecated all re-exports from @bufbuild/connect(-core) - we ask you kindly to simply change the import to @bufbuild/connect
  • updates connect-node-test to run all tests against a fastify h2c server in addition
  • updates the example to use a vanilla http2 server instead of express for better interop with gRPC clients - we may want to spice this example up in the future

@timostamm timostamm merged commit eacd8ca into main Feb 20, 2023
@timostamm timostamm deleted the tstamm/add-fastify branch February 20, 2023 22:56
@smaye81 smaye81 mentioned this pull request Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants