| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package ephelper | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "fmt" | |
| 10 | |
| 11 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
| 12 "github.com/luci/luci-go/appengine/ephelper/epfrontend" | |
| 13 ) | |
| 14 | |
| 15 const errPrefix = "endpoints.Register: " | |
| 16 | |
| 17 var ( | |
| 18 // ErrServerNil is returned if you pass a nil server to Register. Don't
do | |
| 19 // that. | |
| 20 ErrServerNil = errors.New(errPrefix + "server is nil") | |
| 21 | |
| 22 // ErrServiceNil is returned if you pass a nil service to Register. Don'
t do | |
| 23 // that. | |
| 24 ErrServiceNil = errors.New(errPrefix + "service is nil") | |
| 25 | |
| 26 // DefaultHelper is the default Helper implementation. | |
| 27 DefaultHelper = Helper{} | |
| 28 ) | |
| 29 | |
| 30 // MethodInfoMap is the common registry for an endpoints service. It's | |
| 31 // used by infra/libs/endpoints_client to populate its API. | |
| 32 type MethodInfoMap map[string]*endpoints.MethodInfo | |
| 33 | |
| 34 // Helper is the endpoint helper configuration. | |
| 35 type Helper struct { | |
| 36 // Frontend is the internally-hosted endpoints frontend service. | |
| 37 // | |
| 38 // If not nil, calls to Register() will also register the resulting | |
| 39 // services with the Frontend instance. | |
| 40 Frontend *epfrontend.Server | |
| 41 } | |
| 42 | |
| 43 // Register adds an endpoints.RegisterService-compatible service object using | |
| 44 // the MethodInfoMap to look up the MethodInfo objects by methodName. It is | |
| 45 // intended to be called at init()-time of an app or client which relies on | |
| 46 // these endpoints. | |
| 47 // | |
| 48 // service should be an instance of your service type (as if you were passing | |
| 49 // it to "go-endpoints/endpoints".RegisterService). | |
| 50 func (h *Helper) Register(server *endpoints.Server, service interface{}, si *end
points.ServiceInfo, | |
| 51 mi MethodInfoMap) error { | |
| 52 if server == nil { | |
| 53 return ErrServerNil | |
| 54 } | |
| 55 if service == nil { | |
| 56 return ErrServiceNil | |
| 57 } | |
| 58 if si == nil { | |
| 59 si = &endpoints.ServiceInfo{Default: true} | |
| 60 } | |
| 61 | |
| 62 api, err := server.RegisterService(service, si.Name, si.Version, | |
| 63 si.Description, si.Default) | |
| 64 if err != nil { | |
| 65 return err | |
| 66 } | |
| 67 | |
| 68 for methodName, info := range mi { | |
| 69 method := api.MethodByName(methodName) | |
| 70 if method == nil { | |
| 71 return fmt.Errorf( | |
| 72 errPrefix+"no method %q (did you forget to expor
t it?)", methodName) | |
| 73 } | |
| 74 curInfo := method.Info() | |
| 75 // These three are set automatically based on reflection, so onl
y override | |
| 76 // them if the info object contains something new. | |
| 77 if info.Name == "" { | |
| 78 info.Name = curInfo.Name | |
| 79 } | |
| 80 if info.Path == "" { | |
| 81 info.Path = curInfo.Path | |
| 82 } | |
| 83 if info.HTTPMethod == "" { | |
| 84 info.HTTPMethod = curInfo.HTTPMethod | |
| 85 } | |
| 86 *curInfo = *info | |
| 87 mi[methodName] = curInfo // So that we can observe the merged re
sult | |
| 88 } | |
| 89 | |
| 90 if h.Frontend != nil { | |
| 91 if err := h.Frontend.RegisterService(api); err != nil { | |
| 92 return fmt.Errorf("%s: failed to register with frontend:
%v", errPrefix, err) | |
| 93 } | |
| 94 } | |
| 95 return nil | |
| 96 } | |
| 97 | |
| 98 // Register is a convenience method to pass through registration to the | |
| 99 // DefaultHelper. | |
| 100 func Register(server *endpoints.Server, service interface{}, si *endpoints.Servi
ceInfo, mi MethodInfoMap) error { | |
| 101 return DefaultHelper.Register(server, service, si, mi) | |
| 102 } | |
| OLD | NEW |