• Stars
    star
    120
  • Rank 285,534 (Top 6 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

💻 pbgo is a mini RPC/REST/Grpc-Gateway framework based on Protobuf

pbgo: mini rpc/rest framework based on Protobuf

Build Status Go Report Card GoDoc

Features

  • protoc plugin generate code
  • define rpc api with Protobuf, generate stub code, work with standard net/rpc framework
  • define rest api with Protobuf, and generate service and client code
  • work with grpc framework

Install

  1. install protoc at first: http://github.com/google/protobuf/releases
  2. go get github.com/chai2010/pbgo
  3. go get github.com/chai2010/pbgo/protoc-gen-pbgo
  4. go run hello.go

Example (net/rpc)

create proto file:

syntax = "proto3";
package hello_pb;

message String {
	string value = 1;
}
message Message {
	string value = 1;
}

service HelloService {
	rpc Hello (String) returns (String);
	rpc Echo (Message) returns (Message);
}

generate rpc code:

$ protoc -I=. -I=$GOPATH/src --pbgo_out=. hello.proto

use generate code:

package main

import (
	"fmt"
	"log"
	"net"
	"net/rpc"

	"github.com/chai2010/pbgo/examples/hello.pb"
)

type HelloService struct{}

func (p *HelloService) Hello(request *hello_pb.String, reply *hello_pb.String) error {
	reply.Value = "hello:" + request.GetValue()
	return nil
}
func (p *HelloService) Echo(request *hello_pb.Message, reply *hello_pb.Message) error {
	*reply = *request
	return nil
}

func startRpcServer() {
	hello_pb.RegisterHelloService(rpc.DefaultServer, new(HelloService))

	listener, err := net.Listen("tcp", ":1234")
	if err != nil {
		log.Fatal("ListenTCP error:", err)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Fatal("Accept error:", err)
		}

		go rpc.ServeConn(conn)
	}
}

func tryRpcClient() {
	client, err := hello_pb.DialHelloService("tcp", "localhost:1234")
	if err != nil {
		log.Fatal(err)
	}

	reply, err := client.Hello(&hello_pb.String{Value: "gopher"})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(reply.GetValue())
}

func main() {
	go startRpcServer()
	tryRpcClient()
}

Example (rest api)

create proto file:

syntax = "proto3";
package hello_pb;

import "github.com/chai2010/pbgo/pbgo.proto";

message String {
	string value = 1;
}

message Message {
	string value = 1;
	repeated int32 array = 2;
	map<string,string> dict = 3;
	String subfiled = 4;
}

message StaticFile {
	string content_type = 1;
	bytes content_body = 2;
}

service HelloService {
	rpc Hello (String) returns (String) {
		option (pbgo.rest_api) = {
			get: "/hello/:value"
			post: "/hello"

			additional_bindings {
				method: "DELETE"; url: "/hello"
			}
			additional_bindings {
				method: "PATCH"; url: "/hello"
			}
		};
	}
	rpc Echo (Message) returns (Message) {
		option (pbgo.rest_api) = {
			get: "/echo/:subfiled.value"
		};
	}
	rpc Static(String) returns (StaticFile) {
		option (pbgo.rest_api) = {
			additional_bindings {
				method: "GET";
				url: "/static/:value";
				content_type: ":content_type";
				content_body: ":content_body"
			}
		};
	}
}

generate rpc/rest code:

$ protoc -I=. -I=$GOPATH/src --pbgo_out=. hello.proto

use generate code:

package main

import (
	"io/ioutil"
	"log"
	"mime"
	"net/http"
	"time"

	"github.com/chai2010/pbgo/examples/hello.pb"
)

type HelloService struct{}

