Skip to content

Instantly share code, notes, and snippets.

@lcrilly
Last active October 1, 2022 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcrilly/3449f53b4048297213a8e32ac9c5856d to your computer and use it in GitHub Desktop.
Save lcrilly/3449f53b4048297213a8e32ac9c5856d to your computer and use it in GitHub Desktop.
Unit intro demo

Simple demo for NGINX Unit

JSON/REST API for simple addition in several languages and a hello world web page.

echo '{"operands": [100,25]}' | curl -d@- localhost:9000/add
import json
def application(environ, start_response):
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
request_json = json.loads(request_body)
response = { "result": 0 }
for value in request_json['operands']:
response['result'] += value
start_response('200 OK', [('Content-Type', 'application/json')])
yield str.encode(json.dumps(response))
import json
def application(environ, start_response):
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
request_json = json.loads(request_body)
response = { "result": 0 }
for value in request_json['operands']:
response['result'] += value
start_response('200 OK', [('Content-Type', 'application/json')])
yield str.encode(json.dumps(response))
require "json"
app = Proc.new do |env|
result = nil
body = JSON.parse(env["rack.input"].read)
body["operands"].each { |operand|
result.nil? ? result = operand : result += operand
}
["200", {
"Content-Type" => "application/json; charset=utf-8",
}, [JSON.pretty_generate({'result' => result})]]
end;
run app
{
"listeners": {
"*:9000": {
"pass": "routes"
}
},
"routes": [
{
"match": {
"uri": "/add"
},
"action": {
"pass": "applications/add"
}
},
{
"action": {
"share": "/var/www/demo/html$uri"
}
}
],
"applications": {
"add": {
"type": "python",
"path": "/var/www/demo/",
"module": "add"
}
}
}
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello world!</h1>
<p>
Powered by NGINX Unit
</p>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment