Categorygithub.com/kedric/lambdarouter
modulepackage
0.0.0-20190714105155-53393fcf62f5
Repository: https://github.com/kedric/lambdarouter.git
Documentation: pkg.go.dev

# README

This router is fully based on https://github.com/dimfeld/httptreemux it was modified for use on AWS Lambda and take all advantage on this

it's possible to use on local with builtin server NOT USE BUILTIN SERVER ON PRODUCTION

Usage

package main

import (
	"context"
	"fmt"
	"github.com/kedric/lambdarouter"
	"github.com/aws/aws-lambda-go/events"
	"log"
)

func Index(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error){
	return events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:     "Welcome!\n",
	}, nil
}

func Hello(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	return events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       fmt.Sprintf("hello, %s!\n", req.PathParameters["name"]),
	}, nil
}

func main() {
	router := lambdarouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)

	router.Serve(":8080", nil)
}

Serve

Serve method on router check if AWS_EXECUTION_ENV is set in env if is not set start mux server with port passed in argument else call lambda.Start(...) from aws sdk

on mux server all path has prefixed by /:__stage__ when request oncomming the stage variable is stored in event.RequestContext.Stage

Stage Variables

if you need to pass a stageVariables to lambda with http handler add them on serv

exemple:

var variables = lambdarouter.StageVariables{
	"stagename": {
		"variablename":       "value",
	},
}

router.Serv(":8080", variables)

Authorizer

ON PROGRESS

For use authorizer add handler function by router.SetAuthorizer(handler)

func authorizer (ctx context.Context, request events.APIGatewayCustomAuthorizerRequestTypeRequest) (events.APIGatewayCustomAuthorizerResponse, error) {
	...
}

func main() {
	router := lambdarouter.New()
	router.SetAuthorizer(authorizer)
	router.Serve(":8080", nil)
}

When you use builtin server it was call befor handler and passed on request. When you deploy on lambda create spesific lambda with env variable AUTHORIZER = true.

# Functions

AddParamsToContext inserts a parameters map into a context using the package's internal context key.
Clean is the URL version of path.Clean, it returns a canonical URL path for p, eliminating .
No description provided by the author
ContextParams returns the params map associated with the given context if one exists.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MethodNotAllowedHandler is the default handler for TreeMux.MethodNotAllowedHandler, which is called for patterns that match, but do not have a handler installed for the requested method.
No description provided by the author
NewContextMux returns a TreeMux preconfigured to work with standard http Handler functions and context objects.
No description provided by the author
No description provided by the author
No description provided by the author
ShowErrorsPanicHandler prints a nice representation of an error to the browser.
SimplePanicHandler just returns error 500.
No description provided by the author

# Constants

Return 301 Moved Permanently.
Return 307 HTTP/1.1 Temporary Redirect.
Return a 308 RFC7538 Permanent Redirect.
Use r.RequestURI.
Use r.URL.Path.
Just call the handler function.

# Structs

ContextGroup is a wrapper around Group, with the purpose of mimicking its API, but with the use of Handle-based handlers.
No description provided by the author
No description provided by the author
LookupResult contains information about a route lookup, which is returned from Lookup and can be passed to ServeLookupResult if the request should be served.
No description provided by the author

# Type aliases

The params argument contains the parameters parsed from wildcards and catch-alls in the URL.
No description provided by the author
No description provided by the author
RedirectBehavior sets the behavior when the router redirects the request to the canonical version of the requested URL using RedirectTrailingSlash or RedirectClean.
No description provided by the author