Server
Server is one of the core features of gRPCity, providing the ability to take over the server-side lifecycle. Through a series of interface functions, it offers full-cycle management APIs for tasks such as certificate import, service binding, middleware support, server startup, and server shutdown.
Initialization
// Get the server instance
const server = await loader.initServer(serverOptions)serverOptions is an object that includes the following parameters:
channelOptions: Optional, communication configuration rules;credentials: Optional, server-side certificate;
Detailed explanations of channelOptions can be found in the Config Guide.
Detailed explanations of credentials can be found in the Credentials Guide.
Service Binding
// Service RPC binding
server.add(name, implementation, options)Parameter explanations:
name: (Required) Specifies the route in theprotofile, specifically to the location of theservice;implementation: (Required) An object or class carrying methods defined in the service RPC;options: (Optional) Object:exclude: (Optional) Array of strings, excluding methods inimplementationthat do not need to be involved inservice RPCbinding;inherit: (Optional) Ifimplementationobtainsfuncthrough inheritance, and the parent class should also be involved in binding, pass the parent class to this parameter;
Service Remove
// Service RPC unbinding
server.remove(name)Parameter explanation:
name: (Required) Specifies the route in theprotofile, specifically to the location of theservice;
Middleware
server.use() provides middleware capabilities for pre and post-processing of implementation. For more details, see the Middleware Guide.
// Middleware support for implementation
// Add one by one
server.use(f1)
server.use(f2)
server.use(f3)
// Add with multiple parameters
server.use(f1, f2, f3)
// Add using an array
server.use([f1, f2, f3])Note: Only async methods are supported for middleware in the current version; callback is not supported yet.
Certificates and Startup
The listen method for server startup supports two parameters: addr and credentials:
addr: (Required) The address and port that the service startup needs to listen to, supporting either of the following types:- string: in the format of ip+port, such as
127.0.0.1:9099; - object:
{ host, port };- host: string, supports both ip and domain names;
- port: number, with a minimum value of 0 and a maximum value of 65535;
- string: in the format of ip+port, such as
credentials: (Optional) Generated usingloader.makeServerCredentials()and passed to thelistencall;
// Server certificate
const credentials = server.makeServerCredentials(rootCerts, keyCertPairs, checkClientCertificate)
// Server startup
await server.listen(addr, credentials)
// or
// await server.listen('127.0.0.1:9099', credentials)
// await server.listen({ host: '127.0.0.1', port: 9099 }, credentials)Priority of credentials: Highest in listen(), followed by initServer(), and finally the default.
Server Shutdown
shutdown() needs to use async to ensure asynchronous execution completion, while forceShutdown does not.
// Graceful server shutdown
await server.shutdown()
// Forceful server shutdown
server.forceShutdown()