Base implementation of JSONRpc v2.0 in Go
Cause: add new handler
Old
http.HandleFunc("/", jsonrpc.Handler)
New
http.HandleFunc("/", jsonrpc.HttpHandler)
Use follow command
go get github.com/DizoftTeam/jsonrpc_server
Command example below
package main
import (
jsonrpc "github.com/DizoftTeam/jsonrpc_server"
"fmt"
"log"
"net/http"
)
// UserLogin controller for login method
type UserLogin struct {}
// Handler worker
func (u UserLogin) Handler(params interface{}) (interface{}, *jsonrpc.RPCError) {
// Some logic/magic here.
// It's like a controller
// To getting raw request you can run this
// session := jsonrpc.NewSession()
// Success
return "Login ok!", nil
// Fail
//return nil, &jsonrpc.RPCError{
// Code: -10,
// Message: "Cant login",
//}
}
// Register methods and callbacks
func registerMethods() {
jsonrpc.Register("user.login", UserLogin{})
jsonrpc.RegisterFunc("version", func(params interface{}) (interface{}, *jsonrpc.RPCError) {
return "1.0.0", nil
})
}
func main() {
registerMethods()
http.HandleFunc("/", jsonrpc.HttpHandler)
log.Print("\nStarting server at :8089\n")
if err := http.ListenAndServe(":8089", nil); err != nil {
log.Panic("Cant start server", err)
}
}