jsonrpc_server

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 9 Imported by: 0

README

Go JsonRpc Server

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

GitHub stars GitHub issues MIT license

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomHandler added in v1.1.0

func CustomHandler(rawJsonData []byte) string

CustomHandler used for custom incoming messages point (like WS)

func HttpHandler added in v1.1.0

func HttpHandler(w http.ResponseWriter, r *http.Request)

HttpHandler Main point function of http handler

func Register

func Register(name string, handler RPCMethod)

Register Add method based on struct style

func RegisterFunc

func RegisterFunc(name string, method Method)

RegisterFunc Add method based on lambda func

Types

type Method

type Method func(params any) (any, *RPCError)

Method Alias on func

type RPCError

type RPCError struct {
	Code    int    // Error code
	Message string // Error message
}

RPCError Error struct

func EmptyRequestError

func EmptyRequestError() (any, *RPCError)

EmptyRequestError Wrong data or Empty request Helper function for most cases

type RPCMethod

type RPCMethod interface {
	Handler(params any) (any, *RPCError)
}

RPCMethod Interface for struct style method

type RPCRequest

type RPCRequest struct {
	Version string `mstruct:"jsonrpc"` // Protocol version
	Method  string `mstruct:"method"`  // Method name
	Params  any    `mstruct:"params"`  // Method params
	ID      int    `mstruct:"id"`      // Request id
}

RPCRequest RPC struct

type Session

type Session struct {
	Request *http.Request // Current request
}

Session contains request info

func NewSession

func NewSession() *Session

NewSession create new request session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL