service

package
v0.0.0-...-7e023e1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const ContentType = "Content-Type"
View Source
const ErrPathRequired = lerr.Str("RouteConfig: Path is required")
View Source
const HttpRedirect = 302

Variables

View Source
var RequestResponderFilter = filter.ConvertableTo[RequestResponder]()

Functions

func Register

func Register(zeroValues ...type32.TypeIDer32)

Register types with both gob and the typemap.

Types

type Client

type Client struct {
	*Mux
	*Conn
}

func NewClient

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

func (*Client) Add

func (c *Client) Add(h RequestResponder, r *RouteConfig)

func (*Client) Run

func (c *Client) Run()

type ClientEndpoints

type ClientEndpoints struct {
	*Client
	Endpoints lmap.Wrapper[string, RequestResponder]
	Counters  lmap.Wrapper[string, int]
}

func NewClientEndpoints

func NewClientEndpoints(addr string, endpoints any) (ce ClientEndpoints, err error)

NewClientEndpoints solves a specific problem. Creating an endpoint function but forgetting to register it with the Client. ClientEndpoints finds all methods on the endpoints object that fulfill RequestResponder. As each is used to add a route to the Client it is removed the Endpoints map. Calling UnusedEnpoints will panic if a RequestResponder was added but not used in a route.

func (ClientEndpoints) Add

func (ce ClientEndpoints) Add(handler string, rCfg *RouteConfig)

Add pops the endpoints associated with the handler and adds it to the client with the given route. Ths allows UnusedEnpoints to be called which will panic if any handlers are unused.

func (ClientEndpoints) UnusedEnpoints

func (ce ClientEndpoints) UnusedEnpoints()

UnusedEnpoints panics if any endpoints are unused

type Conn

type Conn struct {
	Listener bus.Listener
	Sender   *serialbus.Sender
	NetConn  net.Conn
}

func NewConn

func NewConn(conn net.Conn) (*Conn, error)

type Mux

type Mux struct {
	Handlers map[string]RequestHandler
	Routes   Routes
}

Mux maps the Requests to their handlers.

func NewMux

func NewMux() *Mux

NewMux creates a Mux.

func (*Mux) Add

func (m *Mux) Add(h RequestHandler, r *RouteConfig)

Add a RequestHandler to the Mux mapped to the RouteConfig.

func (*Mux) Handle

func (m *Mux) Handle(r *Request)

Handle a request. If there is no handler, nothing happens.

type Request

type Request struct {
	ID          uint32
	RouteConfig string
	Path        string
	Method      string
	PathVars    map[string]string
	Query       map[string]string
	Form        url.Values
	Body        []byte
	User        *lusers.User
}

Request represents a user request that the luce Server is relaying to the service.

func (*Request) ErrCheck

func (r *Request) ErrCheck(err error) *Response

ResponseErr sets the response body to the error and sets the status.

func (*Request) Redirect

func (r *Request) Redirect(url string) *Response

func (*Request) Response

func (r *Request) Response(body []byte) *Response

Response to the Request.

func (*Request) ResponseErr

func (r *Request) ResponseErr(err error, status int) *Response

ResponseErr sets the response body to the error and sets the status.

func (*Request) ResponseString

func (r *Request) ResponseString(body string) *Response

ResponseString to the Request.

func (*Request) ResponseTemplate

func (r *Request) ResponseTemplate(name string, t luceio.TemplateExecutor, data any) *Response

ResponseTemplate populates the body of the response using the provided template and data. If there is a template error, that will populate the body.

func (*Request) SerializeResponse

func (r *Request) SerializeResponse(s serial.Serializer, data any, buf []byte) (*Response, error)

SerializeResponse uses the provided Serializer and data to create a Response to the Request.

func (*Request) ServeFile

func (r *Request) ServeFile(path string) (resp *Response)

func (*Request) TypeID32

func (*Request) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

type RequestHandler

type RequestHandler func(r *Request)

RequestHandler will handle a request but it will not respond to it.

type RequestResponder

type RequestResponder func(r *Request) *Response

RequestResponder will hand a request and return a reponse.

type Require

type Require struct {
	Group string
}

Require breaks out the possible route requirements so the logic can be shared between RouteConfigGen and RouteConfig.

func (*Require) RequireGroup

func (r *Require) RequireGroup(group string)

RequireGroup sets or appends the group to the Group field.

type Response

type Response struct {
	ID     uint32
	Body   []byte
	Status int
	Header http.Header
}

Response to a request. The ID is the same as the ID is taken from the request.

func (*Response) ContentType

func (r *Response) ContentType(val string) *Response

func (*Response) ErrCheck

func (r *Response) ErrCheck(err error) (notNil bool)

ErrCheck will set the response to the error body only if err is not nil. In this case it will check if the error fulfills lhttp.StatusErr and use that for the status, other wise it will set the status to StatusInternalServerError.

func (*Response) SetHeader

func (r *Response) SetHeader(key, val string) *Response

func (*Response) SetStatus

func (r *Response) SetStatus(status int) *Response

Set Response HTTP Status Code

func (*Response) TypeID32

func (*Response) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

func (*Response) Write

func (r *Response) Write(p []byte) (n int, err error)

Write p to the body. Fulfills io.Writer.

type RouteConfig

