gola

package module
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

GoLA(Golang framework for Lambda with ALB)

HOWTO

Enable ALB MultiValueSupport

Enable Multi Value Header

New Serve
serve := gola.NewServe()
Register Serve
serve := gola.NewServe()
runtime.Start(serve.Register)
Register Endpoint
type DefaultRootHandler struct {
}

func (d *DefaultRootHandler) Run(ctx context.Context, request Request, response Response) (er error) {
    response.SetHeader("ROOT", "VAL")
    response.SetBody(buf.EmptyByteBuf().WriteString("{}"))
    return nil
}

type DefaultEmptyHandler struct {
}

func (d *DefaultEmptyHandler) Run(ctx context.Context, request Request, response Response) (er error) {
    response.SetContentType("text/plain")
    return nil
}

type DefaultJSONHandler struct {
}

func (d *DefaultJSONHandler) Run(ctx context.Context, request Request, response Response) (er error) {
    response.SetHeader("USER_ID", request.PathParameter("user_id"))
    response.SetHeader("BOOK_ID", request.PathParameter("book"))
    response.JSONResponse(buf.EmptyByteBuf().WriteString("{}"))
    return nil
}

type DefaultBadHandler struct {
}

func (d *DefaultBadHandler) Run(ctx context.Context, request Request, response Response) (er error) {
    return erresponse.ServerError
}


func main() {
    serve := gola.NewServe()
    var CORS = &gola.DefaultCORSHandler{}
    route.
        // :user_id is path parameter, named user endpoint path parameter to :user_id
        SetEndpoint("/auth/group/user/:user_id", CORS, &DefaultEmptyHandler{}).
        // :user_id, :book are path parameter
        // named user endpoint path parameter to :user_id
        // named book endpoint path parameter to :book
        SetEndpoint("/auth/group/user/:user_id/book/:book", CORS, &DefaultJSONHandler{}).
        // :user_id is path parameter,
        // it will create a path parameter :profile because profile is also an endpoint node.
        SetEndpoint("/auth/group/user/:user_id/profile", CORS, &DefaultEmptyHandler{}).
        // /bad endpoint will always return ServerError 500 because it `return erresponse.ServerError` as error return
        SetEndpoint("/bad", CORS, &DefaultBadHandler{}).
        // * is wildcard match, match all path under /wild/
        SetEndpoint("/wild/*", CORS, &DefaultWildHandler{}).
        // * is wildcard match, match all path under /case/wild/
        SetEndpoint("/case/wild/*", CORS, &DefaultWildHandler{})
    
    runtime.Start(serve.Register)
}

Documentation

Index

Constants

View Source
const (
	CtxGoLA             = "gola"
	CtxGoLAParams       = "gola-params"
	CtxGoLANode         = "gola-node"
	CtxGoLANodeLast     = "gola-node-last"
	CtxGoLAHandler      = "gola-handler"
	CtxGoLAHandlerError = "gola-handler-error"
)

Variables

View Source
var NotImplemented = erresponse.NotImplemented

Functions

func CORSHelper

func CORSHelper(request Request, response Response)

Types

type DefaultCORSHandler

type DefaultCORSHandler struct {
}

func (*DefaultCORSHandler) Run

func (d *DefaultCORSHandler) Run(ctx context.Context, request Request, response Response) (er error)

type DefaultEmptyHandler added in v0.8.3

type DefaultEmptyHandler struct {
	DefaultHandler
}

func (*DefaultEmptyHandler) Run added in v0.8.3

func (d *DefaultEmptyHandler) Run(ctx context.Context, request Request, response Response) (er error)

type DefaultHandler

type DefaultHandler struct {
}

func (*DefaultHandler) GetParam added in v0.8.6

func (d *DefaultHandler) GetParam(ctx context.Context, key string) any

func (*DefaultHandler) GoLA added in v0.8.1

func (d *DefaultHandler) GoLA(ctx context.Context) *GoLA

func (*DefaultHandler) IsLastNode added in v0.8.1

func (d *DefaultHandler) IsLastNode(ctx context.Context) bool

func (*DefaultHandler) Node added in v0.8.1

func (d *DefaultHandler) Node(ctx context.Context) Node

func (*DefaultHandler) Run

func (d *DefaultHandler) Run(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHandler) SetParam added in v0.8.6

func (d *DefaultHandler) SetParam(ctx context.Context, key string, value any)

type DefaultHttpHandler added in v0.7.3

type DefaultHttpHandler struct {
	DefaultHandler
}

func (*DefaultHttpHandler) After added in v0.7.3

