Skip to content

Instantly share code, notes, and snippets.

@niquola
Last active August 29, 2015 14:03
Show Gist options
  • Save niquola/c44a6150bb568bda76a0 to your computer and use it in GitHub Desktop.
Save niquola/c44a6150bb568bda76a0 to your computer and use it in GitHub Desktop.
web stacks
> Gives more than one object an opportunity
> to handle a request by linking receiving objects together.
//Handler
public interface EmailHandler
{
//reference to the next handler in the chain
public void setNext(EmailHandler handler);
//handle request
public void handleRequest(Email email);
}
public class BusinessMailHandler implements EmailHandler
{
private EmailHandler next;
public void setNext(EmailHandler handler)
{
next = handler;
}
public void handleRequest(Email email)
{
if(!email.getFrom().endsWith("@businessaddress.com")
{
next.handleRequest(email);
}
else
{
//handle request (move to correct folder)
}
}
}
def hello(environ, start_response):
start_response('200 OK', [('Content-type','text/plain')])
return ["Hello World!"]
# The Web Server Gateway Interface is a Python standard created in 2003 by Philip J. Eby
class Middleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return self.app(environ, start_response)
app = Middleware(hello)
from wsgiref import simple_server
httpd = simple_server.WSGIServer(
('0.0.0.0', 8000),
simple_server.WSGIRequestHandler,
)
httpd.set_app(app)
httpd.serve_forever()
require 'rack'
app = Proc.new do |env|
['200', {'Content-Type' => 'text/html'}, ['A barebones rack app.']]
end
class Middleware
def initialize(app)
@app = app
end
def call(env)
# transform request
new_env = cond? ? transform(env) : env
# intercept
return [status, headers, res] if cond?
# transform responce
status, headers, response = @app.call(new_env)
new_responce cond? ? transform(response) : response
[status, headers, new_response]
end
end
use Middleware
Rack::Handler::WEBrick.run app
var connect = require('connect')
var http = require('http')
var app = connect()
// gzip/deflate outgoing responses
var compression = require('compression')
app.use(compression())
// list of banned IPs
var banned = [
'127.0.0.1',
'192.168.2.12'
];
// the middleware function
blacklist = function() {
return function(req, res, next) {
if (banned.indexOf(req.connection.remoteAddress) > -1) {
res.end('Banned');
}
else { next(); }
}
};
app.use(blacklist());
// respond to all requests
app.use(function(req, res){
res.end('Hello from Connect!\n');
})
//create node.js http server and listen on port
http.createServer(app).listen(3000)
(defn handler [req]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello World"})
(defn middleware [h & opt]
(fn [req]
(let [res (h req)]
(transform res)))
(def app (-> handler middleware mw2 mw3))
(use 'ring.adapter.jetty)
(use 'hello-world.core)
(run-jetty app {:port 3000})
@niquola
Copy link
Author

niquola commented Jun 25, 2014

  • json request
  • REST methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment