Skip to content

Commit

Permalink
Merge branch 'go-sdk'
Browse files Browse the repository at this point in the history
  • Loading branch information
walleliu1016 committed Jun 6, 2022
2 parents 755df48 + 3d547bd commit 5b85819
Show file tree
Hide file tree
Showing 96 changed files with 9,089 additions and 0 deletions.
6 changes: 6 additions & 0 deletions eventmesh-sdk-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
EventMesh Go SDK
---
support api
1. gRPC
2. HTTP
3. TCP
48 changes: 48 additions & 0 deletions eventmesh-sdk-go/common/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

var Constants = struct {
LANGUAGE_GO string
HTTP_PROTOCOL_PREFIX string
HTTPS_PROTOCOL_PREFIX string
PROTOCOL_TYPE string
PROTOCOL_VERSION string
PROTOCOL_DESC string
DEFAULT_HTTP_TIME_OUT int64
EVENTMESH_MESSAGE_CONST_TTL string

// Client heartbeat interval
HEARTBEAT int64

// Protocol type
CLOUD_EVENTS_PROTOCOL_NAME string
EM_MESSAGE_PROTOCOL_NAME string
OPEN_MESSAGE_PROTOCOL_NAME string
}{
LANGUAGE_GO: "GO",
HTTP_PROTOCOL_PREFIX: "http://",
HTTPS_PROTOCOL_PREFIX: "https://",
PROTOCOL_TYPE: "protocoltype",
PROTOCOL_VERSION: "protocolversion",
PROTOCOL_DESC: "protocoldesc",
DEFAULT_HTTP_TIME_OUT: 15000,
EVENTMESH_MESSAGE_CONST_TTL: "ttl",
HEARTBEAT: 30 * 1000,
CLOUD_EVENTS_PROTOCOL_NAME: "cloudevents",
EM_MESSAGE_PROTOCOL_NAME: "eventmeshmessage",
OPEN_MESSAGE_PROTOCOL_NAME: "openmessage",
}
22 changes: 22 additions & 0 deletions eventmesh-sdk-go/common/id/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package id

// Interface api to generate uniq id
type Interface interface {
// Next create uniq ID
Next() string
}
79 changes: 79 additions & 0 deletions eventmesh-sdk-go/common/id/id_snake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package id

import (
"bytes"
"fmt"
"net"
"strconv"
"strings"

"github.com/sony/sonyflake"
)

// flake generate uid by flake
type flake struct {
sf *sonyflake.Sonyflake
}

func NewFlake() Interface {
macAddr := getMacAddr()
st := sonyflake.Settings{
MachineID: func() (uint16, error) {
ma := strings.Split(macAddr, ":")
mid, err := strconv.ParseInt(ma[0]+ma[1], 16, 16)
return uint16(mid), err
},
}
return &flake{
sf: sonyflake.NewSonyflake(st),
}
}

// getMacAddr return the current machine mac address
func getMacAddr() (addr string) {
interfaces, err := net.Interfaces()
if err == nil {
for _, i := range interfaces {
if i.Flags&net.FlagUp != 0 && bytes.Compare(i.HardwareAddr, nil) != 0 {
// Don't use random as we have a real address
addr = i.HardwareAddr.String()
break
}
}
}
return
}

// Nextv generates next id as an uint64
func (f *flake) Nextv() (id uint64, err error) {
var i uint64
if f.sf != nil {
i, err = f.sf.NextID()
if err == nil {
id = i
}
}
return
}

// Next generates next id as a string
func (f *flake) Next() string {
var i uint64
i, _ = f.Nextv()
return fmt.Sprintf("%d", i)
}
34 changes: 34 additions & 0 deletions eventmesh-sdk-go/common/id/id_uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package id

import (
"github.com/google/uuid"
"strings"
)

// UUID generate id by uuid
type UUID struct {
}

// NewUUID uuid instance
func NewUUID() Interface {
return &UUID{}
}

func (u *UUID) Next() string {
return strings.ReplaceAll(uuid.New().String(), "-", "")
}
24 changes: 24 additions & 0 deletions eventmesh-sdk-go/common/protocol/http/body/body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package body

type Body struct {
ToMap map[string]interface{}
}

func (b *Body) BuildBody(requestCode string, originalMap map[string]interface{}) *Body {
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package client

import (
"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/protocol/http/body"
)

var HeartbeatRequestBodyKey = struct {
CLIENTTYPE string
CONSUMERGROUP string
HEARTBEATENTITIES string
}{
CLIENTTYPE: "clientType",
HEARTBEATENTITIES: "heartbeatEntities",
CONSUMERGROUP: "consumerGroup",
}

type HeartbeatEntity struct {
Topic string `json:"topic"`
Url string `json:"url"`
ServiceId string `json:"serviceId"`
InstanceId string `json:"instanceId"`
}

type HeartbeatRequestBody struct {
body.Body
consumerGroup string
clientType string
heartbeatEntities string
}

func (h *HeartbeatRequestBody) ConsumerGroup() string {
return h.consumerGroup
}

func (h *HeartbeatRequestBody) SetConsumerGroup(consumerGroup string) {
h.consumerGroup = consumerGroup
}

func (h *HeartbeatRequestBody) ClientType() string {
return h.clientType
}

func (h *HeartbeatRequestBody) SetClientType(clientType string) {
h.clientType = clientType
}

func (h *HeartbeatRequestBody) HeartbeatEntities() string {
return h.heartbeatEntities
}

func (h *HeartbeatRequestBody) SetHeartbeatEntities(heartbeatEntities string) {
h.heartbeatEntities = heartbeatEntities
}

func (h *HeartbeatRequestBody) BuildBody(bodyParam map[string]interface{}) *HeartbeatRequestBody {
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package client

import (
"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/protocol"
"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/protocol/http/body"
)

var SubscribeRequestBodyKey = struct {
TOPIC string
URL string
CONSUMERGROUP string
}{
TOPIC: "topic",
URL: "url",
CONSUMERGROUP: "consumerGroup",
}

type SubscribeRequestBody struct {
body.Body
topics []protocol.SubscriptionItem
url string
consumerGroup string
}
35 changes: 35 additions & 0 deletions eventmesh-sdk-go/common/protocol/http/common/client_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

type ClientType struct {
Type int `json:"type"`
Desc string `json:"desc"`
}

var DefaultClientType = struct {
PUB ClientType
SUB ClientType
}{
PUB: ClientType{
Type: 1,
Desc: "Client for publishing",
},
SUB: ClientType{
Type: 2,
Desc: "Client for subscribing",
},
}
27 changes: 27 additions & 0 deletions eventmesh-sdk-go/common/protocol/http/common/eventmesh_ret_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package common

type EventMeshRetCode struct {
RetCode int `json:"retCode"`
ErrMsg string `json:"errMsg"`
}

var DefaultEventMeshRetCode = struct {
SUCCESS EventMeshRetCode
}{
SUCCESS: EventMeshRetCode{RetCode: 0, ErrMsg: "success"},
}
Loading

0 comments on commit 5b85819

Please sign in to comment.