Skip to content

DizoftTeam/jsonrpc_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go JsonRpc Server

GitHub go.mod Go version of a Go module GitHub tag

GitHub stars GitHub issues MIT license Open Source? Yes!

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)
    }
}