// Process function func (s *Server) Process(str *string) (string, error) { buffer := new(bytes.Buffer) _, err := buffer.WriteString(*str) if err != nil { s.Err(err) return "", err } t1 := &thrift.TMemoryBuffer{Buffer: buffer} in := thrift.NewTJSONProtocol(t1) t2 := thrift.NewTMemoryBufferLen(1024) //defer t2.Close() out := thrift.NewTJSONProtocol(t2) s.processor.Process(in, out) return t2.String(), nil }
// NewThriftHandler retuns http.Handler func NewThriftHandler(processor thrift.TProcessor) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { if r.Method != `POST` { http.Error(w, "Thrift Handler requires POST access", http.StatusInternalServerError) return } buffer := new(bytes.Buffer) _, err := buffer.ReadFrom(r.Body) if err != nil { http.Error(w, "Could not read body", http.StatusInternalServerError) return } inTransport := &thrift.TMemoryBuffer{Buffer: buffer} inProtocol := thrift.NewTJSONProtocol(inTransport) outTransport := thrift.NewTMemoryBufferLen(1024) outProtocol := thrift.NewTJSONProtocol(outTransport) processor.Process(inProtocol, outProtocol) fmt.Fprint(w, outTransport) } }
"github.com/gin-gonic/gin" "github.com/gorilla/websocket" "github.com/huodon/wpc/thrift/impl" "github.com/huodon/wpc/thrift/service" ) var upgrade = websocket.Upgrader{ HandshakeTimeout: 15 * time.Second, Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) { log.Printf("some error happend: %v, state: %d, reason: %s", r.Host, status, reason) }, } var ( trans = thrift.NewTMemoryBufferLen(bytes.MinRead << 4) proto = thrift.NewTJSONProtocol(trans) process = thrift.NewTMultiplexedProcessor() boxProc = service.NewBoxServiceProcessor(impl.GetServiceImpl()) inChan = make(chan []byte) ) func init() { process.RegisterDefault(boxProc) } func wsHandle(w http.ResponseWriter, r *http.Request) { log.Println(`incomeing websocket request`) c, err := upgrade.Upgrade(w, r, nil) defer c.Close() go ThriftProcess(c)