func (p *HelloService) Hello(request *hello_pb.String, reply *hello_pb.String) error {
	reply.Value = "hello:" + request.GetValue()
	return nil
}
func (p *HelloService) Echo(request *hello_pb.Message, reply *hello_pb.Message) error {
	*reply = *request
	return nil
}
func (p *HelloService) Static(request *hello_pb.String, reply *hello_pb.StaticFile) error {
	data, err := ioutil.ReadFile("./testdata/" + request.Value)
	if err != nil {
		return err
	}

	reply.ContentType = mime.TypeByExtension(request.Value)
	reply.ContentBody = data
	return nil
}

func someMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(wr http.ResponseWriter, r *http.Request) {
		timeStart := time.Now()
		defer func() {
			timeElapsed := time.Since(timeStart)
			log.Println(r.Method, r.URL, timeElapsed)
		}()

		next.ServeHTTP(wr, r)
	})
}

func main() {
	router := hello_pb.HelloServiceHandler(new(HelloService))
	log.Fatal(http.ListenAndServe(":8080", someMiddleware(router)))
}

Go client:

func main() {
	var reply hello_pb.Message
	err := pbgo.HttpDo("GET", "http://127.0.0.1:8080/echo/xx",
		&hello_pb.Message{
			Value: "chai2010",
			Array: []int32{1, 2, 3},
		},
		&reply,
	)
	if err != nil {
		println("aaa")
		log.Fatal(err)
	}

	// {chai2010 [1 2 3] map[] value:"xx"  {} [] 0}
	fmt.Println(reply)
}

CURL client:

$ curl localhost:8080/hello/gopher
{"value":"hello:gopher"}
$ curl "localhost:8080/hello/gopher?value=vgo"
{"value":"hello:vgo"}
$ curl localhost:8080/hello -X POST --data '{"value":"cgo"}'
{"value":"hello:cgo"}

$ curl localhost:8080/echo/gopher
{"subfiled":{"value":"gopher"}}
$ curl "localhost:8080/echo/gopher?array=123&array=456"
{"array":[123,456],"subfiled":{"value":"gopher"}}
$ curl "localhost:8080/echo/gopher?dict%5Babc%5D=123"
{"dict":{"abc":"123"},"subfiled":{"value":"gopher"}}

$ curl localhost:8080/static/gopher.png
$ curl localhost:8080/static/hello.txt

Rest Client

https://github.com/chubin/wttr.in

$ curl http://wttr.in/wuhan?format=j1
$ curl http://wttr.in/武汉?format=j1
func main() {
	var reply struct {
		CurrentCondition []struct {
			FeelsLikeC       string `json:"FeelsLikeC"`
			FeelsLikeF       string `json:"FeelsLikeF"`
			Cloudcover       string `json:"cloudcover"`
			Humidity         string `json:"humidity"`
			LocalObsDateTime string `json:"localObsDateTime"`
			Observation_time string `json:"observation_time"`
		} `json:"current_condition"`
	}

	err := pbgo.HttpGet("http://wttr.in/wuhan?format=j1", nil, &reply)
	if err != nil {
		log.Fatal(err)
	}

	json, err := json.MarshalIndent(reply, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(json))
}

Form Example

Create example/form_pb/comment.proto:

syntax = "proto3";
package form_pb;

message Comment {
	string user = 1;
	string email = 2;
	string url = 3;
	string text = 4;
	string next = 5; // redirect
}

generate proto code:

$ protoc -I=. --go_out=. comment.proto

create web server:

package main

import (
	"github.com/chai2010/pbgo"
	"github.com/chai2010/pbgo/examples/form_pb"
	"github.com/julienschmidt/httprouter"
)

