-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from begonia-org/feature/feat-grpc-proxy
eat:add README
- Loading branch information
Showing
28 changed files
with
3,111 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
OUTPUT_DIR=./bin | ||
GO_BIN_DIR := $(shell go env GOPATH)/bin | ||
version=$(shell git describe --tags --abbrev=0) | ||
commit=$(shell git rev-parse --short HEAD) | ||
build_time=$(shell date '+%Y%m%d%H%M%S') | ||
build: | ||
go build -ldflags -X=github.com/begonia-org/begonia.Version=$(version)\ -X=github.com/begonia-org/begonia.BuildTime=$(build_time)\ -X=github.com/begonia-org/begonia.Commit=$(commit) -o $(OUTPUT_DIR)/begonia cmd/gateway/main.go | ||
|
||
.DEFAULT_GOAL := build | ||
go build -ldflags -X=github.com/begonia-org/begonia.Version=$(version)\ -X=github.com/begonia-org/begonia.BuildTime=$(build_time)\ -X=github.com/begonia-org/begonia.Commit=$(commit) -o $(OUTPUT_DIR)/begonia cmd/begonia/*.go | ||
install: | ||
go install -ldflags -X=github.com/begonia-org/begonia.Version=$(version)\ -X=github.com/begonia-org/begonia.BuildTime=$(build_time)\ -X=github.com/begonia-org/begonia.Commit=$(commit) cmd/begonia/*.go | ||
all: build install | ||
.DEFAULT_GOAL := all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,81 @@ | ||
<div align="center"> | ||
<h1>Begonia</h1> | ||
<h1 align="center">Begonia</h1> | ||
<center> | ||
|
||
[![Go Report Card](https://goreportcard.com/badge/github.com/begonia-org/begonia)](https://goreportcard.com/report/github.com/begonia-org/begonia) | ||
[![codecov](https://codecov.io/github/begonia-org/begonia/graph/badge.svg?token=VGGAA5A87B)](https://codecov.io/github/begonia-org/begonia) | ||
|
||
</center> | ||
|
||
<center> | ||
|
||
[English](README.md) | [中文](README_ZH.md) | ||
|
||
</center> | ||
<p> | ||
A gateway service that reverse proxies HTTP requests to gRPC services. | ||
Begonia is an HTTP to gRPC reverse proxy server, which registers services defined by `gRPC-gateway` to the gateway based on the descriptor_set_out generated by protoc, thereby implementing reverse proxy functionality. The HTTP service follows the RESTful standard to handle HTTP requests and forwards RESTful requests to gRPC services. | ||
</p> | ||
</div> | ||
|
||
# About | ||
# Features | ||
- Compatible with all `gRPC-gateway` feature functionalities | ||
|
||
- Supports converting all HTTP request methods and parameter formats into gRPC requests and data formats | ||
|
||
- Supports converting gRPC's bi-directional streaming transport into websocket protocol for request handling | ||
|
||
- Supports converting server-side streaming transport into SSE (Server-Side-Event) protocol for request handling | ||
|
||
- Based on custom `application/begonia-client-stream` request type to forward gRPC client streaming requests | ||
|
||
- Supports requests with `application/x-www-form-urlencoded` and `multipart/form-data` parameter formats | ||
|
||
- Rich built-in middleware, such as APIKEY verification, AKSK verification, `go-playground/validator` parameter verification middleware | ||
|
||
# Getting Started | ||
|
||
### Installation | ||
```bash | ||
git clone https://github.com/begonia-org/begonia.git | ||
``` | ||
|
||
```bash | ||
cd begonia && make install | ||
``` | ||
|
||
### Define proto | ||
Refer to [example/example.proto](example/example.proto) | ||
|
||
### Generate Descriptor Set | ||
|
||
```shell | ||
protoc --descriptor_set_out=example.pb --include_imports --proto_path=./ example.proto | ||
``` | ||
|
||
### Start the Gateway Service | ||
|
||
#### 1. Build the runtime environment | ||
|
||
```bash | ||
docker compose up -d | ||
``` | ||
|
||
#### 2. Initialize the database | ||
|
||
```bash | ||
begonia init -e dev | ||
``` | ||
|
||
#### 3. Start the service | ||
|
||
```bash | ||
begonia start -e dev | ||
``` | ||
|
||
Begonia is an HTTP to gRPC reverse proxy server that registers service routes to the gateway based on descriptor_set_out generated by protoc, thus implementing reverse proxy functionality. The HTTP service adheres to RESTful standards to handle HTTP requests and forwards RESTful requests to gRPC services | ||
#### 4. Register the service | ||
```bash | ||
go run . endpoint add -n "example" -d /data/work/begonia-org/begonia/example/example.pb -p 127.0.0.1:1949 -p 127.0.0.1:2024 | ||
``` | ||
|
||
#### 5. Test request service | ||
``` | ||
curl -vvv http://127.0.0.1:12138/api/v1/example/hello | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<div > | ||
<h1 align="center">Begonia</h1> | ||
<center> | ||
|
||
[![Go Report Card](https://goreportcard.com/badge/github.com/begonia-org/begonia)](https://goreportcard.com/report/github.com/begonia-org/begonia) | ||
[![codecov](https://codecov.io/github/begonia-org/begonia/graph/badge.svg?token=VGGAA5A87B)](https://codecov.io/github/begonia-org/begonia) | ||
|
||
</center> | ||
|
||
<center> | ||
|
||
[English](README.md) | [中文](README_ZH.md) | ||
|
||
</center> | ||
<p> | ||
Begonia 是一个 HTTP 到 gRPC 的反向代理服务器,它基于 protoc 生成的 descriptor_set_out 注册由`gRPC-gateway`定义的服务路由到网关,从而实现反向代理功能。HTTP 服务遵循 RESTful 标准来处理 HTTP 请求,并将 RESTful 请求转发到 gRPC 服务。 | ||
</p> | ||
</div> | ||
|
||
# 特性 | ||
|
||
- 兼容所有的`gRPC-gateway`功能特性 | ||
|
||
- 支持所有 HTTP 请求方式和参数格式转换为 gRPC 请求和数据格式 | ||
|
||
- 支持将 gRPC 的双向流式传输转换为 websocket 协议进行请求处理 | ||
|
||
- 支持将服务端流式传输转换为 SSE(Server-Side-Event)协议进行请求处理 | ||
|
||
- 基于自定义的`application/begonia-client-stream` 请求类型转发 gRPC 的客户端流式请求 | ||
|
||
- 支持`application/x-www-form-urlencoded`和`multipart/form-data`参数格式的请求 | ||
|
||
- 丰富的内置中间件,例如 APIKEY 校验、AKSK 校验,`go-playground/validator`参数校验中间件 | ||
|
||
# 开始 | ||
|
||
### 安装 | ||
|
||
```bash | ||
git clone https://github.com/begonia-org/begonia.git | ||
``` | ||
|
||
```bash | ||
cd begonia && make install | ||
``` | ||
|
||
### 定义 proto | ||
|
||
参考[example/example.proto](example/example.proto) | ||
|
||
### 生成 Descriptor Set | ||
|
||
```shell | ||
protoc --descriptor_set_out=example.pb --include_imports --proto_path=./ example.proto | ||
``` | ||
|
||
### 启动网关服务 | ||
|
||
#### 1、构建运行环境 | ||
|
||
```bash | ||
docker compose up -d | ||
``` | ||
|
||
#### 2、初始化数据库 | ||
|
||
```bash | ||
begonia init -e dev | ||
``` | ||
|
||
#### 3、启动服务 | ||
|
||
```bash | ||
begonia start -e dev | ||
``` | ||
|
||
#### 4、注册服务 | ||
|
||
```bash | ||
go run . endpoint add -n "example" -d /data/work/begonia-org/begonia/example/example.pb -p 127.0.0.1:1949 -p 127.0.0.1:2024 | ||
``` | ||
|
||
#### 5、测试请求服务 | ||
|
||
``` | ||
curl -vvv http://127.0.0.1:12138/api/v1/example/hello | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
goloadbalancer "github.com/begonia-org/go-loadbalancer" | ||
api "github.com/begonia-org/go-sdk/api/app/v1" | ||
endpoint "github.com/begonia-org/go-sdk/api/endpoint/v1" | ||
"github.com/begonia-org/go-sdk/client" | ||
) | ||
|
||
var accessKey string | ||
var secret string | ||
var addr string | ||
|
||
func readInitAPP() { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
return | ||
} | ||
path := filepath.Join(homeDir, ".begonia") | ||
path = filepath.Join(path, "admin-app.json") | ||
file, err := os.Open(path) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
return | ||
|
||
} | ||
defer file.Close() | ||
|
||
decoder := json.NewDecoder(file) | ||
app := &api.Apps{} | ||
err = decoder.Decode(app) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
return | ||
|
||
} | ||
accessKey = app.AccessKey | ||
secret = app.Secret | ||
path2 := filepath.Join(homeDir, ".begonia") | ||
path2 = filepath.Join(path2, "gateway.json") | ||
gwFile, err := os.Open(path2) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
return | ||
|
||
} | ||
defer gwFile.Close() | ||
gwDecoder := json.NewDecoder(gwFile) | ||
gw := &struct { | ||
Addr string `json:"addr"` | ||
}{} | ||
err = gwDecoder.Decode(gw) | ||
if err != nil { | ||
log.Fatalf(fmt.Sprintf("read gateway file error:%s", err.Error())) | ||
return | ||
|
||
} | ||
addr = gw.Addr | ||
|
||
} | ||
func RegisterEndpoint(name string, endpoints []string, pbFile string, opts ...client.EndpointOption) { | ||
readInitAPP() | ||
pb, err := os.ReadFile(pbFile) | ||
if err != nil { | ||
panic(err) | ||
|
||
} | ||
apiClient := client.NewEndpointAPI(addr, accessKey, secret) | ||
meta := make([]*endpoint.EndpointMeta, 0) | ||
for i, v := range endpoints { | ||
meta = append(meta, &endpoint.EndpointMeta{ | ||
Addr: v, | ||
Weight: int32(i), | ||
}) | ||
} | ||
endpoint := &endpoint.EndpointSrvConfig{ | ||
DescriptorSet: pb, | ||
Name: name, | ||
ServiceName: name, | ||
Description: name, | ||
Balance: string(goloadbalancer.RRBalanceType), | ||
Endpoints: meta, | ||
Tags: make([]string, 0), | ||
} | ||
for _, opt := range opts { | ||
opt(endpoint) | ||
|
||
} | ||
rsp, err := apiClient.PostEndpointConfig(context.Background(), endpoint) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
panic(err.Error()) | ||
} | ||
log.Printf("#####################Add Endpoint Success#####################") | ||
log.Printf("#####################ID:%s####################################", rsp.Id) | ||
} | ||
func DeleteEndpoint(id string) { | ||
readInitAPP() | ||
apiClient := client.NewEndpointAPI(addr, accessKey, secret) | ||
log.Printf("#####################Delete Endpoint:%s#####################", id) | ||
_, err := apiClient.DeleteEndpointConfig(context.Background(), id) | ||
if err != nil { | ||
log.Printf("delete err %v ", err) | ||
panic(err.Error()) | ||
} | ||
log.Printf("#####################Delete Endpoint Success#####################") | ||
} |
Oops, something went wrong.