func (h *DefaultHttpHandler) After(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Before added in v0.7.3

func (h *DefaultHttpHandler) Before(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Connect added in v0.7.3

func (h *DefaultHttpHandler) Connect(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Create added in v0.8.4

func (h *DefaultHttpHandler) Create(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Delete added in v0.7.3

func (h *DefaultHttpHandler) Delete(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) ErrorCaught added in v0.7.3

func (h *DefaultHttpHandler) ErrorCaught(ctx context.Context, request Request, response Response, err erresponse.ErrorResponse)

func (*DefaultHttpHandler) Get added in v0.7.3

func (h *DefaultHttpHandler) Get(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Index added in v0.7.3

func (h *DefaultHttpHandler) Index(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Options added in v0.7.3

func (h *DefaultHttpHandler) Options(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Patch added in v0.7.3

func (h *DefaultHttpHandler) Patch(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Post added in v0.7.3

func (h *DefaultHttpHandler) Post(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Put added in v0.7.3

func (h *DefaultHttpHandler) Put(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Run added in v0.8.0

func (h *DefaultHttpHandler) Run(ctx context.Context, request Request, response Response) (er error)

func (*DefaultHttpHandler) Trace added in v0.7.3

func (h *DefaultHttpHandler) Trace(ctx context.Context, request Request, response Response) (er error)

type DefaultNotFoundHandler

type DefaultNotFoundHandler struct {
}

func (*DefaultNotFoundHandler) Run

func (d *DefaultNotFoundHandler) Run(ctx context.Context, request Request, response Response) (er error)

type DefaultServerErrorHandler

type DefaultServerErrorHandler struct {
}

func (*DefaultServerErrorHandler) Run

func (d *DefaultServerErrorHandler) Run(ctx context.Context, request Request, response Response) (er error)

type ErrorResponseImpl added in v0.7.3

type ErrorResponseImpl struct {
	erresponse.ErrorResponse
	Caught *kkpanic.CaughtImpl `json:"caught,omitempty"`
}

func (*ErrorResponseImpl) String added in v0.7.3

func (e *ErrorResponseImpl) String() string

type GoLA

type GoLA struct {
	BeginHandler, NotFoundHandler, ServerErrorHandler, FinishHandler Handler
	// contains filtered or unexported fields
}

func NewServe

func NewServe() *GoLA

func (*GoLA) Context added in v0.8.5

func (g *GoLA) Context(key any) any

func (*GoLA) ContextInject added in v0.8.1

func (g *GoLA) ContextInject(key any, value any) *GoLA

func (*GoLA) Register

func (*GoLA) Route

func (g *GoLA) Route() *Route

type Handler

type Handler interface {
	Run(ctx context.Context, request Request, response Response) (er error)
}

type HttpHandler added in v0.7.3

type HttpHandler interface {
	Index(ctx context.Context, request Request, response Response) (er error)
	Get(ctx context.Context, request Request, response Response) (er error)
	Create(ctx context.Context, request Request, response Response) (er error)
	Post(ctx context.Context, request Request, response Response) (er error)
	Put(ctx context.Context, request Request, response Response) (er error)
	Delete(ctx context.Context, request Request, response Response) (er error)
	Options(ctx context.Context, request Request, response Response) (er error)
	Patch(ctx context.Context, request Request, response Response) (er error)
	Trace(ctx context.Context, request Request, response Response) (er error)
	Connect(ctx context.Context, request Request, response Response) (er error)
	Before(ctx context.Context, request Request, response Response) (er error)
	After(ctx context.Context, request Request, response Response) (er error)
	ErrorCaught(ctx context.Context, request Request, response Response, err erresponse.ErrorResponse)
}

type Node

type Node interface {
	Parent() Node
	Handlers() []Handler
	Name() string
	ParameterName() string
	Children() map[string]Node
	NodeType() NodeType
}

type NodeType

type NodeType int
const (
	NodeTypeEndPoint NodeType = iota
	NodeTypeNamespace
	NodeTypeRecursive
	NodeTypeRoot
)

type Request

type Request interface {
	Request() *events.ALBTargetGroupRequest
	Method() string
	Path() string
	PathParameter(name string) string
	TraceId() string
	UserAgent() string
	Header() http.Header
	GetHeader(name string) string
	GetHeaders(name string) []string
	QueryValue(name string) string
	QueryValues(name string) []string
	Body() buf.ByteBuf
}

func NewRequest added in v0.8.7

func NewRequest(req events.ALBTargetGroupRequest, pathParameters map[string]string) Request

type Response

type Response interface {
	Build() *events.ALBTargetGroupResponse
	StatusCode() int
	SetStatusCode(code int) Response
	AddHeader(name string, value string) Response
	SetHeader(name string, value string) Response
	DelHeader(name string) Response
	Header() http.Header
	GetHeader(name string) string
	GetHeaders(name string) []string
	Cookie(name string) *http.Cookie
	SetCookie(cookie http.Cookie) Response
	Cookies() map[string][]http.Cookie
	Body() []byte
	SetBody(buf buf.ByteBuf) Response
	SetContentType(ct string) Response
	JSONResponse(buf buf.ByteBuf) Response
}

func NewResponse added in v0.8.7

func NewResponse() Response

type Route

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

func NewRoute

func NewRoute() *Route

func (*Route) FindNode

func (r *Route) FindNode(path string) Node

func (*Route) RouteNode

func (r *Route) RouteNode(path string) (node Node, parameters map[string]string, isLast bool)

func (*Route) SetEndpoint

func (r *Route) SetEndpoint(path string, handlers ...Handler) *Route

func (*Route) SetRootHandlers

func (r *Route) SetRootHandlers(handlers ...Handler) *Route

func (*Route) String

func (r *Route) String() string

Jump to

Keyboard shortcuts

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