lotus

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2020 License: MIT Imports: 14 Imported by: 1

README

lotus

Lotus is a microframework for microservices but using fasthttp

License

MIT

Documentation

Index

Constants

View Source
const (
	Binary DataType = 1
	JSON            = 2
	Form            = 3
)
View Source
const (
	// DELETE defines the DELETE type of request
	DELETE Method = fasthttp.MethodDelete
	// GET defines the GET type of request
	GET = fasthttp.MethodGet
	// POST defines the POST type of request
	POST = fasthttp.MethodPost
	// PUT defines the PUT type of request
	PUT = fasthttp.MethodPut

	// Default values
	DefaultRouteMethod Method = GET

	DefaultBodyDataType = Binary

	// DefaultKey is the UserValue key used when no Key is passed to the initializer
	DefaultKey string = "data"
)
View Source
const (

	// Default Host is the host used if not initialized.
	DefaultHost string = "localhost"
	// Default Port is the port used if not initialized.
	DefaultPort = "8080"
	// Default Namespace is the namespace used  if not initialized.
	DefaultNamespace = ""
	// Default Version is the version used if not initialized.
	DefaultVersion = "v0"

	// HTTP protocol
	HTTP protocol = "http"
	// HTTPS protocol
	HTTPS = "https"

	// RouteNotFoundError error when the service can't find a route
	RouteNotFoundError string = "route not found"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	*fasthttp.RequestCtx
	ServiceClients []ServiceClient
}

func (*Context) Payload

func (ctx *Context) Payload() (Payload, error)

func (*Context) ServiceClient

func (ctx *Context) ServiceClient(sub ServiceContract) *ServiceClient

type Contract

type Contract struct {
	Services []ServiceContract
}

type DataHandlerConfig

type DataHandlerConfig struct {
	BodyType     DataType
	UserValueKey string
}

type DataType

type DataType int

type Method

type Method string

Method defines usable methods for endpoints

type Payload

type Payload interface{} // This will be much better with generics

type RequestHandler

type RequestHandler func(ctx *Context)

type Route

type Route struct {
	*RouteContract
	// The endpoint is the path used to receive the request
	RequestHandler RequestHandler
	// Middlewares functions executed before the RequestHandler
	Middlewares []fastalice.Constructor
	// DataHandlers used to receive requests
	DataHandler fastalice.Constructor
	// contains filtered or unexported fields
}

Route is the definition of a route and what's used to initialize a route

type RouteContract

type RouteContract struct {
	// Label is an identification for the route. It's used on status and log information
	Label string
	// Description is a short text that helps to document the route
	Description string
	// Method is the fasthttp method the route will listen upon
	Method Method
	// Path is the location where the route will listen to
	Path string
	// DataHandlerConfig is the configuration for the route DataHandler. This is an optional field
	DataHandlerConfig DataHandlerConfig
	// Data is the Data used. Use an empty struct value
	Data interface{}
}

RouteContract is the Contract description of a Route

func (*RouteContract) DataType

func (route *RouteContract) DataType() DataType

type Service

type Service struct {
	*ServiceContract
	// contains filtered or unexported fields
}

Service is a Service Provider that starts itself and serves declared routes over a self created router

func (*Service) AddRoute

func (service *Service) AddRoute(route *Route)

AddRoute adds an already created route to the service routes array

func (*Service) SetupRoute

func (service *Service) SetupRoute(
	label string,
	endpoint RequestHandler,
	middlewares []fastalice.Constructor,
	dataHandler fastalice.Constructor,
) *Route

SetupRoute searches for a route contract identified by label, creates a Route, add to it the endpoint and the middlewares and put it on the Routes array returning the newly created routes

func (*Service) Start

func (service *Service) Start()

* Start inits the main process executed by the service. It first creates the internal router and then start a listener for those routes and serve then on the specified Host, Port, Namespace and Version. For a Service created with the following values:

service := &Service {
	Label: "ExampleService",
	Description: "Just a server to cover route url building",
	Protocol: lotus.HTTP,
	Host: "myhost.com",
	Namespace: "example",
}

func (*Service) Status

func (service *Service) Status() *ServiceStatus

func (*Service) Stop

func (service *Service) Stop() error

func (*Service) SubscribeToService added in v0.0.1

func (service *Service) SubscribeToService(sub ServiceContract)

type ServiceClient

type ServiceClient struct {
	*ServiceContract
}

func (*ServiceClient) SendRequest

func (sc *ServiceClient) SendRequest(routeContract RouteContract, payload ServiceRequest) (*fasthttp.Response, error)

Sends a request and returns a response and an error. The response must be released

type ServiceContract

type ServiceContract struct {
	// Name of the service. Used as a identifier for the service
	Label string
	// Description of the service. Intended to be a Short text describing the functionalities of the service
	Description string
	// Protocol is the protocol which the serve will use. The current options are: HTTP, HTTPS or TCP. Defaults to HTTP
	Protocol protocol
	// Host of the service. Can be an IPV4 or IPV6 address. Defaults to "localhost"
	Host string
	// Namespace of the service. The unique namespace which the service will be delivered upon. Defaults to "/"
	Namespace string
	// Port of the service listener
	Port int
	// A version identifier for the service. Defaults to "v0"
	Version string
	// RoutesContracts is an array of RouteContract used to define the Routes on the contract
	RoutesContracts []RouteContract
}

ServiceContract holds the Contract description of a service

func (*ServiceContract) RouteContractByLabel

func (sc *ServiceContract) RouteContractByLabel(label string) *RouteContract

RouteContractByLabel returns the route contract for the given label

func (*ServiceContract) RouteUrl

func (sc *ServiceContract) RouteUrl(label string) (string, error)

func (*ServiceContract) Suffix

func (sc *ServiceContract) Suffix() string

type ServiceProvider

type ServiceProvider interface {
	// Start inits the main process executed by the service
	Start()
	// Stop terminates all processes related to the service
	Stop() error
	// Status returns information about the health of the service
	Status() *ServiceStatus
}

Service providers are constructs able to start, stop and show it's current health (heartbeat)

type ServiceRequest

type ServiceRequest struct {
	RouteParams map[string]string
	QueryParams map[string]string
	Body        Payload
	DataType    DataType
}

type ServiceStatus

type ServiceStatus struct {
	IsRunning        bool
	Address          net.Addr
	RegisteredRoutes int
}

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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