Documentation ¶
Index ¶
- type Middleware
- type Opt
- func WithCertCacheDir(certCache string) Opt
- func WithGRPCRoutes(router RouterFunc) Opt
- func WithHTTPRoutes(router RouterFunc) Opt
- func WithHostPolicy(policy autocert.HostPolicy) Opt
- func WithInsecurePort(insecurePort int) Opt
- func WithLogger(logger *logger.Logger) Opt
- func WithMiddlewares(middlewares ...Middleware) Opt
- func WithSecurePort(securePort int) Opt
- func WithStreamInterceptors(sinterceptors ...grpc.StreamServerInterceptor) Opt
- func WithUnaryInterceptors(uinterceptors ...grpc.UnaryServerInterceptor) Opt
- type Proxy
- type RouterFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
Middleware is an http middleware
type Opt ¶
type Opt func(p *Proxy)
Opt is a function that configures a Proxy instance
func WithCertCacheDir ¶ added in v0.0.5
WithCertCacheDir sets the directory in which certificates will be cached (default: /tmp/certs)
func WithGRPCRoutes ¶
func WithGRPCRoutes(router RouterFunc) Opt
WithGRPCRoutes sets the gRPC RouterFunc which takes a hostname and returns an endpoint to route to. either http routes, gRPC routes, or both are required.
func WithHTTPRoutes ¶
func WithHTTPRoutes(router RouterFunc) Opt
WithHTTPRoutes sets the http RouterFunc which takes a hostname and returns an endpoint to route to either http routes, gRPC routes, or both are required.
func WithHostPolicy ¶
func WithHostPolicy(policy autocert.HostPolicy) Opt
WithHostPolicy sets the host policy function on the proxy(required)
func WithInsecurePort ¶
WithInsecurePort sets the port that non-encrypted traffic will be served on(optional)
func WithLogger ¶
WithLogger sets the proxies logger instance(optional)
func WithMiddlewares ¶
func WithMiddlewares(middlewares ...Middleware) Opt
WithMiddlewares sets the http middlewares on encrypted & non-encrypted traffic(optional)
func WithSecurePort ¶
WithSecurePort sets the port that encrypted traffic will be served on(optional)
func WithStreamInterceptors ¶
func WithStreamInterceptors(sinterceptors ...grpc.StreamServerInterceptor) Opt
WithStreamInterceptors adds gRPC stream interceptors to the proxy instance
func WithUnaryInterceptors ¶
func WithUnaryInterceptors(uinterceptors ...grpc.UnaryServerInterceptor) Opt
WithUnaryInterceptors adds gRPC unary interceptors to the proxy instance
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is a secure(lets encrypt) gRPC & http reverse proxy
func New ¶
New creates a new proxy instance. A host policy & either http routes, gRPC routes, or both are required.
Example ¶
package main import ( "context" "errors" "fmt" "github.com/graphikDB/gproxy" "net/http" "net/http/httptest" "strings" "time" ) func main() { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world")) })) proxy, err := gproxy.New(ctx, gproxy.WithInsecurePort(8080), gproxy.WithHTTPRoutes(func(ctx context.Context, host string) (string, error) { if strings.Contains(host, "localhost") { return srv.URL, nil } return "", errors.New(fmt.Sprintf("%s not allowed", host)) }), gproxy.WithHostPolicy(func(ctx context.Context, host string) error { return nil })) if err != nil { fmt.Println(err.Error()) return } if err := proxy.Serve(ctx); err != nil { fmt.Println(err.Error()) return } }
Output: