2017-10-31 09:25:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2017-11-01 06:17:32 +00:00
|
|
|
"git.loafle.net/commons_go/server"
|
2017-10-31 09:25:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
2017-11-01 06:17:32 +00:00
|
|
|
server.ServerHandlers
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-01 06:17:32 +00:00
|
|
|
RPCRegistry rpc.Registry
|
|
|
|
codecs map[string]protocol.ServerCodec
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterCodec adds a new codec to the server.
|
|
|
|
//
|
|
|
|
// Codecs are defined to process a given serialization scheme, e.g., JSON or
|
|
|
|
// XML. A codec is chosen based on the "Content-Type" header from the request,
|
|
|
|
// excluding the charset definition.
|
|
|
|
func (sh *ServerHandlers) RegisterCodec(codec protocol.ServerCodec, contentType string) {
|
|
|
|
if nil == sh.codecs {
|
|
|
|
sh.codecs = make(map[string]protocol.ServerCodec)
|
|
|
|
}
|
|
|
|
sh.codecs[strings.ToLower(contentType)] = codec
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) GetContentType(r io.Reader) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPreRead(r io.Reader) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPostRead(r io.Reader) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPreWriteResult(w io.Writer, result interface{}) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPostWriteResult(w io.Writer, result interface{}) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPreWriteError(w io.Writer, err error) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Validate() {
|
2017-11-01 06:17:32 +00:00
|
|
|
sh.ServerHandlers.Validate()
|
|
|
|
|
2017-10-31 10:00:51 +00:00
|
|
|
if nil == sh.RPCRegistry {
|
|
|
|
panic("RPCRegistry must be specified.")
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) getCodec(contentType string) (protocol.ServerCodec, error) {
|
|
|
|
var codec protocol.ServerCodec
|
|
|
|
if contentType == "" && len(sh.codecs) == 1 {
|
|
|
|
// If Content-Type is not set and only one codec has been registered,
|
|
|
|
// then default to that codec.
|
|
|
|
for _, c := range sh.codecs {
|
|
|
|
codec = c
|
|
|
|
}
|
|
|
|
} else if codec = sh.codecs[strings.ToLower(contentType)]; codec == nil {
|
|
|
|
return nil, fmt.Errorf("Unrecognized Content-Type: %s", contentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return codec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) invoke(codec protocol.RegistryCodec) (result interface{}, err error) {
|
2017-10-31 10:00:51 +00:00
|
|
|
return sh.RPCRegistry.Invoke(codec)
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|