Django middleware vs Openresty:
Django is a python framework suitable for building and deploying production quality web application software. It enables model, view controller framework out of the box for fast web application development. In addition, it integrates seamlessly with rest_framework and documentation support to provide documented REST based APIs. Django also provides what is called a middleware to enable http modules or plugins to be written that can modify the request or response or mutate the View parameters. This is defined as a sequence of classes in the settings file. The hooks that middleware classes uses for their processing include request , response, view, template_response and exception. Django middleware are typically used for sessions, authentication, CSRF protection and GZipping content .
OpenResty is a full-fledged web application server that bundles the core and modules of Nginx that opens it up for developers to configure via Lua programming/scripting language. These modules and the core are written in C and are therefore highly performant and cross platform compatible. This web server is able to handle 10K plus connections. Openresty enables a developers server side web application to be run entirely within Nginx server. Nginx is known to support asynchronous IO not only with http clients but also with database backends.
To compare the two, we review a common use case – the ability to route REST API calls to different API providers. This functionality is often referred to as an API gateway.
Only some of the tradeoffs are presented as follows:
Features Django Middleware OpenResty bundle
Support request and
response modification Yes Yes
Support authentication and
authorization including OAuth Yes Yes
Support large number
of connections Yes Yes, but this is faster
Support on a wide More popular by
variety of hardware virtue of LAMP stack Not as popular
Production quality and
quality control More tested in commercial Less tested
Memory and Disk I/O Can be large Aims to be more
performant with little
memory footprint
Software support Generally not needed. Some Requires workarounds
legacy platforms are still in use and fixes
without issues, more reliable
Today we continue to do some more coding exercises.
Let us look at a Skyline problem. This appeared in Leetcode challenge. A city's skyline is the outer contour formed by the buildings. The buildings are given by (Li, Ri, Hi) where Li is the x-coordiante of the building's left edge and Ri is the x-coordinate of the building's ending edge. Hi is the height of the building. The buildings are arranged left to right in increasing order of their left edges.
The output is a list of key points where the contour changes direction. The number of building is guaranteed to be less than 10000. There must be no consecutive horizontal line of equal height and should appear merged instead.
public class Solution {
public IList<int[]> GetSkyline(int[,] buildings) {
var ret = new List<int[]>();
int x = 0;
int y = 0;
var stk = new Stack<int>();
for (int i = 0; i < buildings.Length; i++){
if (buildings[i][2] > y) {
if (stk.isEmpty() == false) {
int j = stk.Pop();
if (building[i][1] < building[j][1])
x = building[j][1];
AddToList(ref ret, x, y);
y = 0;
AddToList(ref ret, x, y);
}
stk.Push(i);
x = buildings[i][0];
AddToList(ref ret, x, y);
y = buildings[i][2];
AddToList(ref ret, x,y);
}
else
{
if (buildings[i][1] < buildings[stk.Peek()][1))
continue;
int j = stk.Pop();
x = buildings[j][1];
AddToList(ref ret, x, y);
y = buildings[i][2];
AddToList(ref ref, x, y);
stk.push(i);
}
}
}
private static void AddToList(ref List<int[]> ret, int x, int y)
{
var current = new int[2];
current[0] = x;
current[1] = y;
ret.Add(current);
}
}
Features Django Middleware OpenResty bundle
Support request and
response modification Yes Yes
Support authentication and
authorization including OAuth Yes Yes
Support large number
of connections Yes Yes, but this is faster
Support on a wide More popular by
variety of hardware virtue of LAMP stack Not as popular
Production quality and
quality control More tested in commercial Less tested
Memory and Disk I/O Can be large Aims to be more
performant with little
memory footprint
Software support Generally not needed. Some Requires workarounds
legacy platforms are still in use and fixes
without issues, more reliable
Today we continue to do some more coding exercises.
Let us look at a Skyline problem. This appeared in Leetcode challenge. A city's skyline is the outer contour formed by the buildings. The buildings are given by (Li, Ri, Hi) where Li is the x-coordiante of the building's left edge and Ri is the x-coordinate of the building's ending edge. Hi is the height of the building. The buildings are arranged left to right in increasing order of their left edges.
The output is a list of key points where the contour changes direction. The number of building is guaranteed to be less than 10000. There must be no consecutive horizontal line of equal height and should appear merged instead.
public class Solution {
public IList<int[]> GetSkyline(int[,] buildings) {
var ret = new List<int[]>();
int x = 0;
int y = 0;
var stk = new Stack<int>();
for (int i = 0; i < buildings.Length; i++){
if (buildings[i][2] > y) {
if (stk.isEmpty() == false) {
int j = stk.Pop();
if (building[i][1] < building[j][1])
x = building[j][1];
AddToList(ref ret, x, y);
y = 0;
AddToList(ref ret, x, y);
}
stk.Push(i);
x = buildings[i][0];
AddToList(ref ret, x, y);
y = buildings[i][2];
AddToList(ref ret, x,y);
}
else
{
if (buildings[i][1] < buildings[stk.Peek()][1))
continue;
int j = stk.Pop();
x = buildings[j][1];
AddToList(ref ret, x, y);
y = buildings[i][2];
AddToList(ref ref, x, y);
stk.push(i);
}
}
}
private static void AddToList(ref List<int[]> ret, int x, int y)
{
var current = new int[2];
current[0] = x;
current[1] = y;
ret.Add(current);
}
}