package main import ( "context" "log" "net" "google.golang.org/grpc" "github.com.cockroachdb.cockroach.rpc" ) type MyServer struct {} func (s *MyServer) SayHello(ctx context.Context, in *HelloRequest) (*HelloResponse, error) { return &HelloResponse{Message: "Hello, " + in.Name}, nil } func main() { l, err := net.Listen("tcp", "localhost:8080") if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() rpc.RegisterMyServer(s, &MyServer{}) if err := s.Serve(l); err != nil { log.Fatalf("failed to serve: %v", err) } }
package main import ( "context" "log" "net" "google.golang.org/grpc" "github.com.cockroachdb.cockroach.rpc" ) type MyServer struct {} func (s *MyServer) GetWeather(ctx context.Context, in *WeatherRequest) (*WeatherResponse, error) { // Call external weather API to get forecast for requested city forecast, err := getForecastForCity(in.City) if err != nil { return nil, err } return &WeatherResponse{Forecast: forecast}, nil } func main() { l, err := net.Listen("tcp", "localhost:8080") if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() rpc.RegisterMyServer(s, &MyServer{}) if err := s.Serve(l); err != nil { log.Fatalf("failed to serve: %v", err) } }This example demonstrates how to use the github.com.cockroachdb.cockroach.rpc Server library to build an RPC server that calls an external API to obtain weather forecast information for a given city. This example shows how easy it is to connect to external APIs and incorporate their functionality into your RPC server.