/
main.go
52 lines (39 loc) · 1.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"github.com/gorilla/mux"
"github.com/gorilla/rpc"
"github.com/gorilla/rpc/json"
"net/http"
"fmt"
)
type SmsArgs struct {
Number, Content string
}
type EmailArgs struct {
To, Subject, Content string
}
type Response struct {
Result string
}
type SmsService struct{}
type EmailService struct{}
func (t *SmsService) SendSMS(r *http.Request, args *SmsArgs, result *Response) error {
*result = Response{Result: fmt.Sprintf("Sms sent to %s", args.Number)}
return nil
}
func (t *EmailService) SendEmail(r *http.Request, args *EmailArgs, result *Response) error {
*result = Response{Result: fmt.Sprintf("Email sent to %s", args.To)}
return nil
}
func main() {
rpcServer := rpc.NewServer()
rpcServer.RegisterCodec(json.NewCodec(), "application/json")
rpcServer.RegisterCodec(json.NewCodec(), "application/json;charset=UTF-8")
sms := new(SmsService)
email := new(EmailService)
rpcServer.RegisterService(sms, "sms")
rpcServer.RegisterService(email, "email")
router := mux.NewRouter()
router.Handle("/delivery", rpcServer)
http.ListenAndServe(":1337", router)
}