1 package server 2 3 import ( 4 "context" 5 "fmt" 6 au "golang.conradwood.net/apis/auth" 7 pb "golang.conradwood.net/apis/registry" 8 ) 9 10 type ServerDef interface { 11 12 //if something needs to be done to errors before they propagate up the stack, then this hook can be used to do so 13 SetErrorHandler(e func(ctx context.Context, fn string, err error)) 14 /* 15 set to true if this server does NOT require authentication (default: it does need authentication). 16 This should normally not be necessary. Normally, a service needs to be called with EITHER a service account OR a user account OR both. There are very special circumstances where this is not possible, for example, the registry and the auth service cannot be called with a service or user account, because in order to get one, the service needs to lookup and call the auth service. Thus registry and auth both expose their RPCs as "NoAuth". In normal circumstances this is never necessary. 17 */ 18 SetNoAuth() 19 // the tcp port to listen on 20 SetPort(port int) 21 // register the implementation of the gRPC service 22 SetRegister(r Register) 23 DontRegister() // if this service should not register with the registry initially 24 // assume the service is directly accessible on a public ip. this disables functionalitity normally filtered out by proxies, such as /internal/ helpers and reflection. Normally not needed. Typically h2gproxy proxies requests. 25 SetPublic() 26 /* 27 set a callback that is called AFTER grpc server started successfully 28 */ 29 SetOnStartupCallback(f func()) 30 AddTag(key, value string) // add a routing tag to a serverdef 31 32 } 33 34 // no longer exported - please use NewServerDef instead 35 type serverDef struct { 36 callback func() // called if/when server started up successfully 37 port int // the port the GRPC server should listen on 38 Certificate []byte // do not override the default. Exposed due to an implementation limitation 39 Key []byte // do not override the default. Exposed due to an implementation limitation 40 CA []byte // do not override the default. Exposed due to an implementation limitation 41 register Register // do not override the default. Exposed due to an implementation limitation 42 /* 43 set to true if this server does NOT require authentication (default: it does need authentication). 44 This should normally not be necessary. Normally, a service needs to be called with EITHER a service account OR a user account OR both. There are very special circumstances where this is not possible, for example, the registry and the auth service cannot be called with a service or user account, because in order to get one, the service needs to lookup and call the auth service. Thus registry and auth both expose their RPCs as "NoAuth". In normal circumstances this is never necessary. 45 */ 46 noAuth bool 47 // set to false if this service should not register with the registry initially 48 registerService bool 49 name string 50 types []pb.Apitype 51 registered_id string 52 deployPath string // do not override the default. Exposed due to an implementation limitation 53 serviceID uint64 54 asUser *au.SignedUser // if we're running as a user rather than a server this is the account 55 tags map[string]string 56 /* 57 if something needs to be done to errors before they propagate up the stack, then this hook can be used to do so 58 */ 59 errorHandler func(ctx context.Context, function_name string, err error) 60 local_service *au.SignedUser // the local service account 61 service_user_id string // the serviceaccount userid 62 public bool 63 port_set bool 64 } 65 66 func (s *serverDef) SetErrorHandler(e func(ctx context.Context, fn string, err error)) { 67 s.errorHandler = e 68 } 69 func (s *serverDef) SetNoAuth() { 70 s.noAuth = true 71 } 72 func (s *serverDef) SetPort(port int) { 73 s.port = port 74 s.port_set = true 75 } 76 func (s *serverDef) SetRegister(r Register) { 77 s.register = r 78 } 79 func (s *serverDef) DontRegister() { 80 s.registerService = false 81 } 82 func (s *serverDef) SetPublic() { 83 s.public = true 84 } 85 86 /* 87 set a callback that is called AFTER grpc server started successfully 88 */ 89 func (s *serverDef) SetOnStartupCallback(f func()) { 90 s.callback = f 91 } 92 93 // add a routing tag to a serverdef 94 func (s *serverDef) AddTag(key, value string) { 95 s.tags[key] = value 96 } 97 func (s *serverDef) toString() string { 98 return fmt.Sprintf("Port #%d: %s (%v)", s.port, s.name, s.types) 99 } 100