type RouteConfig struct {
	ID   string
	Path string
	Host string
	// Method is comma delimited methods that are accepted
	Method     string
	PathPrefix bool
	PathVars   bool
	Form       bool
	Body       bool
	User       bool
	Query      bool
	Require
}

func NewRoute

func NewRoute(path string) *RouteConfig

NewRoute defi* Routesned by path (relative to the base path).

func (*RouteConfig) AddMethod

func (r *RouteConfig) AddMethod(method string) *RouteConfig

AddMethod is a chainable helper. It sets the Method field. If the Method field is not empty, it appends the input with comma seperation.

func (*RouteConfig) Delete

func (r *RouteConfig) Delete() *RouteConfig

Delete is a chainable helper. It adds Delete to the Method field.

func (*RouteConfig) Get

func (r *RouteConfig) Get() *RouteConfig

Get is a chainable helper. It adds Get to the Method field.

func (*RouteConfig) Methods

func (r *RouteConfig) Methods() []string

func (*RouteConfig) Post

func (r *RouteConfig) Post() *RouteConfig

Post is a chainable helper. It adds Post to the Method field.

func (*RouteConfig) Put

func (r *RouteConfig) Put() *RouteConfig

Put is a chainable helper. It adds Put to the Method field.

func (*RouteConfig) RequireGroup

func (r *RouteConfig) RequireGroup(group string) *RouteConfig

RequireGroup is a chainable helper. It calls RequireGroup on the embedded Require.

func (*RouteConfig) SetHost

func (r *RouteConfig) SetHost(host string) *RouteConfig

SetHost is a chainable helper. It sets the Host field.

func (*RouteConfig) String

func (r *RouteConfig) String() string

String fulfills Stringer. Returns the RouteConfig as "(Method)path...", where the ellipse will only be present if the path is a prefix.

func (*RouteConfig) Validate

func (r *RouteConfig) Validate() error

Validate the RouteConfig has necessary fields filled in. Unset fields will be set to their defaults.

func (*RouteConfig) WithBody

func (r *RouteConfig) WithBody() *RouteConfig

WithBody is a chainable helper. It sets the Body field to true.

func (*RouteConfig) WithForm

func (r *RouteConfig) WithForm() *RouteConfig

WithQuery is a chainable helper. It sets the Form field to true.

func (*RouteConfig) WithPrefix

func (r *RouteConfig) WithPrefix() *RouteConfig

WithPrefix is a chainable helper. It sets the PathPrefix field to true.

func (*RouteConfig) WithQuery

func (r *RouteConfig) WithQuery() *RouteConfig

WithQuery is a chainable helper. It sets the Query field to true.

func (*RouteConfig) WithUser

func (r *RouteConfig) WithUser() *RouteConfig

WithUser is a chainable helper. It sets the User field to true.

type RouteConfigGen

type RouteConfigGen struct {
	Base string
	Host string
	User bool
	Require
}

RouteConfigGen is used to create RouteConfigs from a Base path.

func NewRouteConfigGen

func NewRouteConfigGen(basePath string) *RouteConfigGen

func (*RouteConfigGen) Copy

func (r *RouteConfigGen) Copy() *RouteConfigGen

func (*RouteConfigGen) Get

func (g *RouteConfigGen) Get(path string) *RouteConfig

Get creates a RouteConfig with a Path of Base+path at

func (*RouteConfigGen) GetQuery

func (g *RouteConfigGen) GetQuery(path string) *RouteConfig

GetQuery creates a clone of the RouteConfigGen appending path to the Base and setting the method to Get.

func (*RouteConfigGen) Path

func (g *RouteConfigGen) Path(path string) *RouteConfig

Path creates a RouteConfig appending path to the Base and copying the Require object.

func (*RouteConfigGen) Post

func (g *RouteConfigGen) Post(path string) *RouteConfig

GetQuery creates a clone of the RouteConfigGen appending path to the Base and setting the method to Post.

func (*RouteConfigGen) PostForm

func (g *RouteConfigGen) PostForm(path string) *RouteConfig

GetQuery creates a clone of the RouteConfigGen appending path to the Base and setting the method to Post and enabling form data to be included in the requets.

func (*RouteConfigGen) RequireGroup

func (r *RouteConfigGen) RequireGroup(group string) *RouteConfigGen

RequireGroup is a chainable helper. It calls RequireGroup on the embedded Require.

func (*RouteConfigGen) SetHost

func (r *RouteConfigGen) SetHost(host string) *RouteConfigGen

SetHost is a chainable helper. It sets the Host field.

func (*RouteConfigGen) WithUser

func (g *RouteConfigGen) WithUser() *RouteConfigGen

type Routes

type Routes []RouteConfig

Routes holds a collection of RouteConfigs.

func (Routes) TypeID32

func (Routes) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

type SocketClose

type SocketClose struct {
	ID uint32
}

func (SocketClose) TypeID32

func (SocketClose) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

type SocketMessage

type SocketMessage struct {
	ID   uint32
	Body []byte
}

func (SocketMessage) TypeID32

func (SocketMessage) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

type SocketOpened

type SocketOpened struct {
	ID uint32
}

func (SocketOpened) TypeID32

func (SocketOpened) TypeID32() uint32

TypeID32 fulfill TypeIDer32. The ID was choosen at random.

Jump to

Keyboard shortcuts

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