salt

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

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

Go to latest
Published: Sep 3, 2023 License: Apache-2.0 Imports: 9 Imported by: 2

README

Salt - tiny wrapper around NATS

still in early alfa so API will change!

todo:

  • pub/sub
  • todo

go get -u -v github.com/ituoga/salt

package main

import (
	"log"
	"time"

	"github.com/ituoga/salt"
)

type permissionsChecker struct {
	token    string
	resource string
	db       *struct{}
}

func (p *permissionsChecker) WithToken(token string) salt.RBACContext {
	p.token = token
	return p
}

func (p permissionsChecker) Can(action string) bool {
	// TODO: get User from DB
	// TODO: get user permissions from DB
	// TODO: check if user has action and/or for resource
	return true
}

func (p *permissionsChecker) WithResource(resource string) salt.RBACContext {
	p.resource = resource
	return p
}

var _ salt.RBACContext = (*permissionsChecker)(nil)

func main() {
	router := salt.NewRouter()
	router.WithPermission(&permissionsChecker{
		// TODO: implement this
		// TODO: db connection and other
	})

	router.Handle("topic.example-1", func(ctx *salt.Context) {
		ctx.Response().Reply("topic")
	})

	router.Handle("topic.example-2", func(ctx *salt.Context) {
		if ctx.Can("view-own") {
			ctx.Response().Reply("owns")
			return
		}
		ctx.Response().Reply("default resonse")
	})

	go func() {
		c, _ := salt.NewClient("nats://localhost:4222")
		for {
			time.Sleep(1 * time.Second)
			response, err := c.Request("topic.example-1",
				salt.WithPayloadClient("hello world"),
				salt.WithTokenClient("token"),
			)
			if err != nil {
				log.Printf("%v", err)
				continue
			}
			log.Printf("%v", response)
		}
	}()

	router.Run("nats://localhost:4222")
	select {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(natsURL string) (*Client, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Request

func (c *Client) Request(topic string, opts ...ClientRequestOpts) (*ResponseToClient, error)

type ClientRequestOpts

type ClientRequestOpts func(*SystemMessage)

func WithArg

func WithArg(args string) ClientRequestOpts

func WithPayloadClient

func WithPayloadClient(payload interface{}) ClientRequestOpts

func WithPayloadClientRaw

func WithPayloadClientRaw(payload interface{}) ClientRequestOpts

func WithQueryClient

func WithQueryClient(uv url.Values) ClientRequestOpts

func WithTokenClient

func WithTokenClient(token string) ClientRequestOpts

type Context

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

func (*Context) Can

func (c *Context) Can(action string) bool

func (*Context) Error

func (ctx *Context) Error(opts ...SystemResponseOption)

Error is a helper function to reply with a SystemResponse and error

func (*Context) ErrorWith

func (ctx *Context) ErrorWith(code int, message string)

Add this to your salt.Context

func (*Context) Request

func (c *Context) Request() *Request

func (*Context) Response

func (c *Context) Response() *Response

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc defines the handler function

type Meta

type Meta struct {
	Token string `json:"token"`
}

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

func RBAC

func RBAC(action string) MiddlewareFunc

type RBACContext

type RBACContext interface {
	WithToken(string) RBACContext
	WithResource(string) RBACContext
	Can(action string) bool
}

type Request

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

func (*Request) Arg

func (r *Request) Arg() string

func (*Request) GetPayload

func (r *Request) GetPayload() string

func (*Request) Query

func (r *Request) Query() *url.Values

func (*Request) To

func (r *Request) To(to interface{}) error

type Response

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

Response struct

func (*Response) Notify

func (r *Response) Notify(topic string, payload interface{}, opts ...SystemResponseOption) error

Notify sends a notification to a topic with payload and options

func (*Response) Reply

func (r *Response) Reply(payload interface{}, opts ...SystemResponseOption) error

Reply sends a reply to the request with payload and options

type ResponseToClient

type ResponseToClient struct {
	Data    responseToClient `json:"data,omitempty"`
	Code    int              `json:"code"`
	Message string           `json:"message"`
}

func (*ResponseToClient) CopyTo

func (r *ResponseToClient) CopyTo(to *json.RawMessage) error

func (*ResponseToClient) To

func (r *ResponseToClient) To(to interface{}) error

func (*ResponseToClient) Write

func (r *ResponseToClient) Write(w io.Writer) (int, error)

type Router

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

Router struct

func NewRouter

func NewRouter() *Router

NewRouter creates a new router

func (*Router) Dispatch

func (r *Router) Dispatch(topic string, ctx *Context)

Dispatch dispatches the message to the right handler

func (*Router) DispatchEvent

func (r *Router) DispatchEvent(topic string, ctx *Context)

Dispatch dispatches the message to the right handler

func (*Router) Handle

func (r *Router) Handle(topic string, handler HandlerFunc)

Handle registers a new route with a handler

func (*Router) HandleEvent

func (r *Router) HandleEvent(topic string, handler HandlerFunc)

func (*Router) Run

func (r *Router) Run(natsURL string) error

Run runs the router, connects to nats and subscribes to all routes

func (*Router) WithPermission

func (r *Router) WithPermission(rbac RBACContext) *Router

func (*Router) WithResource

func (r *Router) WithResource(resource string) *Router

type SystemMessage

type SystemMessage struct {
	Meta    *Meta      `json:"meta"`
	Payload string     `json:"payload"`
	Query   url.Values `json:"query"`
	Arg     string     `json:"arg"`
}

func (*SystemMessage) Marshal

func (m *SystemMessage) Marshal() ([]byte, error)

func (*SystemMessage) Unmarshal

func (m *SystemMessage) Unmarshal(data []byte) error

type SystemResponse

type SystemResponse struct {
	Code    int            `json:"code"`
	Message string         `json:"message"`
	Type    string         `json:"type,omitempty"`
	SysMsg  *SystemMessage `json:"data,omitempty"`
}

SystemMessage is the data field of SystemResponse

func NewSystemResponse

func NewSystemResponse(code int, message string) *SystemResponse

SystemMessage is the data field of SystemResponse

func (*SystemResponse) ToJSON

func (e *SystemResponse) ToJSON() string

Convert the struct to JSON, handle errors as needed

type SystemResponseOption

type SystemResponseOption func(*SystemResponse)

SystemResponseOption is a function that modifies a SystemResponse

func WithCode

func WithCode(code int) SystemResponseOption

WithCode is a helper function to reply with a SystemResponse and specific code

func WithError

func WithError(code int, message string) SystemResponseOption

WithError is a helper function to reply with a SystemResponse and error

func WithMessage

func WithMessage(msg string) SystemResponseOption

WithMessage is a helper function to reply with a SystemResponse and specific message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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