From a1aa4aa824bfcab493e28e05b0e1f76491794e66 Mon Sep 17 00:00:00 2001 From: yanghong <10195501424@stu.ecnu.edu.cn> Date: Wed, 13 Jan 2021 10:47:26 +0800 Subject: [PATCH] final --- index.js | 11 ++++- package-lock.json | 43 +++++++++++++++++ routes/home.js | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++ views/home-layout.pug | 26 +++++------ views/home-signup.pug | 6 +-- views/home.pug | 3 +- views/layout.pug | 24 +++++----- 7 files changed, 207 insertions(+), 31 deletions(-) create mode 100644 routes/home.js diff --git a/index.js b/index.js index 7137601..695bbf4 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ const session = require('express-session'); // }); // Link Database -mongoose.connect('mongodb://10.23.153.10/', {useMongoClient:true}) +mongoose.connect('mongodb://root:123456@10.23.153.10', {useUnifiedTopology: true, useNewUrlParser: true}) let db = mongoose.connection; mongoose.plugin(schema => { schema.options.usePushEach = true }); @@ -31,6 +31,15 @@ db.on('error', function(err) { }); +// codeserver = cp.execFile('sudo', [ +// 'code-server', +// '--user-data-dir=/root/codedata', +// '--auth=none', +// '--bind-addr=0.0.0.0:' + 8080 +// ], function(error, stdout, stderr) { +// console.log(error + '\n' + stdout + '\n' + stderr); +// }); + // Init App const app = express(); diff --git a/package-lock.json b/package-lock.json index ca44d92..003ce03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,11 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", "integrity": "sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==" }, + "address": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/address/-/address-1.1.2.tgz", + "integrity": "sha512-aT6camzM4xEA54YVJYSqxz1kv4IHnQZRtThJJHhUMRExaU5spC7jX5ugSwTaTgJliIgs4VhZOk7htClvQ/LmRA==" + }, "array-flatten": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -135,6 +140,21 @@ "is-regex": "^1.0.3" } }, + "cify": { + "version": "2.1.12", + "resolved": "https://registry.npmjs.org/cify/-/cify-2.1.12.tgz", + "integrity": "sha512-VTTmFXfi/o5BuhnkzqicWm/9cdtOHldGIWiUi1RDh5cQN4FEqrPw5cgJ4mNeFR+cpDBq8Px5x2oU5r4z6JNdyg==", + "requires": { + "ntils": "^3.1.9" + }, + "dependencies": { + "ntils": { + "version": "3.1.11", + "resolved": "https://registry.npmjs.org/ntils/-/ntils-3.1.11.tgz", + "integrity": "sha512-nHughq/vzfQE/7igQUc2a4k7dfVEZDX12/Q0XyBNrkr2fqweLfo5c4M20V1fJBxbEusF+6bV87lGSI+XblsdEg==" + } + } + }, "constantinople": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/constantinople/-/constantinople-4.0.1.tgz", @@ -195,6 +215,15 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "detect-port": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/detect-port/-/detect-port-1.3.0.tgz", + "integrity": "sha512-E+B1gzkl2gqxt1IhUzwjrxBKRqx1UzC3WLONHinn8S3T6lwV/agVCyitiFOsGJ/eYuEUBvD71MZHy3Pv1G9doQ==", + "requires": { + "address": "^1.0.1", + "debug": "^2.6.0" + } + }, "doctypes": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/doctypes/-/doctypes-1.1.0.tgz", @@ -539,6 +568,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" }, + "ntils": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ntils/-/ntils-2.1.2.tgz", + "integrity": "sha1-d9PWWD6PycuzydjlsX+RpV2EKq8=" + }, "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -557,6 +591,15 @@ "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz", "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==" }, + "oneport": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/oneport/-/oneport-1.0.2.tgz", + "integrity": "sha1-MKQmgc7c2wAG+K9Yb85CohCfatM=", + "requires": { + "cify": "^2.1.1", + "ntils": "^2.0.3" + } + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", diff --git a/routes/home.js b/routes/home.js new file mode 100644 index 0000000..95e79dd --- /dev/null +++ b/routes/home.js @@ -0,0 +1,125 @@ +const express = require("express"); +const cp = require('child_process'); +const router= express.Router(); +const detect = require('detect-port'); +const oneport = require('oneport'); + + +let User = require("../DBcollection/user.js"); +const detectPort = require("detect-port"); + +let servers = new Set(); + +router.get('/', function(req, res) { + + if (req.session.user) { + // res.render('home', { + // user: req.session.user, + // title: 'HOME', + // }); + + oneport.acquire(function (err, port) { + if (err) return console.error(err); + console.log('port:', port); + + codeserver = cp.execFile('sudo', [ + 'code-server', + '--user-data-dir=/root/codedata/' + req.session.user.name, + '--auth=none', + '--bind-addr=0.0.0.0:' + port, + '--extensions-dir=/root/extensionsdata/' + req.session.user.name, + '--home=http://106.75.254.18/logoff' + ], function(error, stdout, stderr) { + console.log(error + '\n' + stdout + '\n' + stderr); + }); + servers.add({ + user: req.session.user, + server: codeserver + }); + res.redirect('http://106.75.254.18:' + port); + }); + } + else { + res.render('home-signin', { + user: req.session.user, + title: "user Sign in", + }); + } + +}); + +router.get('/logoff', function(req, res) { + servers.forEach(function(server) { + if (server.user == req.session.user) { + server.server.exit(0); + servers.delete(server); + } + }) + delete req.session.user; + res.redirect('/'); +}) + +router.post('/', function(req, res) { + let query = { + name: req.body.name, + password: req.body.password + }; + User.findOne(query, function(err, user) { + if (err) { + res.end("some thing wrong") + return console.log(err); + } + if (user != undefined) { + req.session.user = user; + // return res.render('home', { + // title: "HOME", + // user: req.session.user + // }); + return res.redirect('/'); + } + else { + return res.render('home-signin', { + title: "Sorry, no this user", + user: req.session.user + }) + } + }); +}); + +router.get('/signup', function(req, res) { + res.render('home-signup', { + user: req.session.user, + title: "user Sign up", + }); +}); + +router.post('/signup', function(req, res) { + User.find({name: req.body.name}, function(err, find) { + if (find.length) { + console.log(find); + res.render("home-signup", { + user: req.session.user, + title: "Same User Name - plz try again", + }); + } + else { + let user = new User(); + + console.log(user); + user.name = req.body.name; + user.password = req.body.password; + + user.save(function(err) { + if (err) { + res.end("some thing wrong"); + return console.log(err); + } + req.session.user = user; + res.redirect('/'); + }); + } + }); +}); + + +module.exports = router; \ No newline at end of file diff --git a/views/home-layout.pug b/views/home-layout.pug index a0fa471..766f68f 100644 --- a/views/home-layout.pug +++ b/views/home-layout.pug @@ -1,15 +1,15 @@ extends layout -block page-nav - li.nav-item - a.nav-link(href="/contest") - | Contest - li.nav-item - a.nav-link(href="/file") - |File - li.nav-item - a.nav-link(href="/picture") - |Picture - li.nav-item - a.nav-link(href="/danmu") - |Danmu +//- block page-nav +//- li.nav-item +//- a.nav-link(href="/contest") +//- | Contest +//- li.nav-item +//- a.nav-link(href="/file") +//- |File +//- li.nav-item +//- a.nav-link(href="/picture") +//- |Picture +//- li.nav-item +//- a.nav-link(href="/danmu") +//- |Danmu diff --git a/views/home-signup.pug b/views/home-signup.pug index 59c67a9..5df5607 100644 --- a/views/home-signup.pug +++ b/views/home-signup.pug @@ -8,9 +8,9 @@ block content #form-group label 密码: input.form-control(name='password', type='password') - #form-gruop - lable 是否教师身份注册: - input.form-control#form-control-checkbox(name='type', type='checkbox') + //- #form-gruop + //- lable 是否教师身份注册: + //- input.form-control#form-control-checkbox(name='type', type='checkbox') br input.btn.btn-primary(type='submit', value='Sign up') diff --git a/views/home.pug b/views/home.pug index 3828564..51e1ad3 100644 --- a/views/home.pug +++ b/views/home.pug @@ -1,5 +1,4 @@ extends home-layout block content - p Hello #{user.name} ! - p You are a #{user.type} in this website. \ No newline at end of file + p Hello #{user.name} ! \ No newline at end of file diff --git a/views/layout.pug b/views/layout.pug index 612e265..9ad237c 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -21,21 +21,21 @@ html nav#sidebarMenu.col-md-3.col-lg-2.d-md-block.bg-light.sidebar.collapse .sidebar-sticky.pt-3 - ul.nav.flex-column - li.nav-item - .nav-link.active(href="#") - | WEB-ASSIGNMENT - span.sr-only (current) - li.nav-item - a.nav-link(href="/") - h4 Home - hr - block page-nav + //- ul.nav.flex-column + //- li.nav-item + //- .nav-link.active(href="#") + //- | WEB-ASSIGNMENT + //- span.sr-only (current) + //- li.nav-item + //- a.nav-link(href="/") + //- h4 Home + //- hr + //- block page-nav hr if user != undefined - p welcome #{user.name}, you are a #{user.type} + p welcome #{user.name} else - p who you are ? + p who are you ?