modulepackage
2.0.0+incompatible
Repository: https://github.com/dizoftteam/jsonrpc_server.git
Documentation: pkg.go.dev
# README
Go JsonRpc Server
Base implementation of JSONRpc v2.0 in Go
Breaking changes of v2
HttpHandler
Cause: add new handler
Old
http.HandleFunc("/", jsonrpc.Handler)
New
http.HandleFunc("/", jsonrpc.HttpHandler)
How to get
Use follow command
go get github.com/DizoftTeam/jsonrpc_server
Example
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)
}
}
# Packages
No description provided by the author
# Functions
CustomHandler used for custom incoming messages point (like WS).
EmptyRequestError Wrong data or Empty request Helper function for most cases.
HttpHandler Main point function of http handler.
NewSession create new request session.
Register Add method based on struct style.
RegisterFunc Add method based on lambda func.
# Structs
RPCError Error struct.
RPCRequest RPC struct.
Session contains request info.
# Interfaces
RPCMethod Interface for struct style method.
# Type aliases
Method Alias on func.