func main() {
	router := httprouter.New()

	// http://localhost:8080
	router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		w.Header().Add("Content-Type", "text/html; charset=utf-8")

		fmt.Fprint(w, `
			<form method="POST" action="/api/comment">
				<input type="text" name="user" value="user name">
				<input type="email" name="email" value="[email protected]">
				<input name="url" value="http://chai2010.cn"></textarea>
				<textarea name="text" value="text">www</textarea>
				<input type="text" name="next" value="/thanks">
				<button type="submit">Send Test</button>
			</form>
		`)
	})

	router.GET("/thanks", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		fmt.Fprintln(w, "Thanks")
	})

	// curl -d "user=chai&[email protected]&text=hello&next=http://github.com" localhost:8080/api/comment
	router.POST("/api/comment", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		var form form_pb.Comment
		if err := pbgo.BindForm(r, &form); err != nil {
			http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
			log.Println("err:", err)
			return
		}

		if form.Next != "" {
			http.Redirect(w, r, form.Next, http.StatusFound)
		}

		log.Println("form:", form)
	})

	log.Fatal(http.ListenAndServe(":8080", router))
}
$ curl -d "user=chai&[email protected]&text=hello&next=http://github.com" localhost:8080/api/comment-form
{"user":"chai","email":"[email protected]","text":"hello","next":"http://github.com"}

gRPC & Rest Example

See https://github.com/chai2010/pbgo-grpc

BUGS

Report bugs to [email protected].

Thanks!

More Repositories

1

advanced-go-programming-book

📚 《Go语言高级编程》开源图书,涵盖CGO、Go汇编语言、RPC实现、Protobuf插件实现、Web框架实现、分布式系统等高阶主题(完稿)
Handlebars
18,653
star
2

go-ast-book

📚 《Go语言定制指南》(原名:Go语法树入门/开源免费图书/Go语言进阶/掌握抽象语法树/Go语言AST)
Go
5,233
star
3

awesome-wasm-zh

WebAssembly(wasm)资源精选
WebAssembly
976
star
4

webp

WebP decoder and encoder for Go (Zero Dependencies).
Go
447
star
5

gopherchina2018-cgo-talk

📖 GopherChina2018: 深入CGO编程 - 最新修订
JavaScript
330
star
6

ptyy

莆田医院: 莆田系 野鸡医院 查询 (iOS/Swift/Go/Ruby/)
Go
153
star
7

opencv

Go bindings for OpenCV1.1 (Dev/Zero Dependencies).
C++
116
star
8

guetzli-go

Guetzli perceptual JPEG encoder for Go (Zero Dependencies).
Go
86
star
9

cgo

golang cgo helper
Go
82
star
10

gettext-go

🆎 GNU gettext for Go (Imported By Kubernetes)
Go
82
star
11

tinylang

Tiny玩具语言(Go语言实现/包含Tiny编译器/CASL汇编器/COMET虚拟机/调试器/支持WebAssembly/LLVM)
Go
66
star
12

protorpc

prtorpc(2013-2021): Google Protocol Protobufs RPC for Go.
Go
61
star
13

notepadplus-go

notepad++ support Go language and Go assembly language (Windows).
55
star
14

wasm-book-code

WebAssembly标准入门 代码
JavaScript
54
star
15

tiff

Rich TIFF/BigTIFF/GeoTIFF decoder/encoder for Go (Pure Go/Zero Dependencies)
Go
50
star
16

errors

Go errors process, support error code, caller info, json format and wraped error.
Go
40
star
17

gotlang

🐶🐶🐶 狗头语言(gotlang) 🐶🐶🐶
Go
38
star
18

base60

base60: 天干地支编码: 乙丑癸巳甲寅己亥丁卯甲申丁未甲午己巳
Go
35
star
19

notepadplus-protobuf

notepad++ support Protobuf (Windows).
32
star
20

libfacedetection-go

libfacedetection binding for Go
C++
32
star
21

guetzli-js

Guetzli for NodeJS/Browser
C++
28
star
22

calculator

基于flex&goyacc实现的计算器
Go
28
star
23

leetcode-go

💻LeetCode练习, Go语言版本
Go
24
star
24

coder-books

📚 码农相关的经典图书收集
21
star
25

bpg

BPG decoder for Go (Zero Dependencies).
Go
20
star
26

ugo-rs

µGo编程语言(Rust版本) - 试验场
Rust
20
star
27

qrcode

QR code encoder/decoder
Go
18
star
28

vscode-extdev-book

📚 《VS Code 插件开发》
Makefile
17
star
29

