Created
August 25, 2021 15:59
-
-
Save progrium/7c3473f18663c46c217384d95bbc152b to your computer and use it in GitHub Desktop.
simple qtalk rpc server example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server.go | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net" | |
"strings" | |
"github.com/progrium/qtalk-go/codec" | |
"github.com/progrium/qtalk-go/fn" | |
"github.com/progrium/qtalk-go/rpc" | |
) | |
type service struct{} | |
func (svc *service) Upper(s string) string { | |
return strings.ToUpper(s) | |
} | |
// methods can opt-in to receive the call as last argument. | |
// also, errors can be returned to be received as remote errors. | |
func (svc *service) Error(s string, c *rpc.Call) error { | |
return fmt.Errorf("%s [%s]", s, c.Selector) | |
} | |
func main() { | |
// create a tcp listener | |
l, err := net.Listen("tcp", "localhost:9999") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// setup a server using fn.HandlerFrom to | |
// handle methods from the service type | |
srv := &rpc.Server{ | |
Codec: codec.JSONCodec{}, | |
Handler: fn.HandlerFrom(new(service)), | |
} | |
// serve until the listener closes | |
srv.Serve(l) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment