Skip to content

Instantly share code, notes, and snippets.

@lcrilly
Created January 12, 2021 19:09
Show Gist options
  • Select an option

  • Save lcrilly/12e2e49979a0d9b5e2e945daf910ddca to your computer and use it in GitHub Desktop.

Select an option

Save lcrilly/12e2e49979a0d9b5e2e945daf910ddca to your computer and use it in GitHub Desktop.
NGINX echo service
js_import conf.d/echo.js;
map $request_method $return_code {
"POST" 201;
"DELETE" 204;
default 200;
}
keyval_zone zone=kv:32k;
keyval '1' $has_keyval zone=kv;
server {
listen 8000-8080;
ignore_invalid_headers off;
default_type application/json;
mirror /_null;
location / {
js_content echo.echo;
}
location /_null {
internal;
return 204;
}
set $has_keyval 1;
#location /api/ { api; }
}
# vim: syntax=nginx
export default {echo}
function echo(r) {
var headers = {};
for (var h in r.headersIn) {
headers[h] = r.headersIn[h];
}
var req = { "client": r.variables.remote_addr, "port": Number(r.variables.server_port), "host": r.variables.host, "method": r.variables.request_method, "uri": r.uri, "httpVersion": r.httpVersion, "headers": headers, "body": r.variables.request_body }
var res = { "status": Number(r.variables.return_code), "timestamp": r.variables.time_iso8601 }
r.return(Number(r.variables.return_code), JSON.stringify({ "request": req, "response": res }) + '\n');
}
split_clients $request_id $emoji {
5% 🧠;
5% πŸ§‘β€πŸ’»;
5% πŸ§‘β€πŸš€;
5% πŸ•Ί;
5% πŸ§—β€β™€οΈ;
5% 🏎;
5% 🦏;
5% 🦩;
5% 🚁;
5% πŸͺ;
5% πŸ¦–;
5% 🧨;
5% 🌻;
5% 🎲;
5% πŸ„;
5% 🧒;
5% πŸ§€;
* πŸ’©;
}
split_clients $request_id $emoji_hex {
5% 26BE; # ⚾
5% 26FA; # β›Ί
5% 1F30E; # 🌎
5% 1F33D; # 🌽
5% 1F3C4; # πŸ„
5% 1F400; # πŸ€
5% 1F527; # πŸ”§
5% 1F5FF; # πŸ—Ώ
5% 1F6E9; # πŸ›©
5% 1F933; # 🀳
10% 1F680; # πŸš€
10% 1F369; # 🍩
10% 1F69C; # 🚜
10% 1F995; # πŸ¦•
* 1F4A9; # πŸ’©
}
server {
listen 80;
location / {
add_header Content-Type text/html;
return 200 '<!DOCTYPE html>\n<html>\n<head>\n<title>Emoji Surprise!</title></head><body bgcolor="#$request_id;">\n
<p style="font-family:sans-serif;font-size:50px">Your emoji surprise!</p>\n
<p style="font-size:200px">&nbsp;&#x$emoji_hex;</p>\n</body>\n</html>\n';
}
location = /favicon.ico {
add_header Content-Type image/svg+xml;
return 200 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">$emoji</text></svg>';
}
}
# vim: syntax=nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment