could listen to unix socket, better conf

This commit is contained in:
lesion
2019-06-10 00:40:37 +02:00
parent d1a56e5135
commit b01f4ef04d
15 changed files with 107 additions and 76 deletions

View File

@@ -42,7 +42,7 @@ const exportController = {
feed(res, events) {
res.type('application/rss+xml; charset=UTF-8')
res.render('feed/rss.pug', { events, config: process.env.config, moment })
res.render('feed/rss.pug', { events, config: process.env, moment })
},
ics(res, events) {

View File

@@ -1,8 +1,6 @@
const Mastodon = require('mastodon-api')
const { setting: Setting } = require('../models')
const baseurl = process.env.baseurl
const settingsController = {
async setAdminSetting(key, value) {
@@ -20,7 +18,7 @@ const settingsController = {
async getAuthURL(req, res) {
const instance = req.body.instance
const callback = `${baseurl}/api/settings/oauth`
const callback = `${process.env.baseurl}/api/settings/oauth`
const { client_id, client_secret } = await Mastodon.createOAuthApp(`https://${instance}/api/v1/apps`,
'gancio', 'read write', callback)
const url = await Mastodon.getAuthorizationUrl(client_id, client_secret,
@@ -33,7 +31,7 @@ const settingsController = {
async code(req, res) {
const code = req.query.code
let client_id, client_secret, instance
const callback = `${baseurl}/api/settings/oauth`
const callback = `${process.env.baseurl}/api/settings/oauth`
const settings = await settingsController.settings()

View File

@@ -30,7 +30,7 @@ const userController = {
email: user.email,
scope: [user.is_admin ? 'admin' : 'user']
},
config.SECRET_CONF.secret
config.secret
)
res.json({ token: accessToken })
@@ -164,7 +164,7 @@ const userController = {
if (!user) return res.sendStatus(200)
user.recover_code = crypto.randomBytes(16).toString('hex')
mail.send(user.email, 'recover', { user, config: config.SHARED_CONF })
mail.send(user.email, 'recover', { user, config })
await user.save()
res.sendStatus(200)
@@ -208,7 +208,7 @@ const userController = {
const user = await User.findByPk(req.body.id)
if (user) {
if (!user.is_active && req.body.is_active) {
await mail.send(user.email, 'confirm', { user, config: config.SHARED_CONF })
await mail.send(user.email, 'confirm', { user, config })
}
await user.update(req.body)
res.json(user)
@@ -229,7 +229,7 @@ const userController = {
const user = await User.create(req.body)
try {
mail.send([user.email, config.SECRET_CONF.admin], 'register', { user, config: config.SHARED_CONF })
mail.send([user.email, config.admin], 'register', { user, config })
} catch (e) {
return res.status(400).json(e)
}
@@ -238,7 +238,7 @@ const userController = {
email: user.email,
scope: [user.is_admin ? 'admin' : 'user']
}
const token = jwt.sign(payload, config.SECRET_CONF.secret)
const token = jwt.sign(payload, config.secret)
res.json({ token, user })
} catch (e) {
res.status(404).json(e)

View File

@@ -22,7 +22,7 @@ api.use(bodyParser.urlencoded({ extended: false }))
api.use(bodyParser.json())
const jwt = expressJwt({
secret: config.SECRET_CONF.secret,
secret: config.secret,
credentialsRequired: false
})

View File

@@ -3,7 +3,7 @@ const path = require('path')
const moment = require('moment')
const config = require('../config')
moment.locale(config.SHARED_CONF.locale)
moment.locale(config.locale)
const mail = {
send(addresses, template, locals) {
const email = new Email({
@@ -17,25 +17,25 @@ const mail = {
}
},
message: {
from: `${config.SHARED_CONF.title} <${config.SECRET_CONF.smtp.auth.user}>`
from: `${config.title} <${config.smtp.auth.user}>`
},
send: true,
i18n: {
directory: path.join(__dirname, '..', '..', 'locales', 'email'),
defaultLocale: config.SHARED_CONF.locale
defaultLocale: config.locale
},
transport: config.SECRET_CONF.smtp
transport: config.smtp
})
const msg = {
template,
message: {
to: addresses,
bcc: config.SECRET_CONF.admin
bcc: config.admin
},
locals: {
...locals,
locale: config.SHARED_CONF.locale,
config: config.SHARED_CONF,
locale: config.locale,
config: { title: config.title, baseurl: config.baseurl, description: config.description },
datetime: datetime => moment(datetime).format('ddd, D MMMM HH:mm')
}
}

View File

@@ -4,10 +4,10 @@ const path = require('path')
const Sequelize = require('sequelize')
const config_path = path.resolve(argv.config || './config.js')
const basename = path.basename(__filename)
const config = require(config_path).SECRET_CONF.db
const config = require(config_path)
const db = {}
const sequelize = new Sequelize(config)
const sequelize = new Sequelize(config.db)
fs
.readdirSync(__dirname)

View File

@@ -1,23 +0,0 @@
doctype xml
rss(version='2.0')
channel
title #{config.title}
link #{config.baseurl}
description #{config.description}
language #{config.locale}
//- if events.length
lastBuildDate= new Date(posts[0].publishedAt).toUTCString()
each event in events
item
title= event.title
link #{config.baseurl}/event/#{event.id}
description
| <![CDATA[
| <h4>#{event.title}</h4>
| <strong>#{event.place.name} - #{event.place.address}</strong>
| #{moment(event.start_datetime).format("dddd, D MMMM HH:mm")}<br/>
| <img src="#{config.apiurl}/../uploads/#{event.image_path}"/>
| <pre>!{event.description}</pre>
| ]]>
pubDate= new Date(event.createdAt).toUTCString()
guid(isPermaLink='false') #{config.baseurl}/event/#{event.id}

View File

@@ -3,5 +3,3 @@ const path = require('path')
const config_path = path.resolve(argv.config || './config.js')
module.exports = require(config_path)

View File

@@ -10,8 +10,8 @@ if (!fs.existsSync(config_path)) {
process.exit(1)
}
const { SECRET_CONF, SHARED_CONF } = require(config_path)
if (!SECRET_CONF.secret) {
const config = require(config_path)
if (!config.secret) {
console.error(`Please specify a random 'secret' in '${config_path}'!`)
process.exit(1)
}
@@ -19,9 +19,9 @@ if (!SECRET_CONF.secret) {
const Sequelize = require('sequelize')
let db
try {
db = new Sequelize(SECRET_CONF.db)
db = new Sequelize(config.db)
} catch (e) {
console.error(`DB Error: check '${SHARED_CONF.env}' configuration.\n (sequelize error -> ${e})`)
console.error(`DB Error: check '${config.env}' configuration.\n (sequelize error -> ${e})`)
process.exit(1)
}
@@ -29,13 +29,13 @@ try {
module.exports = db.authenticate()
.then(() => {
require('./api/models')
if (SHARED_CONF.env === 'development') {
if (config.env === 'development') {
console.error('DB Force sync')
return db.sync({ force: true })
}
})
.catch(e => {
console.error(e)
console.error(`DB Error: check '${SHARED_CONF.env}' configuration\n (sequelize error -> ${e})`)
console.error(`DB Error: check '${config.env}' configuration\n (sequelize error -> ${e})`)
process.exit(1)
})

View File

@@ -6,17 +6,18 @@ const morgan = require('morgan')
const { Nuxt, Builder } = require('nuxt')
const firstRun = require('./firstrun')
// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
const nuxt_config = require('../nuxt.config.js')
const config = require('./config')
const app = express()
async function start() {
// Init Nuxt.js
const nuxt = new Nuxt(config)
const nuxt = new Nuxt(nuxt_config)
const { host, port } = nuxt.options.server
// const { host, port } = nuxt.options.server
// Build only in dev mode
if (config.dev) {
if (nuxt_config.dev) {
const builder = new Builder(nuxt)
await builder.build()
} else {
@@ -29,10 +30,26 @@ async function start() {
app.use(nuxt.render)
// Listen the server
app.listen(port, host)
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
const server = app.listen(config.server)
// close connections/port/unix socket
function shutdown() {
consola.info(`Closing connections..`)
server.close()
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)
server.on('error', e => {
consola.error(e)
})
server.on('listening', () => {
const address = server.address()
consola.ready({
message: `Server listening on ${(typeof address) === 'object' ? `${address.address}:${address.port}` : address}`,
badge: true
})
})
}

View File

@@ -1,7 +1,7 @@
// const mail = require('./mail')
const mail = require('./mail')
const bot = require('./api/controller/bot')
const settingsController = require('./api/controller/settings')
// const config = require('./config.js')
const config = require('./config.js')
const { Event, Notification, EventNotification,
User, Place, Tag } = require('./api/models')
@@ -12,10 +12,10 @@ async function sendNotification(notification, event, eventNotification) {
switch (notification.type) {
// case 'mail':
// return mail.send(notification.email, 'event', { event, config, notification })
// case 'admin_email':
case 'admin_email':
// const admins = await User.findAll({ where: { is_admin: true } })
// const admin_emails = admins.map(admin => admin.email)
// return mail.send(admin_emails, 'event', { event, to_confirm: true, notification })
return mail.send(admin_emails, 'event', { event, to_confirm: true, notification })
case 'mastodon':
// instance publish
if (settings.mastodon_auth.instance && settings.mastodon_auth.access_token) {
@@ -58,8 +58,4 @@ function startLoop(seconds) {
interval = setInterval(notify, seconds * 1000)
}
function stopLoop() {
stopInterval(interval)
}
module.exports = { startLoop, stopLoop }
startLoop(26000)