2023-01-04 11:17:41 +11:00
|
|
|
const express = require('express')
|
|
|
|
|
|
|
|
const { getLatestPost, getNow, publishNewPost, requireLoggedIn, resetPassword, saveFile, updatePost, verifyUser, getSavedFile } = require('./utilities')
|
|
|
|
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const Database = require('better-sqlite3');
|
|
|
|
const session = require('express-session')
|
|
|
|
const SqliteStore = require("better-sqlite3-session-store")(session)
|
2023-01-11 13:30:52 +11:00
|
|
|
const sprightly = require('sprightly');
|
2023-01-04 11:17:41 +11:00
|
|
|
|
|
|
|
// configure Express
|
|
|
|
const app = express()
|
|
|
|
const PORT = process.env.SOYUZ_PORT
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
|
|
|
app.use(express.static('static'))
|
|
|
|
|
|
|
|
// configure session store
|
|
|
|
db = new Database('soyuz.db', {});
|
|
|
|
app.use(
|
|
|
|
session({
|
|
|
|
store: new SqliteStore({
|
|
|
|
client: db,
|
|
|
|
expired: {
|
|
|
|
clear: true,
|
|
|
|
intervalMs: 900000 //ms = 15min
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
saveUninitialized: false,
|
|
|
|
secret: process.env.SOYUZ_SESSION_SECRET,
|
|
|
|
resave: false,
|
|
|
|
cookie: {
|
|
|
|
sameSite: 'strict',
|
|
|
|
maxAge: 1.21e+9 // 2 weeks
|
|
|
|
},
|
|
|
|
name: 'soyuz-web'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// configure template engine
|
|
|
|
app.engine('spy', sprightly);
|
|
|
|
app.set('views', './templates');
|
|
|
|
app.set('view engine', 'spy');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ROUTES
|
|
|
|
*/
|
|
|
|
|
|
|
|
// GET
|
|
|
|
|
|
|
|
app.get('/', requireLoggedIn, (req, res) => {
|
2023-03-12 15:02:39 +11:00
|
|
|
res.render('index.spy', {title: 'Soyuz home', writeNew: getSavedFile(req.session.user.username)? 'Return to draft' : 'New'})
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/login', (req, res) => {
|
|
|
|
if (req.session.user) {
|
|
|
|
res.redirect('/')
|
|
|
|
} else {
|
2023-01-30 16:56:17 +11:00
|
|
|
res.render('login.spy', {title: 'Log In'})
|
2023-01-04 11:17:41 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-12 15:02:39 +11:00
|
|
|
app.get('/new', requireLoggedIn, (req, res) => {
|
|
|
|
let message = getSavedFile(req.session.user.username) || "# Title of my note"
|
|
|
|
let data = {
|
|
|
|
title: 'New post',
|
|
|
|
disabled: '',
|
|
|
|
message: message
|
|
|
|
}
|
|
|
|
let today = getNow().toISOString().slice(0,10)
|
|
|
|
// check whether user has already posted today
|
|
|
|
return getLatestPost(req.session.user.directory, true, (dateString)=> {
|
|
|
|
if (today === dateString) {
|
|
|
|
data.disabled = 'disabled'
|
|
|
|
data.message = `Relax, ${req.session.user.username}, you have already posted today.`
|
|
|
|
}
|
|
|
|
res.render('new.spy', data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-04 11:17:41 +11:00
|
|
|
app.get('/edit', requireLoggedIn, (req, res) => {
|
2023-03-12 15:02:39 +11:00
|
|
|
return getLatestPost(req.session.user.directory, true, (dateString) => {
|
|
|
|
if (dateString) {
|
|
|
|
return getLatestPost( req.session.user.directory, false, (message, path) => {
|
|
|
|
res.render('edit.spy', {message: message, path: path, title: 'Edit'})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.redirect('/new')
|
|
|
|
}
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/settings', requireLoggedIn, (req, res) => {
|
2023-01-30 16:56:17 +11:00
|
|
|
res.render('settings.spy', {title: 'Settings'})
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/try-again', requireLoggedIn, (req, res, next) => {
|
2023-01-30 16:56:17 +11:00
|
|
|
res.render('try-again.spy', {title: 'Log In'})
|
2023-01-11 14:04:31 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/help', requireLoggedIn, (req, res, next) => {
|
2023-01-30 16:56:17 +11:00
|
|
|
res.render('help.spy', {title: 'Help'})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.get('/published', requireLoggedIn, (req, res, next) => {
|
|
|
|
res.render('published.spy', {title: 'You published a note!'})
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
// POST
|
|
|
|
|
|
|
|
app.post('/login', verifyUser,
|
|
|
|
function(req, res){
|
|
|
|
if (req.session.user) {
|
|
|
|
res.redirect('/')
|
|
|
|
} else {
|
|
|
|
res.redirect('/try-again')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/logout', function(req, res, next){
|
|
|
|
req.session.destroy( (err) => {
|
|
|
|
if (err) {console.error(err)}
|
|
|
|
res.redirect('/login')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/publish', requireLoggedIn, (req, res) => {
|
|
|
|
publishNewPost(req, () => {
|
2023-01-30 16:56:17 +11:00
|
|
|
res.redirect('/published')
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/save', requireLoggedIn, (req, res) => {
|
2023-01-11 19:05:13 +11:00
|
|
|
saveFile(req.session.user.username, req.body.textarea, () => {
|
2023-03-12 15:02:39 +11:00
|
|
|
res.redirect('/new')
|
2023-01-04 11:17:41 +11:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/update', requireLoggedIn, (req, res) => {
|
|
|
|
updatePost(req, () => {
|
|
|
|
res.redirect('/')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.post('/reset-password', requireLoggedIn, (req, res) => {
|
|
|
|
resetPassword(req.session.user.username, req.body.password, password => {
|
|
|
|
return req.session.destroy( (err) => {
|
|
|
|
if (err) {console.error(err)}
|
|
|
|
res.redirect('/login')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Let's go!
|
|
|
|
*/
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
console.log(`Soyuz Web listening on port ${PORT}`)
|
|
|
|
})
|