Documentation ¶
Overview ¶
Package babel provides access to the exported methods of an object across an http connection. A server registers an object, making it visible as a service with the name of the type of the object. After registration, exported methods of the object will be accessible remotely. A server may register multiple objects (services) of different types but it is an error to register multiple objects of the same type.
Only methods that satisfy these criteria will be made available for remote access; other methods will be ignored:
- the method is exported.
- the method has two arguments, both exported (or builtin) types.
- the method's second argument is a pointer.
- the method has return type error.
In effect, the method must look schematically like
func (t *T) MethodName(argType T1, replyType *T2) error
where T, T1 and T2 can be marshaled by encoding/json.
NOTE: Half of this code is adapted from Go's rpc/jsonrpc package. The goal was to be able to use Babel as either a normal Babel HTTP server or a Go jsonrpc server.
Index ¶
Constants ¶
const (
// Defaults used by HandleHTTP
DefaultHttpPath = "/_babel_/"
)
Variables ¶
var DefaultServer = NewServer()
DefaultServer is the default instance of *Server.
Functions ¶
func BabelError ¶
func BabelError(w http.ResponseWriter, msg string, code int)
func HandleHTTP ¶
func HandleHTTP()
HandleHTTP registers an HTTP handler for RPC messages to DefaultServer on DefaultRPCPath and a debugging handler on DefaultDebugPath. It is still necessary to invoke http.Serve(), typically in a go statement.
func Register ¶
func Register(rcvr interface{}) error
Register publishes the receiver's methods in the DefaultServer.
func RegisterName ¶
RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.
Types ¶
type Error ¶
type Error struct { // The service-specific error code Code *string `json:"Code,omitempty"` // The text of the error in US-English Message *string `json:"Message,omitempty"` // The list of parameters to the error message. This could be used by // localization systems to generate messages based on the error code. Params []*string `json:"Params,omitempty"` }
Error defines a single error message and code that might be localized and displayed to a caller.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a Babel Server.
func (*Server) HandleHTTP ¶
HandleHTTP registers an HTTP handler for Babel messages on babelPath. It is still necessary to invoke http.Serve(), typically in a go statement.
func (*Server) Register ¶
Register publishes in the server the set of methods of the receiver value that satisfy the following conditions:
- exported method
- two arguments, both of exported type
- the second argument is a pointer
- one return value, of type error
It returns an error if the receiver is not an exported type or has no suitable methods. It also logs the error using package log. The client accesses each method using a string of the form "Type.Method", where Type is the receiver's concrete type.
func (*Server) RegisterName ¶
RegisterName is like Register but uses the provided name for the type instead of the receiver's concrete type.
type ServiceError ¶
type ServiceError struct { // Time of the error Time *time.Time `json:"Time,omitempty"` // Tags to categorize the error Tags []*string `json:"Tags,omitempty"` // A list of errors that occurred Errors []*Error `json:"Errors,omitempty"` // Context is a map of additional error details and context Context map[string]map[string]*string `json:"Context,omitempty"` // Details contains optional detailed error information, such as error messages // or a stack trace specific to the given server environment Details *string `json:"Details,omitempty"` // Inner points to a service error propagated from another tier that is using Babel Inner *ServiceError `json:"Inner,omitempty"` }
ServiceError defines the error response generated by Babel for service failures
func (*ServiceError) Init ¶
func (obj *ServiceError) Init() *ServiceError
Init sets default values for a ServiceError