Giter VIP home page Giter VIP logo

jsonrpc_server's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.