Permalink
Browse files

messin around

  • Loading branch information...
0 parents commit eaa11deea26d8dbdfd0766e6f705bb8513d299c7 @leafo committed May 2, 2014
Showing with 197 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +1 −0 Tupfile
  3. +1 −0 Tuprules.tup
  4. +79 −0 mime.types
  5. +32 −0 nginx.conf
  6. +23 −0 test_server.moon
  7. +57 −0 web.moon
@@ -0,0 +1,4 @@
+*.lua
+.tup
+logs/
+nginx.conf.compiled
@@ -0,0 +1 @@
+include_rules
@@ -0,0 +1 @@
+: foreach *.moon |> moonc %f |> %B.lua
@@ -0,0 +1,79 @@
+types {
+ text/html html htm shtml;
+ text/css css;
+ text/xml xml;
+ image/gif gif;
+ image/jpeg jpeg jpg;
+ application/x-javascript js;
+ application/atom+xml atom;
+ application/rss+xml rss;
+
+ text/mathml mml;
+ text/plain txt;
+ text/vnd.sun.j2me.app-descriptor jad;
+ text/vnd.wap.wml wml;
+ text/x-component htc;
+
+ image/png png;
+ image/tiff tif tiff;
+ image/vnd.wap.wbmp wbmp;
+ image/x-icon ico;
+ image/x-jng jng;
+ image/x-ms-bmp bmp;
+ image/svg+xml svg svgz;
+ image/webp webp;
+
+ application/java-archive jar war ear;
+ application/mac-binhex40 hqx;
+ application/msword doc;
+ application/pdf pdf;
+ application/postscript ps eps ai;
+ application/rtf rtf;
+ application/vnd.ms-excel xls;
+ application/vnd.ms-powerpoint ppt;
+ application/vnd.wap.wmlc wmlc;
+ application/vnd.google-earth.kml+xml kml;
+ application/vnd.google-earth.kmz kmz;
+ application/x-7z-compressed 7z;
+ application/x-cocoa cco;
+ application/x-java-archive-diff jardiff;
+ application/x-java-jnlp-file jnlp;
+ application/x-makeself run;
+ application/x-perl pl pm;
+ application/x-pilot prc pdb;
+ application/x-rar-compressed rar;
+ application/x-redhat-package-manager rpm;
+ application/x-sea sea;
+ application/x-shockwave-flash swf;
+ application/x-stuffit sit;
+ application/x-tcl tcl tk;
+ application/x-x509-ca-cert der pem crt;
+ application/x-xpinstall xpi;
+ application/xhtml+xml xhtml;
+ application/zip zip;
+
+ application/octet-stream bin exe dll;
+ application/octet-stream deb;
+ application/octet-stream dmg;
+ application/octet-stream eot;
+ application/octet-stream iso img;
+ application/octet-stream msi msp msm;
+
+ audio/midi mid midi kar;
+ audio/mpeg mp3;
+ audio/ogg ogg;
+ audio/x-m4a m4a;
+ audio/x-realaudio ra;
+
+ video/3gpp 3gpp 3gp;
+ video/mp4 mp4;
+ video/mpeg mpeg mpg;
+ video/quicktime mov;
+ video/webm webm;
+ video/x-flv flv;
+ video/x-m4v m4v;
+ video/x-mng mng;
+ video/x-ms-asf asx asf;
+ video/x-ms-wmv wmv;
+ video/x-msvideo avi;
+}
@@ -0,0 +1,32 @@
+worker_processes ${{NUM_WORKERS}};
+error_log stderr notice;
+daemon off;
+
+events {
+ worker_connections 1024;
+}
+
+http {
+ include mime.types;
+
+ upstream database {
+ postgres_server 127.0.0.1:35004 user=postgres password= dbname=moonrocks;
+ }
+
+
+ server {
+ listen ${{PORT}};
+ lua_code_cache ${{CODE_CACHE}};
+
+ location / {
+ default_type text/html;
+ content_by_lua_file "web.lua";
+ }
+
+ location /query {
+ postgres_connect_timeout 1s;
+ postgres_pass database;
+ postgres_query "SELECT * FROM cats";
+ }
+ }
+}
@@ -0,0 +1,23 @@
+
+socket = require "socket"
+server = socket.bind "*", 35004
+print server\getsockname!
+
+while true
+ client = server\accept!
+ print "Got client:", client\getsockname!
+
+ while true
+ c, err = client\receive(1)
+ break unless c
+ b = c\byte!
+
+ if b >= 32 and b <= 126
+ print " #{b}\t`#{c}`"
+ else
+ print " #{b}"
+
+ print "\nDisconnected"
+
+
+
@@ -0,0 +1,57 @@
+lapis = require "lapis"
+
+class Postgres
+ AUTH_REQ_OK = 0
+
+ import rshift, lshift, band from require "bit"
+
+ new: (@host, @port, @db) =>
+
+ connect: =>
+ @sock = ngx.socket.tcp!
+ ok, err = @sock\connect @host, tonumber @port
+ return nil, err unless ok
+ msg = "R#{@encode_int AUTH_REQ_OK}"
+
+ receive_message: =>
+ t, err = @sock\receive 1
+ return nil, "failed to get type: #{err}" unless t
+ len, err = @sock\receive 4
+ return nil, "failed to get len: #{err}" unless len
+ len = @decode_int len
+
+ send_message: (t, data, len=#data) =>
+ @sock\send t .. @encode_int(len) .. data
+
+ decode_int: (str, bytes=4) =>
+ switch bytes
+ when 4
+ d, c, b, a = str\byte 1, 4
+ a + lshift(b, 8) + lshift(c, 16) + lshift(d, 24)
+ else
+ error "don't know how to decode #{bytes} byte(s)"
+
+ -- create big endian binary string of number
+ encode_int: (n, bytes=4) =>
+ switch bytes
+ when 4
+ a = band n, 0xff
+ b = band rshift(n, 8), 0xff
+ c = band rshift(n, 16), 0xff
+ d = band rshift(n, 24), 0xff
+ string.char d, c, b, a
+ else
+ error "don't know how to encode #{bytes} byte(s)"
+
+
+lapis.serve class extends lapis.Application
+ "/": =>
+ p = Postgres "127.0.0.1", "5432", "moonrocks"
+
+ @html ->
+ text ":"
+ pre require("moon").dump {
+ -- p\connect!
+ }
+
+

0 comments on commit eaa11de

Please sign in to comment.