在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):pintsized/lua-resty-rack开源软件地址(OpenSource Url):https://github.com/pintsized/lua-resty-rack开源编程语言(OpenSource Language):Lua 56.0%开源软件介绍(OpenSource Introduction):lua-resty-rackA simple and extensible HTTP server framework for OpenResty, providing a clean method for loading Lua HTTP applications ("resty" modules) into Nginx. Drawing inspiration from Rack and also Connect, lua-resty-rack allows you to load your application as a piece of middleware, alongside other middleware. Your application can either; ignore the current request, modify the request or response in some way and pass on to other middleware, or take responsibiliy for the request by generating a response. StatusThis library is considered experimental and the API may change without notice. Please feel free to offer suggestions or raise issues here on Github. InstallationClone the repo and ensure the contents of Using MiddlewareTo install middleware for a given server {
location / {
content_by_lua '
local rack = require "resty.rack"
rack.use(rack.middleware.method_override)
rack.use(require "my.module")
rack.run()
';
}
} rack.use(...)Syntax: If rack.use('/some/path', app, { foo = 'bar' }) For simple cases, the rack.use(function(req, res, next)
res.header["X-Homer"] = "Doh!"
next()
end) rack.run()Syntax: Runs each of the middleware in order, until one chooses to handle the response. Thus, the order in which you call Bundled Middlewaremethod_overriderack.use(rack.middleware.method_override, { key = "METHOD" }) Override the HTTP method using a querystring value. The default argument name is "_method" but this can be overriden by setting the option "key". read_request_headersrack.use(rack.middleware.read_request_headers, { max = 50 }) This is only needed if you wish to iterate over the HTTP request headers. They will be lazy loaded when accessed via You may specify a limit to the number of request headers to be read, which defaults to read_bodyrack.use(rack.middleware.read_body) Explicitly reads the request body (raw). Creating MiddlewareMiddleware applications are simply Lua modules which use the HTTP request and response as a minimal interface. They must implement the function module("resty.rack.method_override", package.seeall)
_VERSION = '0.01'
function call(options)
return function(req, res, next)
local key = options['key'] or '_method'
req.method = string.upper(req.args[key] or req.method)
next()
end
end APIreq.methodThe HTTP method, e.g. req.schemeThe protocol scheme req.urie.g. req.hostThe hostname, e.g. req.queryThe querystring, e.g. req.argsThe query args, as a req.headerA table containing the request headers. Keys are matched case insensitvely, and optionally with underscores instead of hyphens. e.g. req.header["X-Foo"] = "bar"
res.body = req.header.x_foo
--> "bar" HTTP Request headers are read on demand and so cannot be iterated over unless the req.bodyAn empty string until read with the res.statusThe HTTP status code to return. There are constants defined for common statuses. res.headerA table of response headers, which can be matched case insensitively and optionally with underscores instead of hyphens (see res.bodyThe response body. nextThis parameter is a function provided to the middleware, which may be called to indicate rack should try the next middleware. If your application does not intend to send the response to the browser, it must call this function. If however your application is taking responsibility for the response, simply return without calling next. Example purely modifying the request. function call(options)
return function(req, res, next)
local key = options['key'] or '_method'
req.method = string.upper(req.args[key] or req.method)
next()
end
end Example generating a response. function call(options)
return function(req, res)
res.status = 200
res.header['Content-Type'] = "text/plain"
res.body = "Hello World"
end
end Enhancing req / resYour application can add new fields or even functions to the req / res tables where appropriate, which could be used by other middleware so long as the dependencies are clear (and one calls AuthorJames Hurst [email protected] LicenceThis module is licensed under the 2-clause BSD license. Copyright (c) 2012, James Hurst [email protected] All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论