Skip to content

Commit

Permalink
feat:add README
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed Jun 2, 2024
1 parent 8bd79f0 commit a8e3154
Show file tree
Hide file tree
Showing 28 changed files with 3,101 additions and 400 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

- name: Init MySQL
run: |
go run ./cmd/gateway/main.go init -e dev
go run ./cmd/begonia init -e dev
cat ~/.begonia/admin-app.json
- name: Type a temporary tag
Expand Down
9 changes: 6 additions & 3 deletions Makefile
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
78 changes: 72 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,77 @@
<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
## Support

- Compatible with all `gRPC-gateway` functionality features.
- All gRPC request methods and their parameter formats can be requested and forwarded through HTTP 1.1.
- Forwards gRPC bi-directional streaming service requests based on WebSockets.
- Forwards server-side streaming responses based on the SSE (Server-Side-Event) protocol.
- Forwards gRPC client streaming requests based on a custom `application/begonia-client-stream` request type.
- Allows requests with `application/x-www-form-urlencoded` and `multipart/form-data` parameter formats.
- Rich built-in middleware, such as APIKEY verification, AKSK verification, and `go-playground/validator` for parameter validation middleware.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
```
82 changes: 82 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<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`功能特性
- 所有的gRPC请求方式及其参数格式都能够通过HTTP 1.1进行请求和转发
- 基于websocket转发gRPC双向流式服务请求
- 基于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
```
115 changes: 115 additions & 0 deletions cmd/begonia/endpoint.go
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#####################")
}
Loading

0 comments on commit a8e3154

Please sign in to comment.