decorator

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNeedsArguments   = errors.New("need arguments")
	ErrResponseExpected = errors.New("response expected")
)

Functions

func RegisterErrors

func RegisterErrors(b *binder.Binder)

func RegisterHTTPRequest

func RegisterHTTPRequest(ctx context.Context, b *binder.Binder)
Example
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/krakendio/binder"
)

func main() {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		headers, _ := json.Marshal(r.Header)
		fmt.Println(string(headers))
		fmt.Println(r.Method)

		w.Header().Add("X-Multi", "A")
		w.Header().Add("X-Multi", "B")

		if r.Body != nil {
			body, _ := io.ReadAll(r.Body)
			fmt.Println(string(body))
			r.Body.Close()
		}
		fmt.Fprintln(w, "Hello, client")
	}))
	defer ts.Close()

	bindr := binder.New(binder.Options{
		SkipOpenLibs:        true,
		IncludeGoStackTrace: true,
	})

	RegisterLuaList(bindr)
	RegisterHTTPRequest(context.Background(), bindr)

	code := fmt.Sprintf("local url = '%s'\n%s", ts.URL, sampleLuaCode)

	if err := bindr.DoString(code); err != nil {
		fmt.Println(err.Error())
	}

}

const sampleLuaCode = `print("lua http test\n")
local r = http_response.new(url, "POST", '{"foo":"bar"}', {["foo"] = "bar", ["123"] = "456", ["multi"] = {"a", "b"}})
print(r:statusCode())
print(r:headers('Content-Type'))
local hr = r:headerList('X-Multi')
print(hr:get(0))
print(hr:get(1))
print(r:body())
r:close()

local r = http_response.new(url, "POST", '{"foo":"bar"}')
print(r:statusCode())
print(r:headers('Content-Type'))
local hr = r:headerList('X-Multi')
print(hr:get(0))
print(hr:get(1))
print(r:body())
r:close()

local r = http_response.new(url)
print(r:statusCode())
print(r:headers('Content-Type'))
local hr = r:headerList('X-Multi')
print(hr:get(0))
print(hr:get(1))
print(r:body())
r:close()
`
Output:

lua http test

{"123":["456"],"Accept-Encoding":["gzip"],"Content-Length":["13"],"Foo":["bar"],"Multi":["a","b"],"User-Agent":["KrakenD Version undefined"]}
POST
{"foo":"bar"}
200
text/plain; charset=utf-8
A
B
Hello, client

{"Accept-Encoding":["gzip"],"Content-Length":["13"],"User-Agent":["KrakenD Version undefined"]}
POST
{"foo":"bar"}
200
text/plain; charset=utf-8
A
B
Hello, client

{"Accept-Encoding":["gzip"],"User-Agent":["KrakenD Version undefined"]}
GET

200
text/plain; charset=utf-8
A
B
Hello, client

func RegisterLuaList

func RegisterLuaList(b *binder.Binder)

func RegisterLuaTable

func RegisterLuaTable(b *binder.Binder)

func RegisterNil

func RegisterNil(b *binder.Binder)

Types

type Decorator

type Decorator func(*binder.Binder)

Jump to

Keyboard shortcuts

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