Advertisement
Guest User

Code

a guest
Aug 19th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. http = require 'http'
  2. fs = require 'fs'
  3. url = require 'url'
  4. mime = require 'mime'
  5. net = require 'net'
  6.  
  7. checkDomain = (domain, callback) ->
  8.     dataCheck = false
  9.     result = 'fail'
  10.  
  11.     client = net.connect {port: 43, host: 'whois.nic.io'}, ->
  12.         client.write domain + '\r\n'
  13.  
  14.     client.on 'error', (error) ->
  15.         console.log error
  16.  
  17.     client.on 'data', (data) ->
  18.         dataCheck = true
  19.         response = data.toString()
  20.         result =
  21.             if (response.indexOf 'Not available') != -1
  22.                 'taken'
  23.             else if (response.indexOf 'Available') != -1
  24.                 'available'
  25.             else
  26.                 'other'
  27.     client.on 'end', ->
  28.         if !dataCheck
  29.             checkDomain domain, callback
  30.         else
  31.             callback(result)
  32.  
  33. routes =
  34.     '':
  35.         file: 'html/domains.html'
  36.     'domains':
  37.         action: -> [JSON.stringify domains, 'application/json']
  38.     'main.css':
  39.         file: 'css/main.css'
  40.     '404':
  41.         file: 'html/404.html'
  42.     'coffee-script.js':
  43.         file: 'scripts/coffee-script.js'
  44.     'jquery-1.8.0.min.js':
  45.         file: 'scripts/jquery-1.8.0.min.js'
  46.     'check':
  47.         action:
  48.             (path, callback) ->
  49.                 domain = path[2] ? ''
  50.                 domain = domain.slice 0, 50
  51.                 if (domain.indexOf '.io') == -1
  52.                     domain += '.io'
  53.                 checkDomain domain, (result) -> callback (JSON.stringify {name: domain, status: result}), 'application/json'
  54.                 null
  55.                
  56.  
  57. replaceVariables = (string, map) ->
  58.     for key, value of map
  59.         string = string.replace "{#{key}}", value
  60.     string
  61.  
  62. createAbsolutePath = (relativePath) -> './' + relativePath
  63. selectRoute = (name) -> routes[name] ? routes['404']
  64.  
  65. server = http.createServer (request, response) ->
  66.     respond = (data, mimeType) ->
  67.         response.writeHead 200, 'Content-Type': mimeType
  68.         response.write data
  69.         response.end()
  70.  
  71.     path = (url.parse request.url).pathname.split('/')
  72.     console.log path[1]
  73.     route = selectRoute (path[1] ? '404')
  74.  
  75.     if (route.file?)
  76.         fullPath = createAbsolutePath route.file
  77.         mimeType = mime.lookup fullPath
  78.  
  79.         fs.readFile fullPath, (error, data) ->
  80.             respond route.action?(data.toString()) ? data.toString(), mimeType
  81.     else
  82.         result = route.action path, respond
  83.         if (result != null)
  84.             [data, mimeType] = result
  85.             respond data, mimeType
  86.  
  87. domains = {io: []}
  88.  
  89. for tld of domains
  90.     domains[tld] = for line in fs.readFileSync("data/#{tld}.txt").toString().split('\n')
  91.         parts = line.split '\t'
  92.         [parts[0], parseInt parts[1], 10]
  93.  
  94. server.listen 3000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement