Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
@percybolmer
percybolmer / ServerCount - UnaryInterceptor.go
Last active August 6, 2021 11:35
A example of a UnaryServerInterceptor
// 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.
@percybolmer
percybolmer / GenerateTLSApi - With PingCount.go
Last active August 6, 2021 11:36
A pingcounting grpc Server
// 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(
@percybolmer
percybolmer / UnaryClientinterceptor example.go
Last active August 6, 2021 11:36
An Example of a unary client interceptor
// 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...)
}
@percybolmer
percybolmer / grpc Client with Interceptor.go
Last active August 6, 2021 11:36
An grpc client with a interceptor
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
@percybolmer
percybolmer / gRPC-web Own interceptor.js
Last active August 6, 2021 11:36
A function that wraps our gRPC request and adds metadata for JWT
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);
}
}
@percybolmer
percybolmer / gRPC-web Header metadata example.js
Last active August 6, 2021 11:36
An example of how you can listen on header meta
// 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']);
})
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");
package interceptors
import (
"context"
"fmt"
"google.golang.org/grpc"
)
// LogRequest is a gRPC UnaryServerInterceptor that will log the API call to stdOut
// 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(
package main
import (
"log"
"net"
"net/http"
"time"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/percybolmer/grpcexample/interceptors"