-
Notifications
You must be signed in to change notification settings - Fork 9
/
registrar.go
121 lines (96 loc) · 3.4 KB
/
registrar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package servercore
import (
"context"
"net"
"net/http"
"github.com/gorilla/mux"
"google.golang.org/grpc"
"namespacelabs.dev/foundation/std/go/core"
)
type Registrar interface {
grpc.ServiceRegistrar
Handle(path string, p http.Handler) *mux.Route
PathPrefix(path string) *mux.Route
RegisterListener(func(context.Context, net.Listener) error)
}
type Server interface {
Scope(*core.Package) Registrar
}
var _ Server = &ServerImpl{}
// Implements the grpc.ServiceRegistrar interface.
type ServerImpl struct {
srv map[string][]*grpc.Server // Key is configuration name; "" is the default.
listeners []listenerRegistration
httpMux *mux.Router
}
type listenerRegistration struct {
PackageName string
ConfigurationName string
Handler func(context.Context, net.Listener) error
}
func (s *ServerImpl) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) *mux.Route {
return s.Handle(path, http.HandlerFunc(f))
}
func (s *ServerImpl) Handle(path string, p http.Handler) *mux.Route {
return s.httpMux.Handle(path, p)
}
func (s *ServerImpl) PathPrefix(path string) *mux.Route {
return s.httpMux.PathPrefix(path)
}
func (s *ServerImpl) registerService(pkg, config string, desc *grpc.ServiceDesc, impl interface{}) {
if srvs, ok := s.srv[config]; ok {
core.ZLog.Info().Str("package_name", pkg).Str("configuration", config).Msgf("Registered %v", desc.ServiceName)
for _, srv := range srvs {
srv.RegisterService(desc, impl)
}
} else {
core.ZLog.Fatal().Str("package_name", pkg).Msgf("servercore: no such configuration %q (registering %v)", config, desc.ServiceName)
}
}
func (s *ServerImpl) registerListener(pkg, config string, handler func(context.Context, net.Listener) error) {
s.listeners = append(s.listeners, listenerRegistration{
PackageName: pkg,
ConfigurationName: config,
Handler: handler,
})
}
func (s *ServerImpl) Scope(pkg *core.Package) Registrar {
return &scopedServer{pkg: pkg.PackageName, configurationName: pkg.ListenerConfiguration, parent: s}
}
type scopedServer struct {
pkg string
configurationName string
parent *ServerImpl
}
func (s *scopedServer) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
s.parent.registerService(s.pkg, s.configurationName, desc, impl)
}
func (s *scopedServer) Handle(path string, p http.Handler) *mux.Route {
return s.parent.Handle(path, p)
}
func (s *scopedServer) PathPrefix(path string) *mux.Route {
return s.parent.PathPrefix(path)
}
func (s *scopedServer) RegisterListener(handler func(context.Context, net.Listener) error) {
s.parent.registerListener(s.pkg, s.configurationName, handler)
}
func proxyHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" {
r.RemoteAddr = forwardedFor
}
if host := r.Header.Get("X-Forwarded-Host"); host != "" {
r.URL.Host = host
// Set the scheme (proto) with the value passed from the proxy.
// But only do so if there's a host present.
if scheme := r.Header.Get("X-Forwarded-Scheme"); scheme != "" {
r.URL.Scheme = scheme
}
}
// Call the next handler in the chain.
h.ServeHTTP(w, r)
})
}