JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
serve js from /javascript, make cards draggable
[peach-cgt.git] / server.coffee
index fad6347..48e2857 100644 (file)
@@ -26,6 +26,24 @@ html_handler = (args, out, request, url_parts) ->
                if err
                        return out.end 'Server failed to read index.html'
                out.end data
+
+clean_pathname_regex = new RegExp('[^a-zA-Z/_.-]')
+clean_pathname_regex2 = new RegExp('/[.]')
+clean_pathname_regex3 = new RegExp('^[.-]')
+clean_pathname = (str) ->
+       str = str.replace clean_pathname_regex, '_'
+       str = str.replace clean_pathname_regex2, '/_'
+       return str.replace clean_pathname_regex3, '_'
+       
+# serve javascript files from within /usr/share/javascript
+javascript_handler = (args, out, request, url_parts) ->
+       filename = clean_pathname "/usr/share/#{url_parts.pathname}"
+       fs.readFile filename, 'utf8', (err, data) ->
+               if err
+                       out.writeHead 404
+                       return out.end "Server failed to read #{filename}"
+               out.writeHead 200, 'Content-Type': 'text/javascript'
+               out.end data
        
 
 http_server = http.createServer (req, res) ->
@@ -35,7 +53,9 @@ http_server = http.createServer (req, res) ->
 
        rel_path = url_parts.pathname.substr 1
 
-       if rel_path.substr(rel_path.length - 4) is '.css'
+       if rel_path.substr(0, 11) is 'javascript/'
+               return javascript_handler url_parts.query, res, req, url_parts
+       else if rel_path.substr(rel_path.length - 4) is '.css'
                res.writeHead 200, 'Content-Type': 'text/css'
                return css_handler url_parts.query, res, req, url_parts
        else if rel_path.substr rel_path.length - 3 is '.js'