restlike

package
v4.7.4 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: BSD-2-Clause, BSD-2-Clause Imports: 5 Imported by: 7

README

Server Initialization

These steps must first be performed in order to begin creating server objects of any variety.

Make an xxdk.Cmix Object

The xxdk.Cmix object created here will be used for all types of xxdk.Identity and server initialization.

  1. Obtain the NDF
ndfJson, err := xxdk.DownloadAndVerifySignedNdfWithUrl(url, cert)
  1. If not done in previous runs, create a new xxdk.Cmix object in storage using ndfJson. storageDir and password may be customized.

Example:

err := xxdk.NewCmix(ndfJson, "/clientStorage", []byte("testPassword"), "")
  1. LoadCmix in order to obtain the xxdk.Cmix object. storageDir and password may be customized, but must match the values provided to NewCmix(). The result of xxdk.GetDefaultParams() may also be freely modified according to your needs.

Example:

client, err := xxdk.LoadCmix("/clientStorage", []byte("testPassword"), xxdk.GetDefaultParams())
  1. Start the network follower. Timeout may be modified as needed.

Example:

err := client.StartNetworkFollower(10*time.Second)
Make an xxdk.Identity Object

The xxdk.Identity object created here will be used for all types of server initialization. It requires an xxdk.Cmix object.

Example:

identity, err := xxdk.MakeIdentity(client.GetRng(), client.GetStorage().GetE2EGroup())

Building Servers

Creating Connect-backed Servers

receptionId: the client ID that will be used for all incoming requests. Derived from xxdk.Identity object

privKey: the private key belonging to the receptionId. Derived from xxdk.Identity object

rng: from xxdk.Cmix object

grp: from xxdk.Cmix storage object

net: from xxdk.Cmix object

p: customizable parameters for the server Obtained and mutable via connect.GetDefaultParams()

Example:

server, err := connect.NewServer(myIdentity.ID, myIdentity.DHKeyPrivate, client.GetRng(), 
	client.GetStorage().GetE2EGroup(), client.GetCmix(), connect.GetDefaultParams())
Creating Single-backed Servers

receptionId: the client ID that will be used for all incoming requests. Derived from xxdk.Identity object

privKey: the private key belonging to the receptionId. Derived from xxdk.Identity object

grp: from xxdk.Cmix storage object

net: from xxdk.Cmix object

Example:

server, err := single.NewServer(myIdentity.ID, myIdentity.DHKeyPrivate, 
	client.GetStorage().GetE2EGroup(), client.GetCmix())

Adding Server Endpoints

Once you have a server object, you can begin adding Endpoints to process incoming client requests. See documentation in restlike/types.go for more information.

Example:

// Build a callback for the new endpoint
// The callback processes a restlike.Message and returns a restlike.Message response
cb := func(msg *Message) *Message {
    // Read the incoming restlike.Message and print its contents
    // NOTE: You may encode the msg.Contents in any way you like, as long as it matches on both sides.
    //       In this case, we're expecting a simple byte encoding.
    fmt.Printf("Incoming message: %s", string(msg.Content))
    // Return a friendly response to the incoming message 
    // NOTE: For responses, Content, Headers, and Error are the only meaningful fields
    return &restlike.Message{
		Content: []byte("Hello! Nice to meet you."),
		Headers: &restlike.Headers{
			Headers: nil,
			Version: 0,
		},
		Error: nil,
	}
}
// Add an endpoint that accepts 'restlike.Get' requests at the 'results' endpoint
server.GetEndpoints().Add("results", restlike.Get, cb)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_restLikeMessages_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

type Callback

type Callback func(*Message) *Message

Callback serves as an Endpoint function to be called when a Request is received Should return the desired response to be sent back to the sender

type Data

type Data []byte

Data provides a generic structure for data sent with a Request or received in a Message NOTE: The way this is encoded is up to the implementation. For example, protobuf or JSON

