《Packt.Mastering.Go.Web.Services.2015.4.pdf》之Using Gorilla for JSON-RPC

官网
http://www.gorillatoolkit.org/pkg/rpc#subdirs

测试代码

为了更好的说明使用方法,对原文的代码有所修改。

type RPCAPIArguments struct {
    Message string
    Msg     string
}
type RPCAPIResponse struct {
    Message string
    Masg    string
}
type StringService struct{}

func (h *StringService) Length(r *http.Request, arguments *RPCAPIArguments, reply *RPCAPIResponse) error {
    Debug(r.Header)
    Debug(arguments.Message)
    Debug(arguments.Msg)
    reply.Message = "Your string is " + fmt.Sprintf("Your string is%d chars long", utf8.RuneCountInString(arguments.Message)) +
        "characters long"
    reply.Masg = "er"
    return nil
}

func f(i *rpc.RequestInfo) {
    Debug("fucn")
    return
}
func main() {
    fmt.Println("Starting service")
    // s := rpc.NewServer()
    // s.RegisterCodec(json.NewCodec(), "application/json")
    // s.RegisterService(new(StringService), "")
    s := rpc.NewServer()
    s.RegisterCodec(json.NewCodec(), "application/json")
    s.RegisterService(new(StringService), "")
    s.RegisterAfterFunc(f)
    http.Handle("/rpc", s)
    http.ListenAndServe(":10000", nil)
}

测试请求

{
  "method": "StringService.Length",
  "params": [
    {
      "Message": "Testing therservied",
      "Msg": "sbig"
    }
  ],
  "id": "3"
}

响应

{
  "result": {
    "Message": "Your string is Your string is19 chars longcharacters long",
    "Masg": "er"
  },
  "error": null,
  "id": "3"
}

说明:
在POST数据中,method指定要远程执行的方法,StringService.Length对应服务器中的StringService类型的Length方法。通过

s.RegisterService(new(StringService), "")

将StringService注册到RPC。
params指定传递的参数,分别对应StringService的两个成员变量 Message,Msg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About This Book, Effectively deploy and integrate Go web services with applications in the real worldFamiliarize yourself with RESTful practices and apply them in GoA comprehensive tutorial with lots of tips and tricks to program and develop web services in Go, Who This Book Is For, If you are a web programmer with experience in developing web services and have a rudimentary knowledge of using Go, then this is the book for you. Basic knowledge of Go as well as knowledge of relational databases and non-relational NoSQL datastores is assumed. Some basic concurrency knowledge is also required., What You Will Learn, Familiarize yourself with RESTful practices and apply them in Go Acquaint yourself with the best practices for API design such as URL routing and HTTP response methods Utilize useful middleware and frameworks to augment applications otherwise relying on pure Go Understand the basic and complex authentication models for APIs Deploy your code to staging and development environments Look at ways to make your Go web services as speedy and thrifty as possible Secure your web services Ingest your own API and make it appealing to other developers, In Detail, This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise., By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go.
Program and deploy fast, scalable web services and create high-performance RESTful APIs using Go About This Book Effectively deploy and integrate Go web services with applications in the real world Familiarize yourself with RESTful practices and apply them in Go A comprehensive tutorial with lots of tips and tricks to program and develop web services in Go Who This Book Is For If you are a web programmer with experience in developing web services and have a rudimentary knowledge of using Go, then this is the book for you. Basic knowledge of Go as well as knowledge of relational databases and non-relational NoSQL datastores is assumed. Some basic concurrency knowledge is also required. What You Will Learn Familiarize yourself with RESTful practices and apply them in Go Acquaint yourself with the best practices for API design such as URL routing and HTTP response methods Utilize useful middleware and frameworks to augment applications otherwise relying on pure Go Understand the basic and complex authentication models for APIs Deploy your code to staging and development environments Look at ways to make your Go web services as speedy and thrifty as possible Secure your web services Ingest your own API and make it appealing to other developers In Detail This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise. By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go. Table of Contents Chapter 1: Our First API in Go Chapter 2: RESTful Services in Go Chapter 3: Routing and Bootstrapping Chapter 4: Designing APIs in Go Chapter 5: Templates and Options in Go Chapter 6: Accessing and Using Web Services in Go Chapter 7: Working with Other Web Technologies Chapter 8: Responsive Go for the Web Chapter 9: Deployment Chapter 10: Maximizing Performance Chapter 11: Security

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值