petrel Library

The petrel library is mainly used to configure and setup the petrel server from within your bootstrap function. It also provides some helper functions.

Synopsis

Usage in LUA:

function bootstrap()
    petrel.add_lib_search_path("/opt/my_native_lib/path")
    petrel.load_lib("my_native_lib")
    petrel.add_route("/mypath", "mypath_handler")
    petrel.add_directory_route("/myfiles", "/absolute/root/path")
end

function mypath_handler(req, res)
    -- you might need to wait at some point
    petrel.sleep_millis(1000)

    return res
end
petrel.add_route(path, handler)

Setup a handler route.

Parameters:
  • path (string) – The path
  • handler (string) – The function name of the LUA handler

If multiple path’s are overlapping, the most specific path handler gets executed. For example if you setup the following two routes:

petrel.add_route("/mypath", "mypath_handler")
petrel.add_route("/mypath/subpath", "mysubpath_handler")

mysubpath_handler gets executed for requests like:

While mypath_handler gets executed for requests like:

petrel.add_directory_route(path, root)

Setup a static file delivery route.

Parameters:
  • path (string) – The http query path
  • root (string) – The local root directory
petrel.add_lib_search_path(path)

Add a directory to the list of directories to be searched for a native library when calling load_lib().

Parameters:path (string) – The path
petrel.lib_search_path()

Return a colon separated list of directories that get searched for a native library when calling load_lib().

Returns:(string) - A string containing the search directories.
petrel.load_lib(name)

Load a native library and expose it into the LUA environment. A list of directories (the search_path) gets searched to find the shared object file (see add_lib_search_path() and lib_search_path()).

Parameters:name (string) – The name of the native library
petrel.sleep_nanos(N)

Let the current fiber sleep for N nanoseconds.

Parameters:N (integer) – The number of nanoseconds to sleep.
petrel.sleep_micros(N)

Let the current fiber sleep for N microseconds.

Parameters:N (integer) – The number of microseconds to sleep.
petrel.sleep_millis(N)

Let the current fiber sleep for N milliseconds.

Parameters:N (integer) – The number of milliseconds to sleep.