gateway

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 3 Imported by: 0

README

Gateway HTTP ListenAndServe for AWS Lambda (V1 and V2 API Gateway)

Contributor Covenant License GitHub release Status

This Go library provides a drop-in replacement for http.ListenAndServe that is optimized for AWS Lambda's API Gateway (both V1 and V2). The library abstracts away the differences between API Gateway V1 and V2, allowing you to focus on building your application without worrying about the underlying details.

Features

  • Supports both API Gateway V1 (APIGatewayProxyRequest/APIGatewayProxyResponse) and V2 (APIGatewayV2HTTPRequest/APIGatewayV2HTTPResponse).
  • Simple, familiar interface that works with existing http.Handler code.
  • Lightweight, with the underlying complexity hidden.

Getting Started

Installation
  1. Install the library using Go modules:

    go get github.com/go-obvious/gateway
    
  2. Import the package in your Go project:

    import "github.com/go-obvious/gateway"
    
Usage

The library provides two simple entry points for API Gateway V1 and V2:

  • ListenAndServeV1: For API Gateway V1
  • ListenAndServeV2: For API Gateway V2
Example: API Gateway V1

For API Gateway V1, use ListenAndServeV1 as a replacement for http.ListenAndServe.

package main

import (
    "github.com/yourusername/gateway"
    "net/http"
    "encoding/json"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(map[string]string{"message": "Hello from V1 API Gateway!"})
}

func main() {
    // Use ListenAndServeV1 for API Gateway V1
    gateway.ListenAndServeV1(":8080", http.HandlerFunc(myHandler))
}
Example: API Gateway V2

For API Gateway V2, use ListenAndServeV2 as a replacement for http.ListenAndServe.

package main

import (
    "github.com/yourusername/gateway"
    "net/http"
    "encoding/json"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(map[string]string{"message": "Hello from V2 API Gateway!"})
}

func main() {
    // Use ListenAndServeV2 for API Gateway V2
    gateway.ListenAndServeV2(":8080", http.HandlerFunc(myHandler))
}
How It Works
  • ListenAndServeV1: Automatically parses and handles API Gateway V1 requests.
  • ListenAndServeV2: Automatically parses and handles API Gateway V2 requests.
  • Both versions use the familiar http.Handler interface, making it easy to port existing HTTP applications to AWS Lambda.
FAQ
1. Can I use this library outside of AWS Lambda?

No, this library is specifically designed to work with API Gateway in AWS Lambda environments.

2. Can I use both API Gateway versions in the same application?

Yes, you can use both ListenAndServeV1 and ListenAndServeV2 within the same application, depending on which API Gateway version you are targeting.

Contributing

Feel free to submit issues or pull requests for new features, bug fixes, or improvements.

License

This library is licensed under the MIT License. See LICENSE for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListenAndServeV1 added in v0.1.1

func ListenAndServeV1(addr string, h http.Handler) error

func ListenAndServeV2 added in v0.1.1

func ListenAndServeV2(addr string, h http.Handler) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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