prelude

package module
v0.0.0-...-e8158db Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 14 Imported by: 0

README

Prelude

A simple lightweight network framework for Go. It is useful for long connection applications such as IOT, Chatroom, Online Game, Instant Messaging

Feature

  1. Websocket is supported (TCP, MQTT maybe later)
  2. distributed architecture and can be scale out
  3. handle 1 million connections
  4. use the CloudEvents 1.0 specification as event format
  5. support JSON, XML, ProtoBuf as content type
  6. Golang style

Roadmap

  1. support middleware chain

Installation

go get -u github.com/nite-coder/prelude

Example

Server
func main() {
	opts := hubNATS.HubOptions{
		URL:   "nats://nats:4222",
		Group: "gateway",
	}

    // we use nats as mq
	hub, err := hubNATS.NewNatsHub(opts)
	if err != nil {
		panic(err)
	}

	router := prelude.NewRouter(hub)
	router.AddRoute("ping", func(c *prelude.Context) error {
		// handle ping command here
		return c.JSON("pong", "hello world") // the event will send back to client
	})

	websocketGateway := websocket.NewGateway()
	err = websocketGateway.ListenAndServe(":10080", hub)
	if err != nil {
		log.Err(err).Error("main: websocket gateway start failed")
	}
}

Client

func main() {
	ws, _, err := websocket.DefaultDialer.Dial("ws://127.0.0.1:10080", nil)
	if err != nil {
		t.Fatalf("%v", err)
	}
	defer ws.Close()

	pingEvent := cloudevents.NewEvent()
	pingEvent.SetID(uuid.NewString())
	pingEvent.SetSource("client")
	pingEvent.SetType("ping")

	ws.WriteJSON(pingEvent)
}

Documentation

Overview

Package prelude is a generated GoMock package.

Index

Constants

View Source
const (
	SessionID = "sessionid"
)

Variables

View Source
var (
	ErrInvalidEventType = errors.New("prelude: eventType can't be empty")
)

Functions

This section is empty.

Types

type Context

type Context struct {
	Event cloudevents.Event
	// contains filtered or unexported fields
}

func NewContext

func NewContext(hub Huber, event cloudevents.Event) *Context

func (*Context) BindJSON

func (c *Context) BindJSON(obj interface{}) error

func (*Context) Get

func (c *Context) Get(key string) interface{}

func (*Context) JSON

func (c *Context) JSON(eventType string, obj interface{}, sessionIDs ...string) error

func (*Context) ProtoBuf

func (c *Context) ProtoBuf(eventType string, msg proto.Message, sessionIDs ...string) error

func (*Context) SenderSessionID

func (c *Context) SenderSessionID() string

func (*Context) Set

func (c *Context) Set(key string, val interface{}) error

func (*Context) Write

func (c *Context) Write(eventType string, contentType string, bytes []byte, sessionIDs ...string) error

func (*Context) XML

func (c *Context) XML(eventType string, obj interface{}, sessionIDs ...string) error

type Gatewayer

type Gatewayer interface {
	ListenAndServe(bind string, hub Huber) error
	Shutdown(ctx context.Context) error
}

Gatewayer handles all communications between client and server

type HandlerFunc

type HandlerFunc func(c *Context) error

HandlerFunc defines a function to server HTTP requests

type Huber

type Huber interface {
	Router() *Router
	SetRouter(router *Router)
	Publish(topic string, event cloudevents.Event) error
	QueueSubscribe(topic string) error
}

type Item

type Item struct {
	Key   string
	Value interface{}
}

type MockHuber

type MockHuber struct {
	// contains filtered or unexported fields
}

MockHuber is a mock of Huber interface.

func NewMockHuber

func NewMockHuber(ctrl *gomock.Controller) *MockHuber

NewMockHuber creates a new mock instance.

func (*MockHuber) EXPECT

func (m *MockHuber) EXPECT() *MockHuberMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockHuber) Publish

func (m *MockHuber) Publish(topic string, event v2.Event) error

Publish mocks base method.

func (*MockHuber) QueueSubscribe

func (m *MockHuber) QueueSubscribe(topic string) error

QueueSubscribe mocks base method.

func (*MockHuber) Router

func (m *MockHuber) Router() *Router

Router mocks base method.

func (*MockHuber) SetRouter

func (m *MockHuber) SetRouter(router *Router)

SetRouter mocks base method.

type MockHuberMockRecorder

type MockHuberMockRecorder struct {
	// contains filtered or unexported fields
}

MockHuberMockRecorder is the mock recorder for MockHuber.

func (*MockHuberMockRecorder) Publish

func (mr *MockHuberMockRecorder) Publish(topic, event interface{}) *gomock.Call

Publish indicates an expected call of Publish.

func (*MockHuberMockRecorder) QueueSubscribe

func (mr *MockHuberMockRecorder) QueueSubscribe(topic interface{}) *gomock.Call

QueueSubscribe indicates an expected call of QueueSubscribe.

func (*MockHuberMockRecorder) Router

func (mr *MockHuberMockRecorder) Router() *gomock.Call

Router indicates an expected call of Router.

func (*MockHuberMockRecorder) SetRouter

func (mr *MockHuberMockRecorder) SetRouter(router interface{}) *gomock.Call

SetRouter indicates an expected call of SetRouter.

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter(name string, hub Huber) *Router

NewRouter function will create a new router instance

func (*Router) AddRoute

func (r *Router) AddRoute(action string, handler HandlerFunc)

AddRoute function which adding action and handler to router

func (*Router) Find

func (r *Router) Find(path string) HandlerFunc

Find returns http handler for specific path

Directories

Path Synopsis
examples
gateway
hub

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL