This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PingCounter is a struct that keeps track of how many Pings that are performed | |
type PingCounter struct { | |
Pings int | |
} | |
// ServerCount is a gRPC UnaryServerInterceptor that will count number of API calls. | |
func (pc *PingCounter) ServerCount(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (response interface{}, err error) { | |
// Append to PingCounts | |
pc.Pings++ | |
// We want to extract metadata from the incomming context. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GenerateTLSApi will load TLS certificates and key and create a grpc server with those. | |
func GenerateTLSApi(pemPath, keyPath string) (*grpc.Server, error) { | |
cred, err := credentials.NewServerTLSFromFile(pemPath, keyPath) | |
if err != nil { | |
return nil, err | |
} | |
// Add PingCounter from the interceptors package | |
pc := interceptors.PingCounter{} | |
s := grpc.NewServer( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ClientPingCounter is a UnaryClientInterceptor that will count the number of API calls on the Client side | |
func (pc *PingCounter) ClientPingCounter(ctx context.Context, method string, req interface{}, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | |
pc.Pings++ | |
// Run regular gRPC call after | |
// If you dont run the invoker, the gRPC call wont be sent to the server | |
return invoker(ctx, method, req, reply, cc, opts...) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"crypto/tls" | |
"crypto/x509" | |
"fmt" | |
"io/ioutil" | |
"log" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function GetUser(client, callback) { | |
var user = JSON.parse(localStorage.getItem('user')); | |
var userRequest = new UserRequest(); | |
if (user !== null) { | |
userRequest.setId(user.id); | |
var metadata = { "x-user-auth-token": user.token, "x-user-auth-id": user.id, } | |
client.getUser(userRequest, metadata, callback); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lets bind a function to change the counter based on the metadata field | |
request.on('metadata', function(status) { | |
// pingCounts are stored in Metadata, and metadata is a key value map | |
setServerPings(status['ping-counts']); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './App.css'; | |
import React, {useState, useEffect } from 'react'; | |
import { PingPongClient } from './proto/service_grpc_web_pb'; | |
import { PingRequest } from './proto/service_pb'; | |
// We create a client that connects to the api | |
var client = new PingPongClient("https://localhost:8080"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interceptors | |
import ( | |
"context" | |
"fmt" | |
"google.golang.org/grpc" | |
) | |
// LogRequest is a gRPC UnaryServerInterceptor that will log the API call to stdOut |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GenerateTLSApi will load TLS certificates and key and create a grpc server with those. | |
func GenerateTLSApi(pemPath, keyPath string) (*grpc.Server, error) { | |
cred, err := credentials.NewServerTLSFromFile(pemPath, keyPath) | |
if err != nil { | |
return nil, err | |
} | |
// Add PingCounter from the interceptors package | |
pc := interceptors.PingCounter{} | |
s := grpc.NewServer( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"net/http" | |
"time" | |
"github.com/improbable-eng/grpc-web/go/grpcweb" | |
"github.com/percybolmer/grpcexample/interceptors" |