All Projects → semrush → Zenrpc

semrush / Zenrpc

Licence: mit
JSON-RPC 2.0 Server Implementation with SMD support written in Go (go generate)

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Zenrpc

Wxapp
🙋travelib-node 流动图书Node.js后端
Stars: ✭ 132 (-5.71%)
Mutual labels:  websocket
Bolt Js
A framework to build Slack apps using JavaScript
Stars: ✭ 1,971 (+1307.86%)
Mutual labels:  websocket
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (-1.43%)
Mutual labels:  websocket
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (-5.71%)
Mutual labels:  websocket
Marewood
🐚 marewood is an open source automatic packaging deployment tool for front-end || MareWood 是一个Go开发的轻量级前端部署工具,可以很灵活的配置各种打包部署环境并提供访问。
Stars: ✭ 135 (-3.57%)
Mutual labels:  websocket
Gin Rtsp
基于Gin + WebSocket + JSMpeg,在HTML页面上直接播放RTSP视频流。
Stars: ✭ 136 (-2.86%)
Mutual labels:  websocket
Compose Server Side
Experiment with server side rendering using compose and ktor
Stars: ✭ 131 (-6.43%)
Mutual labels:  websocket
Workerman Todpole
HTML5+WebSocket+PHP(Workerman) , rumpetroll server writen using php
Stars: ✭ 1,751 (+1150.71%)
Mutual labels:  websocket
Exchanges Php
This is a virtual currency SDK that brings together multiple exchanges
Stars: ✭ 134 (-4.29%)
Mutual labels:  websocket
Websocket
Minimal and idiomatic WebSocket library for Go
Stars: ✭ 2,013 (+1337.86%)
Mutual labels:  websocket
Httpexpect
End-to-end HTTP and REST API testing for Go.
Stars: ✭ 1,821 (+1200.71%)
Mutual labels:  websocket
Kuma
A network library implemented in C++, supports TCP/UDP/HTTP/HTTP2/WebSocket/SSL on platform Linux/Windows/OSX/iOS/Android.
Stars: ✭ 133 (-5%)
Mutual labels:  websocket
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (-2.14%)
Mutual labels:  websocket
Websocket Rpc
WebSocket RPC library for .NET with auto JavaScript client code generation, supporting ASP.NET Core
Stars: ✭ 132 (-5.71%)
Mutual labels:  websocket
Layuiadminprophp
针对LayuiAdmin后台模板使用ThinkPHP5开发的基础版本
Stars: ✭ 139 (-0.71%)
Mutual labels:  websocket
Zerodha live automate trading using ai ml on indian stock market Using Basic Python
Online trading using Artificial Intelligence Machine leaning with basic python on Indian Stock Market, trading using live bots indicator screener and back tester using rest API and websocket 😊
Stars: ✭ 131 (-6.43%)
Mutual labels:  websocket
Node Camera
Access and stream web camera in nodejs using opencv and websockets.
Stars: ✭ 135 (-3.57%)
Mutual labels:  websocket
Ember Phoenix
Phoenix Framework integration and tooling for Ember.js apps
Stars: ✭ 140 (+0%)
Mutual labels:  websocket
Websocketdemo
在Spring Boot中使用WebSocket,示例包括简单模式、STOMP模式消息、处理对方不在线情况、分布式WebSocket等。
Stars: ✭ 140 (+0%)
Mutual labels:  websocket
Actioncable Vue
A Vue plugin that makes integrating Rails Action Cable dead-easy.
Stars: ✭ 138 (-1.43%)
Mutual labels:  websocket

zenrpc: JSON-RPC 2.0 Server Implementation with SMD support

Go Report Card Build Status codecov GoDoc

zenrpc is a JSON-RPC 2.0 server library with Service Mapping Description support. It's built on top of go generate instead of reflection.

How to Use

Service is struct with RPC methods, service represents RPC namespace.

  1. Install zenrpc generator go get github.com/semrush/zenrpc/v2/zenrpc
  2. Import github.com/semrush/zenrpc/v2 into our code with rpc service.
  3. Add trailing comment //zenrpc to your service or embed zenrpc.Service into your service struct.
  4. Write your funcs almost as usual.
  5. Do not forget run go generate or zenrpc for magic

Accepted Method Signatures

func(Service) Method([args]) (<value>, <error>)
func(Service) Method([args]) <value>
func(Service) Method([args]) <error>
func(Service) Method([args])
  • Value could be a pointer
  • Error is error or *zenrpc.Error

Example

package main

import (
	"flag"
	"context"
	"errors"
	"math"
	"log"
	"net/http"
	"os"	
	
	"github.com/semrush/zenrpc/v2"
	"github.com/semrush/zenrpc/v2/testdata"
)

type ArithService struct{ zenrpc.Service }

// Sum sums two digits and returns error with error code as result and IP from context.
func (as ArithService) Sum(ctx context.Context, a, b int) (bool, *zenrpc.Error) {
	r, _ := zenrpc.RequestFromContext(ctx)

	return true, zenrpc.NewStringError(a+b, r.Host)
}

// Multiply multiples two digits and returns result.
func (as ArithService) Multiply(a, b int) int {
	return a * b
}

type Quotient struct {
	Quo, Rem int
}

func (as ArithService) Divide(a, b int) (quo *Quotient, err error) {
	if b == 0 {
		return nil, errors.New("divide by zero")
	} else if b == 1 {
		return nil, zenrpc.NewError(401, errors.New("we do not serve 1"))
	}

	return &Quotient{
		Quo: a / b,
		Rem: a % b,
	}, nil
}

// Pow returns x**y, the base-x exponential of y. If Exp is not set then default value is 2.
//zenrpc:exp=2
func (as ArithService) Pow(base float64, exp float64) float64 {
	return math.Pow(base, exp)
}

//go:generate zenrpc

func main() {
	addr := flag.String("addr", "localhost:9999", "listen address")
	flag.Parse()

	rpc := zenrpc.NewServer(zenrpc.Options{ExposeSMD: true})
	rpc.Register("arith", testdata.ArithService{})
	rpc.Register("", testdata.ArithService{}) // public
	rpc.Use(zenrpc.Logger(log.New(os.Stderr, "", log.LstdFlags)))

	http.Handle("/", rpc)

	log.Printf("starting arithsrv on %s", *addr)
	log.Fatal(http.ListenAndServe(*addr, nil))
}

Magic comments

All comments are optional.

Method comments
//zenrpc:<method parameter>[=<default value>][whitespaces<description>]
//zenrpc:<error code>[whitespaces<description>]
//zenrpc:return[whitespaces<description>]
 
Struct comments
type MyService struct {} //zenrpc

Need to browse your api and do some test api calls?

We recommend to use SMDBox. It is Swagger-like JSON RPC API browser, compatible with smd scheme, generated by zenrpc.

JSON-RPC 2.0 Supported Features

  • [x] Requests
    • [x] Single requests
    • [x] Batch requests
    • [x] Notifications
  • [x] Parameters
    • [x] Named
    • [x] Position
    • [x] Default values
  • [x] SMD Schema
    • [x] Input
    • [x] Output
    • [x] Codes
    • [ ] Scopes for OAuth

Server Library Features

  • [x] go generate
  • [ ] Transports
    • [x] HTTP
    • [x] WebSocket
    • [ ] RabbitMQ
  • [x] Server middleware
    • [x] Basic support
    • [x] Metrics
    • [x] Logging
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].