-
-
Save SimonRichardson/c156a7319c4dbdf9c5b7 to your computer and use it in GitHub Desktop.
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
require ('./globals')(global); // Mixed in Ramda to global here | |
var Task = require ('data.future'), | |
M = require ('control.monads'), | |
State = require ('fantasy-states'), | |
Reader = require ('fantasy-readers'), | |
Tuple2 = require ('fantasy-tuples').Tuple2, | |
Maybe = require ('data.maybe'), | |
ST = State.StateT (Task), | |
App = Reader.ReaderT (ST); | |
App.liftT = function (t) { | |
return App.liftR (ST (function (s) { | |
return new Task (function (reject, resolve) { | |
t.fork (reject, function (data) { | |
return resolve (Tuple2 (data, s)); | |
}); | |
}); | |
})); | |
}; | |
App.liftR = App.lift; | |
App.lift = compose (App.liftR, ST.lift); | |
App.prototype.map = function (f) { return this.chain (compose (App.of, f)); }; | |
// For inserting custom lift function | |
App.prototype.mapLift = curry (function (lift, f) { | |
return this.chain (compose (lift, f)); | |
}); | |
var | |
// getPathFromRequest :: Request -> String | |
getPathFromRequest = compose (get ('path'), get ('file'), get ('files')), | |
// savePath :: App -> App | |
savePath = App.ask.chain (function (r) { | |
var path = getPathFromRequest (r); | |
return App.liftR (ST.modify ( mixin ({ path: path }) )); | |
}), | |
// getPath :: App -> App | |
getPath = App.liftR (ST.get).map (get ('path')), | |
// getFileHash :: Path -> Task Hash | |
getFileHash = function (path) { | |
return new Task (function (reject, resolve) { | |
require ('child_process').exec ('md5sum '+ path, function (err, stdout, stderr) { | |
if (err) { return reject (err); } | |
return compose (resolve, head, split (/\s/)) (stdout); | |
}); | |
}); | |
}, | |
// saveHash :: Hash -> App | |
saveHash = function (hash) { | |
return App.liftR (ST.modify ( mixin ({ hash: hash }) )); | |
}, | |
getFileHash = M.sequence (App, [ savePath, getPath .chain (compose ( App.liftT, getFileHash )) ]), | |
nil = null; | |
//============================================================================== | |
// Impure | |
//============================================================================== | |
var express = require ('express'), | |
bodyParser = require ('body-parser'), | |
cookieParser = require ('cookie-parser'), | |
multer = require ('multer'), | |
app = express (), | |
errJson = curry (function (code, res, data) { res.send (code, data); }), | |
succJson = curry (function (res, data) { res.json (data || {}); }), | |
appResponse = curry (function (app, req, res) { | |
var defaultState = {}; | |
app.run (req) | |
.evalState (defaultState) | |
.fork ( | |
errJson (401, res), | |
succJson (res) | |
); | |
}), | |
ask = App.ask, | |
nil = null; | |
app.use (express.static (__dirname + '/public')); | |
app.use (bodyParser.urlencoded ({ extended: true })); | |
app.use (bodyParser.json ()); | |
app.use (multer ({ dest: './uploads/' })); | |
app.get ('/', function (req, res) { | |
res.send (compose (toString, add (2))(4)); | |
}); | |
app.post ('/upload', appResponse (getFileHash)); | |
app.listen (7171); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment