Skip to content

Instantly share code, notes, and snippets.

@negasus
Last active June 20, 2023 12:41
Show Gist options
  • Save negasus/14ed3db3ee7b72e3b328ffb81e3814eb to your computer and use it in GitHub Desktop.
Save negasus/14ed3db3ee7b72e3b328ffb81e3814eb to your computer and use it in GitHub Desktop.

API

Usage

return function(context)
    
end

Context

context.route

context.route.destination                   read
context.route.alive                         read
context.route.mapper.server                 read
context.route.mapper.srcMatch               read
context.route.mapper.dst                    read
context.route.mapper.providerID             read
context.route.mapper.pingURL                read
context.route.mapper.matchType              read
context.route.mapper.redirectType           read
context.route.mapper.assetsLocation         read
context.route.mapper.assetsWebRoot          read
context.route.mapper.assetsSPA              read

context.request

context.request.method                      read
context.request.remoteAddr                  read
context.request.requestURI                  read
context.request.host                        read
context.request.readBody()                  callable, fill context.request.body field, returns error
context.request.body                        read (without readBody() returns nil) / write
context.request.header.get(name)            callable, returns string
context.request.header.set(name, value)     callable
context.request.header.add(name, value)     callable
context.request.header.delete(name)         callable

context.response

For read/write response fields you must call next function with captureResponse = true option

context.response.statusCode                 read/write 
context.response.body                       read/write 
context.response.header.get(name)           callable, returns string
context.response.header.set(name, value)    callable
context.response.header.add(name, value)    callable
context.response.header.delete(name)        callable

context.next

context.next(options)                       callable, options is not required

Options is a table

{
    captureResponse = true                  default false
}

Modules

KV

Key/value store

local kv = require('kv')

set

err = kv.set(key, value, [timeout])

Key and Value must be a strings

Returns error if occurred.

Not required timeout must be a string in go duration format.

get

res, err = kv.get(key)

Key must be a string

Returns string value and error, if occurred.

delete

err = kv.delete(key)

Key must be a string

Returns error, if occurred.

LOG

local log = require('log')
log.debug(args...)
log.info(args...)
log.warn(args...)
log.error(args...)

HTTP

local http = require('http')

module constants

http.MethodGet              returns string GET
http.MethodHead             returns string HEAD
http.MethodPost             returns string POST
http.MethodPut              returns string PUT
http.MethodPatch            returns string PATCH 
http.MethodDelete           returns string DELETE
http.MethodConnect          returns string CONNECT
http.MethodOptions          returns string OPTIONS
http.MethodTrace            returns string TRACE

methods

get

resp, err = http.get(url)

URL must be a string

Returns response table (see bellow) and error if occurred

post

resp, err = http.post(url, contentType, body)

URL, contentType and body must be strings

Returns response table (see bellow) and error if occurred

request

resp, err = http.request(method, url, [options])

Method and URL must be string.

Not required options must be a table. All options fields are not require.

Options:

{
    timeout = <string>              string in go duration format, by default '30s'
    body = <string>                 request body, by default empty
    headers = {                     request headers, by default empty
        key1 = value1,
        key2 = value2,
    } 
}

response object

Response is a table

{
    code = <number>                 response status code
    body = <string>                 response body
    headers = {                     response headers
        key1 = {value1, value11},
        key2 = {value2},
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment