Skip to content

Commit

Permalink
Merge pull request rstudio#17 from rstudio/feature/plugin-content-type
Browse files Browse the repository at this point in the history
Allow plugins to specify a content type (optional)
  • Loading branch information
cbarraford committed Apr 25, 2015
2 parents 2b910e8 + 0511187 commit 167ef34
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install:
- sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make
- sudo luarocks install luasec 0.5-2 OPENSSL_DIR=/usr OPENSSL_LIBDIR=/usr/lib/x86_64-linux-gnu
- sudo luarocks install luafilesystem 1.6.2-2
- sudo luarocks install busted 2.0.rc3-0
- sudo luarocks install busted 2.0.rc8-0
- sudo luarocks install lua-cjson 2.1.0-1
- sudo luarocks install lapis 1.0.4-1
- sudo luarocks install moonscript 0.2.6-1
Expand Down
4 changes: 2 additions & 2 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Session data is information about the request that redx has generated. It is a t
The parameter is anything of your choosing. You specify it in the config file, but you can only have one param (but you're free to use a table if you want)

## Pre
The `pre` function is run after the frontend and backend is pulled from redis, but before a server has been choosen to route to. Some examples of what you could use the `pre` function to do are, custom authorization, backend rate limiting, test for required headers, etc. This function should **always** return `nil` unless you want to halt the request with an error code and message. If you wish to halt the request and respond with an error code and message, return a table with `status` as the error code and `message` as the message.
The `pre` function is run after the frontend and backend is pulled from redis, but before a server has been choosen to route to. Some examples of what you could use the `pre` function to do are, custom authorization, backend rate limiting, test for required headers, etc. This function should **always** return `nil` unless you want to halt the request with an error code and message. If you wish to halt the request and respond with an error code and message, return a table with `status` as the error code and `message` as the message. You can also return an optional `content\_type` (default is "text/plain")

#### Example
```moonscript
M.pre = (request, session, param) ->
if param == "call it quits"
return status: 500, message: "I'm calling it quits"
return status: 500, message: "I'm calling it quits", content_type: "text/plain"
else
return nil
```
Expand Down
10 changes: 4 additions & 6 deletions lua/conf/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ M.redis_host = '127.0.0.1'
M.redis_port = '6379'
M.redis_password = ''
M.redis_timeout = 5000
M.redis_keepalive_pool_size = 5
M.redis_keepalive_max_idle_timeout = 10000
M.redis_keepalive_pool_size = 100
M.redis_keepalive_max_idle_timeout = 30000
M.max_path_length = 1
M.session_length = 900
M.plugins = {
'random'
}
M.session_length = 0
M.default_score = 0
M.plugins = {'random'}
return M
6 changes: 5 additions & 1 deletion lua/src/bin/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ process_response = function(response)
if not (response['message']) then
response['message'] = "Unknown failure."
end
ngx.header["Content-type"] = "text/plain"
if response['content_type'] then
ngx.header["Content-type"] = response['content_type']
else
ngx.header["Content-type"] = "text/plain"
end
ngx.status = response['status']
ngx.say(response['message'])
return ngx.exit(response['status'])
Expand Down
5 changes: 4 additions & 1 deletion src/bin/main.moon
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ process_response = (response) ->
response = {} unless type(response) == 'table' -- we only accept tables as the response, enforcing here
response['status'] = 500 unless response['status'] -- default status
response['message'] = "Unknown failure." unless response['message'] -- default message
ngx.header["Content-type"] = "text/plain"
if response['content_type']
ngx.header["Content-type"] = response['content_type']
else
ngx.header["Content-type"] = "text/plain"
ngx.status = response['status']
ngx.say(response['message'])
ngx.exit(response['status'])
Expand Down

0 comments on commit 167ef34

Please sign in to comment.