Permalink
Browse files

Added deployed codes for archive

  • Loading branch information...
1 parent 0ed3ddd commit a3f9839f09e3311a0f02e16ffc0693d8a2c83a31 @wilkinson committed May 19, 2012
Showing with 76 additions and 106 deletions.
  1. +70 −103 templates/backend-nginx-couchdb/etc/nginx.conf
  2. +6 −3 templates/backend-nginx-couchdb/server.js
View
173 templates/backend-nginx-couchdb/etc/nginx.conf
@@ -3,7 +3,7 @@
#- nginx.conf ~~
# ~~ (c) SRW, 03 May 2012
-error_log /var/log/nginx/error.log;
+error_log /var/log/nginx/error.log notice;
events {
@@ -15,7 +15,7 @@ http {
default_type application/octet-stream;
- #gzip on;
+ gzip on;
keepalive_timeout 65;
@@ -27,135 +27,102 @@ http {
server {
- listen 80;
- server_name localhost;
+ listen 80 default_server;
+ server_name qmachine.org localhost;
- access_log /var/log/nginx/localhost.access.log;
+ access_log /var/log/nginx/qmachine.access.log;
+
+ rewrite_log on;
if ($request_method !~ ^(GET|OPTIONS|POST)$ ) {
return 444;
}
- location / {
+ add_header 'Access-Control-Allow-Origin' '*';
- # This matches any query, since all queries begin with '/', but
- # regular expressions and any longer conventional blocks will be
- # matched first.
+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
- if ($request_method = 'GET') {
+ add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept';
- add_header 'Access-Control-Allow-Origin' '*';
+ add_header 'Access-Control-Max-Age' '10'; # seconds
- }
+ if ($request_method = 'OPTIONS') {
+ #add_header 'Content-Length' '0';
+ return 204;
+ }
- if ($request_method = 'OPTIONS') {
+ set $qbit_b 0;
+ set $qbit_g 0;
+ set $qbit_k 0;
+ set $qbit_p 0;
+ set $qbit_s 0;
- add_header
- 'Access-Control-Allow-Origin' '*';
- add_header
- 'Access-Control-Allow-Methods' 'GET, OPTIONS';
- add_header
- 'Access-Control-Allow-Headers' 'Content-Type, Accept';
- add_header
- 'Access-Control-Max-Age' '1000'; # seconds
- add_header
- 'Content-Length' '0';
+ if ($uri ~ ^/box/([^\&\/]+)$) {
+ # See: http://wiki.nginx.org/HttpCoreModule
+ set $q_box $1;
+ set $qbit_b 1;
+ }
- return 204;
+ if ($request_method = 'GET') {
+ set $qbit_g 1;
+ }
- }
+ if ($args ~ ^key=([^\&]+)$) {
+ set $q_key $1;
+ set $qbit_k 1;
+ }
- rewrite /(.*) /www/_design/app/$1 break;
+ if ($request_method = 'POST') {
+ set $qbit_p 1;
+ }
- proxy_pass http://couchdb/;
+ if ($args ~ ^status=([^\&]+)$) {
+ set $q_status $1;
+ set $qbit_s 1;
+ }
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ set $qbit_mask "${qbit_b}${qbit_g}${qbit_k}${qbit_p}${qbit_s}";
- # This one isn't given on http://goo.gl/ruyY ...
- #proxy_set_header X-Real-IP $remote_addr;
+ if ($qbit_mask = 11100) {
+ # GET from box by key
+ rewrite ^ /db/_design/app/_show/data/$q_box&$q_key? break;
+ }
- #proxy_ignore_headers X-Couch-Update-NewRev;
+ if ($qbit_mask = 10110) {
+ # POST to box by key
+ rewrite ^ /db/_design/app/_update/timestamp/$q_box&$q_key? break;
+ }
+ if ($qbit_mask = 11001) {
+ # GET from box by status
+ rewrite ^ /db/_design/app/_list/as-array/jobs?key=[%22$q_box%22,%22$q_status%22]? break;
}
- location /box/ {
+ if ($qbit_mask ~ ^01) {
+ # GET static content ...
+ rewrite /(.*) /www/_design/app/_rewrite/$1 break;
+ }
- # This matches any query beginning with '/box/', but I can't tell if
- # it halts searching or not ...
+ # if we fall through this far, it needs to be denied as 444 ... ?
- proxy_pass http://couchdb/;
+ #add_header 'Access-Control-Allow-Origin' '*';
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
-
- # Good grief, the `if` logic is really difficult in Nginx. I had to
- # resort to a bitmask approach ...
-
- set $q_get 0;
- set $q_key 0;
- set $q_post 0;
- set $q_status 0;
-
- if ($request_method = GET) {
- add_header 'Access-Control-Allow-Origin' '*';
- set $q_get 1;
- }
-
- if ($args ~* ^key=([^\&]+)$) {
- set $key $1;
- set $q_key 1;
- }
-
- if ($request_method = POST) {
- add_header 'Access-Control-Allow-Origin' '*';
- set $q_post 1;
- }
-
- if ($args ~* ^status=([^\&]+)$) {
- set $q_status 1;
- set $status $1;
- }
-
- set $q_bitmask "${q_get}${q_key}${q_post}${q_status}";
-
- if ($q_bitmask = 1100) {
- # GET by key
- rewrite ^/box/([^\&]+)
- /db/_design/app/_show/data/$1&$key?
- break;
- }
-
- if ($q_bitmask = 1001) {
- # GET by status
- rewrite ^/box/([^\&]+)
- /db/_design/app/_list/as-array/jobs?key=["$1","$status"]
- break;
- }
-
- if ($q_bitmask = 0110) {
- # POST by key
- rewrite ^/box/([^\&]+)
- /db/_design/app/_update/timestamp/$1&$key?
- break;
- }
-
- add_header
- 'Access-Control-Allow-Origin' '*';
- add_header
- 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
- add_header
- 'Access-Control-Allow-Headers' 'Content-Type, Accept';
- add_header
- 'Access-Control-Max-Age' '10'; # seconds
- add_header
- 'Content-Length' '0';
- return 204;
+ proxy_set_header Host $host;
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+
+ location / {
+ proxy_pass http://couchdb/;
+ }
+ location ~ /box/([^\&\/]+)$ {
+ proxy_pass http://couchdb;
}
+ #proxy_redirect off;
+
+ # This one isn't given on http://goo.gl/ruyY ...
+ #proxy_set_header X-Real-IP $remote_addr;
+
}
}
View
9 templates/backend-nginx-couchdb/server.js
@@ -85,11 +85,12 @@
// Case 1
outer_res.writeHead('204', '(no content)', {
'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Methods': 'GET, OPTIONS, POST',
+ 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Accept',
'Access-Control-Max-Age': 10, //- seconds
'Content-Length': 0
});
+ outer_res.end();
return;
}
if ((outer_req.method !== 'GET') && (outer_req.method !== 'POST')) {
@@ -98,10 +99,11 @@
outer_res.end();
return;
}
+ outer_res.setHeader('Access-Control-Allow-Origin', '*');
pat = /^\/box\/([^\&\/]+)[?](key|status)[=]([^\&]+)$/;
target = outer_req.url.replace(pat, function (all, box, pkey, pval) {
// This function needs documentation.
- var y = config.app;
+ var y = config.app + '/_';
if (pkey === 'key') {
if (outer_req.method === 'GET') {
return y + 'show/data/' + box + '&' + pval;
@@ -113,9 +115,10 @@
if (target === outer_req.url) {
options = url.parse(config.www + outer_req.url);
} else {
- options = url.parse(target);
+ options = url.parse(encodeURI(target));
}
options.headers = outer_req.headers;
+ options.headers['Content-Type'] = 'application/json';
options.method = outer_req.method;
inner_req = http.request(options, function (inner_res) {
// This function needs documentation.

0 comments on commit a3f9839

Please sign in to comment.