functions

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2020 License: MIT Imports: 8 Imported by: 0

README

Functions

A simple utility to make writing cloud functions easier.

Usage

The main advantages are:

  • avoid boilerplate code to write JSON responses
  • set up log level using a LOG_LEVEL env var
  • set up sentry using SENTRY_DNS env var
  • handle HTTPErrors automatically
var someVariable string

// Setup code
func init() {
    functions.Init(func() (err error) {
        someVariable = "world"
        return nil
    }
}
// Execution
func call(w http.ResponseWriter, r *http.Request) error {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return nil
    }

    hello := map[string]string {
        "hello": someVariable,
    }

	return functions.JSON(w, 200, hello)
}

// HTTP is the actual cloud function
var HTTP = functions.MakeFunction(errors.DefaultTranslator, call)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Empty

func Empty(w http.ResponseWriter)

Empty writes an empty response

func Init

func Init(setup func() error)

Init should be called in the package init

func JSON

func JSON(w http.ResponseWriter, status int, raw interface{}) error

JSON writes a json response

func MakeFunction

func MakeFunction(translator errors.Translator, function Function) func(http.ResponseWriter, *http.Request)

MakeFunction creates a function that can be used by google

Types

type Function

type Function func(http.ResponseWriter, *http.Request) error

Function is a simplified function format

Jump to

Keyboard shortcuts

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