gluo

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Gluo

Write your HTTP application in Go once, deploy it* to your servers and AWS Lambda. Really, it's a drop-in replacement for net/http.

*: it must be compiled as GOOS=linux to be deployed in AWS Lambda.

Status

Alpha quality. It needs to be tested out extensively but it seems to work fine.

GoDoc GoCard Coverage Status

Installation

go get github.com/imdario/gluo

import (
    "github.com/imdario/gluo"
)

Usage

Just call gluo.ListenAndServe instead of http.ListenAndServe.

package main

import (
        "github.com/imdario/gluo"
        "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello from Gluo"))
}

func main() {
        gluo.ListenAndServe(":3000", http.HandlerFunc(handler))
}

Alternatives

Here are some similar projects, although they don't allow to deploy the same binary to servers and AWS Lambda.

Contact me

If I can help you, you have an idea or you are using Gluo in your projects, don't hesitate to drop me a line (or a pull request): @im_dario

About

Written by Dario Castañé.

License

BSD 3-Clause license.

Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIGatewayContext

func APIGatewayContext(ctx context.Context) (events.APIGatewayProxyRequestContext, bool)

APIGatewayContext returns the APIGatewayProxyRequestContext value stored in ctx.

func IsLambda

func IsLambda() bool

IsLambda checks if it is running on AWS Lambda.

func LambdaContext

func LambdaContext(ctx context.Context) (*lambdacontext.LambdaContext, bool)

LambdaContext return the LambdaContext value stored in ctx.

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler) error

ListenAndServe calls http.ListenAndServe with handler to handle requests on incoming connections when invoked outside AWS Lambda environment.

If handler is nil, it uses http.DefaultServeMux as default handler.

In AWS Lambda environment it calls lambda.Start with handler to handle requests transparently as if it was working in a non serverless environment. Addr argument is ignored in AWS Lambda.

A trivial example is:

package main

import (
	"github.com/imdario/gluo"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello from Gluo"))
}

func main() {
	gluo.ListenAndServe(":3000", http.HandlerFunc(handler))
}

ListenAndServe always returns a non-nil error. Under AWS Lambda, it always returns nil.

func ListenAndServeTLS

func ListenAndServeTLS(addr string, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

Types

type LambdaAdapter

type LambdaAdapter struct {
	http.Handler
}

LambdaAdapter allows to wrap a http.Handler for working on AWS Lambda.

func (LambdaAdapter) Handle

Handle handles an AWS Lambda request, converting an APIGatewayProxyRequest into a http.Request.

Jump to

Keyboard shortcuts

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