mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-15 17:19:10 +00:00
chore: remove gripkit as it breaks builds
This commit is contained in:
parent
01d3e6d6cb
commit
af4226ac09
6 changed files with 43 additions and 369 deletions
|
@ -1,16 +0,0 @@
|
|||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "gripkit",
|
||||
srcs = [
|
||||
"gripkit.go",
|
||||
"options.go",
|
||||
],
|
||||
importpath = "github.com/roleypoly/roleypoly/src/gripkit",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"@com_github_improbable_eng_grpc_web//go/grpcweb",
|
||||
"@io_k8s_klog//:klog",
|
||||
"@org_golang_google_grpc//:go_default_library",
|
||||
],
|
||||
)
|
|
@ -1,103 +0,0 @@
|
|||
// Package gripkit provides wrappers and helpers for
|
||||
package gripkit
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
||||
"google.golang.org/grpc"
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
type Gripkit struct {
|
||||
Server *grpc.Server
|
||||
httpHandler *func(w http.ResponseWriter, r *http.Request)
|
||||
options *options
|
||||
grpcWebServer *grpcweb.WrappedGrpcServer
|
||||
ready bool
|
||||
}
|
||||
|
||||
func Create(options ...Option) *Gripkit {
|
||||
gkOptions := evaluateOptions(options...)
|
||||
grpcServer := grpc.NewServer(gkOptions.grpcOptions...)
|
||||
|
||||
var grpcWrapper *grpcweb.WrappedGrpcServer
|
||||
if gkOptions.wrapGrpcWeb {
|
||||
grpcWrapper = grpcweb.WrapServer(grpcServer, gkOptions.grpcWebOptions...)
|
||||
}
|
||||
|
||||
gk := &Gripkit{
|
||||
Server: grpcServer,
|
||||
grpcWebServer: grpcWrapper,
|
||||
options: gkOptions,
|
||||
ready: false,
|
||||
}
|
||||
|
||||
if gk.options.healthz != nil {
|
||||
if gk.options.healthz.UseDefault {
|
||||
if gk.options.healthz.Addr == "" {
|
||||
gk.options.healthz.Addr = ":10456"
|
||||
}
|
||||
|
||||
gk.options.healthz.Handler = gk.defaultHealthHandler
|
||||
}
|
||||
|
||||
go gk.startHealthzServer()
|
||||
}
|
||||
|
||||
return gk
|
||||
}
|
||||
|
||||
func (gk *Gripkit) Serve() error {
|
||||
handler := gk.Server.ServeHTTP
|
||||
if gk.options.wrapGrpcWeb {
|
||||
handler = gk.grpcWebServer.ServeHTTP
|
||||
}
|
||||
|
||||
httpHandler := http.HandlerFunc(handler)
|
||||
|
||||
if gk.options.wrapDebug {
|
||||
httpHandler = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
||||
source := "gRPC native"
|
||||
if req.Header.Get("X-Grpc-Web") == "1" {
|
||||
source = "gRPC web"
|
||||
}
|
||||
klog.Infoln("gRPC debug: url:", req.URL, "source:", source)
|
||||
handler(resp, req)
|
||||
})
|
||||
}
|
||||
|
||||
if gk.options.httpOptions.TLSCertPath == "" || gk.options.httpOptions.TLSKeyPath == "" {
|
||||
return http.ListenAndServe(
|
||||
gk.options.httpOptions.Addr,
|
||||
httpHandler,
|
||||
)
|
||||
}
|
||||
|
||||
gk.ready = true
|
||||
|
||||
return http.ListenAndServeTLS(
|
||||
gk.options.httpOptions.Addr,
|
||||
gk.options.httpOptions.TLSCertPath,
|
||||
gk.options.httpOptions.TLSKeyPath,
|
||||
httpHandler,
|
||||
)
|
||||
}
|
||||
|
||||
func (gk *Gripkit) startHealthzServer() {
|
||||
klog.Infoln("Starting /healthz server on", gk.options.healthz.Addr)
|
||||
err := http.ListenAndServe(gk.options.healthz.Addr, gk.options.healthz.Handler)
|
||||
if err != nil {
|
||||
klog.Errorln("/healthz server failed to start", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (gk *Gripkit) defaultHealthHandler(rw http.ResponseWriter, r *http.Request) {
|
||||
if gk.ready {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
rw.Write([]byte(`OK`))
|
||||
} else {
|
||||
rw.WriteHeader(http.StatusServiceUnavailable)
|
||||
rw.Write([]byte(`Not Ready`))
|
||||
}
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
package gripkit
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type HTTPOptions struct {
|
||||
TLSCertPath string
|
||||
TLSKeyPath string
|
||||
Addr string
|
||||
}
|
||||
|
||||
type HealthzOptions struct {
|
||||
UseDefault bool
|
||||
Handler http.HandlerFunc
|
||||
|
||||
// Optional IP:Port string, defaults to :10456
|
||||
Addr string
|
||||
}
|
||||
|
||||
type Option func(*options)
|
||||
|
||||
type options struct {
|
||||
wrapGrpcWeb bool
|
||||
grpcWebOptions []grpcweb.Option
|
||||
httpOptions HTTPOptions
|
||||
grpcOptions []grpc.ServerOption
|
||||
wrapDebug bool
|
||||
healthz *HealthzOptions
|
||||
}
|
||||
|
||||
var (
|
||||
defaultOptions = &options{
|
||||
wrapGrpcWeb: false,
|
||||
grpcWebOptions: []grpcweb.Option{},
|
||||
grpcOptions: []grpc.ServerOption{},
|
||||
wrapDebug: false,
|
||||
httpOptions: HTTPOptions{
|
||||
Addr: ":8080",
|
||||
TLSKeyPath: "",
|
||||
TLSCertPath: "",
|
||||
},
|
||||
healthz: &HealthzOptions{
|
||||
UseDefault: true,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func evaluateOptions(optionList ...Option) *options {
|
||||
evaluatedOptions := defaultOptions
|
||||
|
||||
for _, optionFunc := range optionList {
|
||||
if optionFunc != nil {
|
||||
optionFunc(evaluatedOptions)
|
||||
}
|
||||
}
|
||||
|
||||
return evaluatedOptions
|
||||
}
|
||||
|
||||
func WithDebug() Option {
|
||||
return func(o *options) {
|
||||
o.wrapDebug = true
|
||||
}
|
||||
}
|
||||
|
||||
func WithGrpcWeb(opts ...grpcweb.Option) Option {
|
||||
return func(o *options) {
|
||||
o.wrapGrpcWeb = true
|
||||
o.grpcWebOptions = opts
|
||||
}
|
||||
}
|
||||
|
||||
func WithOptions(opts ...grpc.ServerOption) Option {
|
||||
return func(o *options) {
|
||||
o.grpcOptions = opts
|
||||
}
|
||||
}
|
||||
|
||||
func WithHTTPOptions(opts HTTPOptions) Option {
|
||||
return func(o *options) {
|
||||
o.httpOptions = opts
|
||||
}
|
||||
}
|
||||
|
||||
// WithHealthz adds a custom /healthz handler to the gRPC HTTP server. Pass nil to disable.
|
||||
func WithHealthz(hzOpts *HealthzOptions) Option {
|
||||
return func(o *options) {
|
||||
if hzOpts == nil {
|
||||
o.healthz = nil
|
||||
return
|
||||
}
|
||||
|
||||
if hzOpts.Addr != "" {
|
||||
o.healthz.Addr = hzOpts.Addr
|
||||
}
|
||||
|
||||
o.healthz.Handler = hzOpts.Handler
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue