Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 217a98b
Showing
21 changed files
with
7,813 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
*.rdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
default: | ||
coffee app.coffee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: coffee app.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
express = require 'express' | ||
crypto = require 'crypto' | ||
Log = require('log') | ||
log = new Log('debug') | ||
Url = require 'url' | ||
|
||
redis = require 'redis' | ||
client = redis.createClient() | ||
|
||
app = express.createServer() | ||
|
||
app.set 'view engine', 'jade' | ||
|
||
app.use express.bodyParser() | ||
app.use express.cookieParser() | ||
app.use express.session secret: "asdajchklshjlawu4hvjbl" | ||
app.use express.static('public/') | ||
|
||
app.get '/', (req, res)-> | ||
res.render 'index' | ||
|
||
app.get '/register', (req, res, next)-> | ||
url = normalizeUrl req.query.url | ||
|
||
return next() unless url | ||
|
||
code = hashUrl(url) | ||
|
||
client.set "url:#{code}", url, (err, reply)-> | ||
log.info "generate code #{code}" | ||
req.session.code = code | ||
req.session.url = url | ||
res.redirect '/create' | ||
|
||
app.get '/create', (req, res)-> | ||
res.render 'create' | ||
code: req.session.code | ||
|
||
app.get /^\/([a-zA-Z0-9]{4,6})$/, (req, res, next)-> | ||
code = req.params[0] | ||
client.get "url:#{code}", (err, reply)-> | ||
return next(err) if err | ||
log.info "redirect user to #{reply}" | ||
client.hincrby 'hits', reply, 1 | ||
res.redirect reply | ||
|
||
app.listen '3000' | ||
|
||
hashUrl = (url)-> | ||
symbals = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' | ||
length = symbals.length | ||
digits = 5 | ||
code = "" | ||
|
||
hash = crypto.createHash('md5') | ||
.update(url) | ||
.digest('hex') | ||
|
||
hash_number = parseInt(hash, 16) % Math.pow(length, digits + 1) | ||
|
||
while hash_number >= length | ||
n = hash_number % length | ||
code += symbals[n] | ||
hash_number = parseInt(hash_number / length) | ||
|
||
code | ||
|
||
normalizeUrl = (url)-> | ||
urlObject = Url.parse url | ||
if not urlObject.protocol? | ||
'http://' + urlObject.href | ||
else | ||
urlObject.href |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"author": "Jimmy Chao <daizenga@gmail.com> (neethack.com)", | ||
"name": "url_shortener", | ||
"version": "0.0.0", | ||
"repository": { | ||
"url": "" | ||
}, | ||
"main": "app.coffee", | ||
"engines": { | ||
"node": "~0.6.3" | ||
}, | ||
"dependencies": { | ||
"express": "~3.0.0rc1", | ||
"redis": "~0.7.2", | ||
"jade": "~0.26.3", | ||
"crypto": "0.0.3", | ||
"url": "~0.7.9", | ||
"log": "~1.3.0", | ||
"coffee-script": "~1.3.3" | ||
}, | ||
"devDependencies": {} | ||
} |
Binary file not shown.
Oops, something went wrong.