Created
January 12, 2021 19:09
-
-
Save lcrilly/12e2e49979a0d9b5e2e945daf910ddca to your computer and use it in GitHub Desktop.
NGINX echo service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"> &#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