Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http = require 'http'
- fs = require 'fs'
- url = require 'url'
- mime = require 'mime'
- net = require 'net'
- checkDomain = (domain, callback) ->
- dataCheck = false
- result = 'fail'
- client = net.connect {port: 43, host: 'whois.nic.io'}, ->
- client.write domain + '\r\n'
- client.on 'error', (error) ->
- console.log error
- client.on 'data', (data) ->
- dataCheck = true
- response = data.toString()
- result =
- if (response.indexOf 'Not available') != -1
- 'taken'
- else if (response.indexOf 'Available') != -1
- 'available'
- else
- 'other'
- client.on 'end', ->
- if !dataCheck
- checkDomain domain, callback
- else
- callback(result)
- routes =
- '':
- file: 'html/domains.html'
- 'domains':
- action: -> [JSON.stringify domains, 'application/json']
- 'main.css':
- file: 'css/main.css'
- '404':
- file: 'html/404.html'
- 'coffee-script.js':
- file: 'scripts/coffee-script.js'
- 'jquery-1.8.0.min.js':
- file: 'scripts/jquery-1.8.0.min.js'
- 'check':
- action:
- (path, callback) ->
- domain = path[2] ? ''
- domain = domain.slice 0, 50
- if (domain.indexOf '.io') == -1
- domain += '.io'
- checkDomain domain, (result) -> callback (JSON.stringify {name: domain, status: result}), 'application/json'
- null
- replaceVariables = (string, map) ->
- for key, value of map
- string = string.replace "{#{key}}", value
- string
- createAbsolutePath = (relativePath) -> './' + relativePath
- selectRoute = (name) -> routes[name] ? routes['404']
- server = http.createServer (request, response) ->
- respond = (data, mimeType) ->
- response.writeHead 200, 'Content-Type': mimeType
- response.write data
- response.end()
- path = (url.parse request.url).pathname.split('/')
- console.log path[1]
- route = selectRoute (path[1] ? '404')
- if (route.file?)
- fullPath = createAbsolutePath route.file
- mimeType = mime.lookup fullPath
- fs.readFile fullPath, (error, data) ->
- respond route.action?(data.toString()) ? data.toString(), mimeType
- else
- result = route.action path, respond
- if (result != null)
- [data, mimeType] = result
- respond data, mimeType
- domains = {io: []}
- for tld of domains
- domains[tld] = for line in fs.readFileSync("data/#{tld}.txt").toString().split('\n')
- parts = line.split '\t'
- [parts[0], parseInt parts[1], 10]
- server.listen 3000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement