main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var express = require('express');
  2. var app = express();
  3. var UserManager = require("./users");
  4. var Response = require("./Response");
  5. var multer = require("multer");
  6. var path = require("path");
  7. var crypto = require("crypto");
  8. var mime = require("mime");
  9. var Server = require("./Server");
  10. var utils = require("./utils");
  11. var upload = multer({
  12. dest: 'tmp/' // this saves your file into a directory called "uploads"
  13. });
  14. var bodyParser = require('body-parser')
  15. app.use( bodyParser.json() ); // to support JSON-encoded bodies
  16. app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
  17. extended: true
  18. }));
  19. const prefix = "";
  20. const apiprefix = prefix+"/api";
  21. const appprefix = prefix+"/app";
  22. var server = new Server();
  23. var storage = multer.diskStorage({
  24. destination: function(req, file, cb) {
  25. cb(null, 'tmp/')
  26. },
  27. filename: function(req, file, cb) {
  28. crypto.pseudoRandomBytes(16, function(err, raw) {
  29. cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
  30. });
  31. }
  32. });
  33. var upload = multer({
  34. storage: storage
  35. });
  36. var userList = new UserManager();
  37. //users
  38. app.get(apiprefix+'/createUser', function (a, b) {server.createUser(a, b)});
  39. app.get(apiprefix+'/users', function (a, b) {server.listUsers(a, b)});
  40. app.get(apiprefix+'/user/:user', function (a, b) {server.serveUser(a, b)});
  41. app.post(apiprefix+'/createUser', function (a, b) {server.createUser(a, b)});
  42. app.post(apiprefix+'/users', function (a, b) {server.listUsers(a, b)});
  43. app.post(apiprefix+'/user/:user', function (a, b) {server.serveUser(a, b)});
  44. app.post(apiprefix+'/user/:user/edit', function (a, b) {server.editUser(a, b)});
  45. app.get(apiprefix+'/user/:user/rename', function (a, b) {server.renameUser(a, b)});
  46. app.post(apiprefix+'/user/:user/rename', function (a, b) {server.renameUser(a, b)});
  47. app.get(apiprefix+'/user/:user/remove', function (a, b) {server.removeUser(a, b)});
  48. app.post(apiprefix+'/user/:user/remove', function (a, b) {server.removeUser(a, b)});
  49. //Projects
  50. app.get(apiprefix+'/user/:user/createProject', function (a, b) {console.log("_1"); server.createProject(a, b)});
  51. app.get(apiprefix+'/user/:user/projects', function (a, b) {server.listProjects(a, b)});
  52. app.get(apiprefix+'/user/:user/:project', function (a, b) { server.serveProject(a, b)});
  53. app.post(apiprefix+'/user/:user/:project/edit', function (a, b) {server.editProject(a, b)});
  54. app.get(apiprefix+'/user/:user/:project/rename', function (a, b) {server.renameProject(a, b)});
  55. app.post(apiprefix+'/user/:user/:project/rename', function (a, b) {server.renameProject(a, b)});
  56. app.get(apiprefix+'/user/:user/:project/remove', function (a, b) {server.removeProject(a, b)});
  57. app.post(apiprefix+'/user/:user/:project/remove', function (a, b) {server.removeProject(a, b)});
  58. //resources
  59. app.get(apiprefix+'/user/:user/:project/createResource', function (a, b) {console.log("_1"); server.createResource(a, b)});
  60. app.get(apiprefix+'/user/:user/:project/resources', function (a, b) {server.listResources(a, b)});
  61. app.get(apiprefix+'/user/:user/:project/:res', function (a, b) { server.serveResource(a, b)});
  62. app.post(apiprefix+'/user/:user/:project/:res/edit', function (a, b) {server.editRescource(a, b)});
  63. app.get(apiprefix+'/user/:user/:project/:res/rename', function (a, b) {server.renameResource(a, b)});
  64. app.post(apiprefix+'/user/:user/:project/:res/rename', function (a, b) {server.renameResource(a, b)});
  65. app.get(apiprefix+'/user/:user/:project/:res/remove', function (a, b) {server.removeResource(a, b)});
  66. app.post(apiprefix+'/user/:user/:project/:res/remove', function (a, b) {server.removeResource(a, b)});
  67. /*
  68. app.post(apiprefix+'/user/:user/:project/:resource', upload.single("image"), function(req, res) {
  69. res.send('<img src="'+apiprefix+'/upload/' + req.file.filename + '" />');
  70. });
  71. app.get(apiprefix+'/upload/:file', function(req, res) {
  72. res.sendFile(path.join(__dirname + '/tmp/'+req.params.file));
  73. });
  74. app.get(appprefix+'/:file', function (req, res) {
  75. res.sendFile(path.join(__dirname + '/../www/'+req.params.file));
  76. });
  77. app.get(apiprefix+'/user/:name', function (req, res) { server.serveUser(req, res);});*/
  78. var rest = app.listen(8081, function () {
  79. var host = rest.address().address
  80. var port = rest.address().port
  81. console.log("Example app listening at http://%s:%s", host, port)
  82. })