defer

Go's defer operator for C++
C++
16
star
30

notepadplus-typescript

notepad++ support TypeScript (Windows).
TypeScript
14
star
31

chai2010.github.io

📖 chai2010 的博客
JavaScript
13
star
32

asmgo-talk

Go汇编语言简介
JavaScript
12
star
33

awesome-cue-zh

📚 CUE配置语言资源精选(欢迎投稿)
Go
11
star
34

cache

LevelDB style LRU cache for Go, support non GC object.
Go
11
star
35

im4corner

Google输入法四角号码扩展
Lua
10
star
36

qml

Qt/QML for Golang (fork from go-qml/qml.v1), Qt/MSVC with Win32/Win64.
Go
10
star
37

protorpc.cxx

This package provides C++ Protobuf-RPC support.
C++
10
star
38

lucky-star

幸运之星: 公平公正的抽奖算法
Go
9
star
39

glua-strings

Go strings package for gopher-lua
Go
8
star
40

bzip2

bzip2 compression for Go
Go
8
star
41

ethutil

♦Ξ♦ 以太坊工具箱(零第三方库依赖) ♦Ξ♦
Go
7
star
42

mdbook-go

mdBook: Go语言迷你版本
CSS
6
star
43

logger

Package logger implements a simple level logging package.
Go
6
star
44

ditaa-docker

docker container for ditaa
Makefile
6
star
45

jsonv

Get Value from JSON
Go
6
star
46

wqy-microhei-go

Go语言文泉驿微米黑字体
Go
5
star
47

glua-helper

GopherLua helper function
Go
5
star
48

FGPTL

FUCK GPT License(GPT 禁止协议)
5
star
49

goapp-in-docker

Building Minimal Docker Containers for Go App
Go
5
star
50

chai2010

4
star
51

protorpc3-cxx

This package provides C++ Protobuf3-RPC support.
C++
4
star
52

wc

基于flex实现的wc命令, 单词计数
Go
4
star
53

jsonmap

Map for json/struct/key-value-db
Go
4
star
54

assert

Assert for Go1.9+
Go
4
star
55

builtin

Provides some usefull functions for Go.
Go
4
star
56

kusion-cookbook

📚 KusionStack云原生八卦
3
star
57

talks

my talk slides
CSS
3
star
58

pbgo-grpc

pbgo grpc/rest examples
Go
3
star
59

brainfuck

brainfuck VM
Go
3
star
60

spacestring

space string contains more than 1 space characters
Go
3
star
61

ethrpc

♦Ξ♦ 以太坊轻量级RPC客户端(开发中) ♦Ξ♦
Go
3
star
62

emscripten-talk

Emscripten简介
JavaScript
2
star
63

protoc

protoc wrap, support `go get`
C++
2
star
64

version

get go-app version
Go
2
star
65

ini

INI configuration file parser
Go
2
star
66

vimpinyin

Vim拼音输入法
Lua
2
star
67

cc-mini-test

C++ Mini UnitTest and Benchmark Library
C++
2
star
68

template

template helper
Go
2
star
69

ibox-docker

docker工具箱
Dockerfile
1
star
70

rust-base60

encodes and decodes base60 as bytes or utf8
Rust
1
star
71

ghutil

ghutil
Go
1
star
72

ArduinoMegaRTOS

FreeRTOS on Arduino Mega
C
1
star
73

lzfse-go

LZFSE decoder and encoder for Go (Zero Dependencies).
C
1
star
74

bench

Go
1
star
75

protoc-gen-grpc-plugins

1
star
76

xmc-builder

xmc-builder
Dockerfile
1
star
77

etcd-docker

etcd docker image
Makefile
1
star
78

etcdfs

(WIP)virtual file system on etcd
Go
1
star
79

image

Package image implements a basic 2-D image library.
Go
1
star
80

libconfd

mini confd lib, based on confd/memkv/secconf/logger
Go
1
star
81

static-public

静态数据,以github-pages发布
JavaScript
1
star