type Endpoints

type Endpoints struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Endpoints represents a map of internal endpoints for a RestServer

func NewEndpoints

func NewEndpoints() *Endpoints

NewEndpoints returns a new Endpoints object

func (*Endpoints) Add

func (e *Endpoints) Add(path URI, method Method, cb Callback) error

Add a new Endpoint Returns an error if Endpoint already exists

func (*Endpoints) Get

func (e *Endpoints) Get(path URI, method Method) (Callback, error)

Get an Endpoint Returns an error if Endpoint does not exist

func (*Endpoints) Remove

func (e *Endpoints) Remove(path URI, method Method) error

Remove an Endpoint Returns an error if Endpoint does not exist

type Headers

type Headers struct {

	// Headers allows for custom headers to be included with a Request
	Headers []byte `protobuf:"bytes,1,opt,name=headers,proto3" json:"headers,omitempty"`
	// Version allows for endpoints to be backwards-compatible
	// and handle different formats of the same Request
	Version uint32 `protobuf:"varint,2,opt,name=version,proto3" json:"version,omitempty"`
	// contains filtered or unexported fields
}

Headers allows different configurations for each Request that will be specified in the Request header

func (*Headers) Descriptor deprecated

func (*Headers) Descriptor() ([]byte, []int)

Deprecated: Use Headers.ProtoReflect.Descriptor instead.

func (*Headers) GetHeaders

func (x *Headers) GetHeaders() []byte

func (*Headers) GetVersion

func (x *Headers) GetVersion() uint32

func (*Headers) ProtoMessage

func (*Headers) ProtoMessage()

func (*Headers) ProtoReflect

func (x *Headers) ProtoReflect() protoreflect.Message

func (*Headers) Reset

func (x *Headers) Reset()

func (*Headers) String

func (x *Headers) String() string

type Message

type Message struct {
	Content []byte   `protobuf:"bytes,1,opt,name=content,proto3" json:"content,omitempty"`
	Headers *Headers `protobuf:"bytes,2,opt,name=headers,proto3" json:"headers,omitempty"`
	Method  uint32   `protobuf:"varint,3,opt,name=method,proto3" json:"method,omitempty"`
	Uri     string   `protobuf:"bytes,4,opt,name=uri,proto3" json:"uri,omitempty"`
	Error   string   `protobuf:"bytes,5,opt,name=error,proto3" json:"error,omitempty"`
	// contains filtered or unexported fields
}

Message are used for sending to and receiving from a RestServer

func (*Message) Descriptor deprecated

func (*Message) Descriptor() ([]byte, []int)

Deprecated: Use Message.ProtoReflect.Descriptor instead.

func (*Message) GetContent

func (x *Message) GetContent() []byte

func (*Message) GetError

func (x *Message) GetError() string

func (*Message) GetHeaders

func (x *Message) GetHeaders() *Headers

func (*Message) GetMethod

func (x *Message) GetMethod() uint32

func (*Message) GetUri

func (x *Message) GetUri() string

func (*Message) ProtoMessage

func (*Message) ProtoMessage()

func (*Message) ProtoReflect

func (x *Message) ProtoReflect() protoreflect.Message

func (*Message) Reset

func (x *Message) Reset()

func (*Message) String

func (x *Message) String() string

type Method

type Method uint32

Method defines the possible Request types

const (
	// Undefined default value
	Undefined Method = iota
	// Get retrieve an existing resource.
	Get
	// Post creates a new resource.
	Post
	// Put updates an existing resource.
	Put
	// Patch partially updates an existing resource.
	Patch
	// Delete a resource.
	Delete
)

func (Method) String

func (m Method) String() string

String returns the Method as a human-readable name.

type RequestCallback

type RequestCallback func(*Message)

RequestCallback provides the ability to make asynchronous Request in order to get the Message later without blocking

type URI

type URI string

URI defines the destination endpoint of a Request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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