[www] + Config model; config updated; using remote config; server refactoring
This commit is contained in:
1 parent
18a0b36a09
commit
5ad321ec0e
7 files changed
+85
-69
No files matched your search
+15
-6
@@ -5,17 +5,18 @@ export const configFile = process.env['PRIVATE'] || 'private.json'
|
|||||||
const privateData = JSON.parse(fs.readFileSync(configFile, 'utf-8'))
|
const privateData = JSON.parse(fs.readFileSync(configFile, 'utf-8'))
|
||||||
|
|
||||||
const indexFile = process.env['INDEX'] || 'templates/main.html'
|
const indexFile = process.env['INDEX'] || 'templates/main.html'
|
||||||
const port = process.env['PORT'] || 1800
|
export const port = process.env['PORT'] || 1800
|
||||||
|
|
||||||
|
const value = (name, def) => name in privateData
|
||||||
|
? privateData[name]
|
||||||
|
: def
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
port: port,
|
|
||||||
pub: './',
|
pub: './',
|
||||||
|
|
||||||
db: {
|
|
||||||
uri: 'mongodb://localhost:27017/hudozka'
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const dbUri = value('dbUri', 'mongodb://localhost:27017/hudozka')
|
||||||
|
|
||||||
export default Object.keys(privateData)
|
export default Object.keys(privateData)
|
||||||
.reduce((config, key) => {
|
.reduce((config, key) => {
|
||||||
config[key] = privateData[key]
|
config[key] = privateData[key]
|
||||||
@@ -46,3 +47,11 @@ export const instagramToken = privateData['instagramToken']
|
|||||||
|
|
||||||
export const viewMain = path.join(__dirname, 'views/main.hbs')
|
export const viewMain = path.join(__dirname, 'views/main.hbs')
|
||||||
export const view404 = path.join(__dirname, 'views/404.hbs')
|
export const view404 = path.join(__dirname, 'views/404.hbs')
|
||||||
|
|
||||||
|
export const sessionConfig = {
|
||||||
|
key: 'sid', /** (string) cookie key */
|
||||||
|
maxAge: 86400000, /** (number) maxAge in ms */
|
||||||
|
overwrite: false, /** (boolean) can overwrite or not */
|
||||||
|
httpOnly: false, /** (boolean) httpOnly or not */
|
||||||
|
signed: true, /** (boolean) signed or not */
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
const Data = require('./Data')
|
||||||
|
const {findOne} = require('../lib/store')
|
||||||
|
|
||||||
|
class Config {
|
||||||
|
static get store() {
|
||||||
|
return Data.getStore(Config)
|
||||||
|
}
|
||||||
|
|
||||||
|
static async findById(id) {
|
||||||
|
const data = await findOne(Config.store, {id})
|
||||||
|
if (!data) return null
|
||||||
|
|
||||||
|
return new Config(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor({hash, file, ...data}) {
|
||||||
|
this.hash = hash
|
||||||
|
this.file = file
|
||||||
|
this.server = data.server
|
||||||
|
this.articlesPerPage = data.articlesPerPage || 5
|
||||||
|
this.collectiveOrder = data.collectiveOrder || []
|
||||||
|
|
||||||
|
this.redirect = new Map(Object
|
||||||
|
.entries(data.redirect)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Config
|
||||||
@@ -3,6 +3,7 @@ const Album = require('./Album')
|
|||||||
const Teacher = require('./Teacher')
|
const Teacher = require('./Teacher')
|
||||||
const Document = require('./Document')
|
const Document = require('./Document')
|
||||||
const Image = require('./Image')
|
const Image = require('./Image')
|
||||||
|
const Settings = require('./Config')
|
||||||
|
|
||||||
const {MongoClient, ObjectId} = require('mongodb')
|
const {MongoClient, ObjectId} = require('mongodb')
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ function init(db) {
|
|||||||
Data.setStore(Image, db.collection('images'))
|
Data.setStore(Image, db.collection('images'))
|
||||||
Data.setStore(Teacher, db.collection('collective'))
|
Data.setStore(Teacher, db.collection('collective'))
|
||||||
Data.setStore(Document, db.collection('documents'))
|
Data.setStore(Document, db.collection('documents'))
|
||||||
|
Data.setStore(Settings, db.collection('settings'))
|
||||||
|
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
+11
-16
@@ -1,36 +1,31 @@
|
|||||||
import 'babel-polyfill'
|
import 'babel-polyfill'
|
||||||
|
|
||||||
import config, {name, privateFile} from './config'
|
import {dbUri, port, name, configFile} from './config'
|
||||||
import {connect, collection} from './core/db'
|
import {connect} from './core/db'
|
||||||
import server from './server'
|
import server from './server'
|
||||||
|
|
||||||
import DataManager from 'hudozka-data'
|
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
async function loop() {
|
async function loop() {
|
||||||
const dbUri = config.db.uri
|
|
||||||
await connect(dbUri)
|
await connect(dbUri)
|
||||||
|
|
||||||
|
const Config = require('./core/Config')
|
||||||
|
const config = await Config.findById('settings')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let data = new DataManager({
|
let app = server(config)
|
||||||
timeline: collection('timeline'),
|
|
||||||
schedules: collection('schedules'),
|
|
||||||
documents: collection('documents'),
|
|
||||||
})
|
|
||||||
|
|
||||||
let app = server(data)
|
app.listen(port)
|
||||||
|
|
||||||
app.listen(config.port)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e.stack)
|
console.error(e.stack)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Using node ${process.version}`)
|
console.log(`Using node ${process.version}`)
|
||||||
console.log(`Config file: ${privateFile}`)
|
|
||||||
console.log(`App ${name} started`)
|
console.log(`App ${name} started`)
|
||||||
console.log(`Listening ${config.port}`)
|
console.log(`Server: ${config.server}`)
|
||||||
console.log(`DB Address ${dbUri}`)
|
console.log(`Config file: ${configFile}`)
|
||||||
|
console.log(`DB Address: ${dbUri}`)
|
||||||
|
console.log(`Listening ${port}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return loop()
|
return loop()
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param redirectMap
|
||||||
|
* [{String url, String redirect}, ...]
|
||||||
|
* @returns {*}
|
||||||
|
*/
|
||||||
|
function redirect(redirectMap) {
|
||||||
|
return async (ctx, next) => {
|
||||||
|
const path = ctx.request.path
|
||||||
|
|
||||||
|
if (redirectMap.has(path)) {
|
||||||
|
const url = redirectMap.get(path)
|
||||||
|
ctx.redirect(url)
|
||||||
|
} else {
|
||||||
|
await next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = redirect
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
*
|
|
||||||
* @param redirectionTable
|
|
||||||
* [{String url, String redirect}, ...]
|
|
||||||
* @returns {*}
|
|
||||||
*/
|
|
||||||
export function redirect(redirectionTable) {
|
|
||||||
return function *(next) {
|
|
||||||
let path = this.request.path;
|
|
||||||
|
|
||||||
let record = redirectionTable.find(i => i.url === path);
|
|
||||||
if(record){
|
|
||||||
this.redirect(record.redirect);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
yield next;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
+8
-28
@@ -7,20 +7,17 @@ import convert from 'koa-convert'
|
|||||||
import conditional from 'koa-conditional-get'
|
import conditional from 'koa-conditional-get'
|
||||||
import etag from 'koa-etag'
|
import etag from 'koa-etag'
|
||||||
import helmet from 'koa-helmet'
|
import helmet from 'koa-helmet'
|
||||||
import mount from 'koa-mount'
|
|
||||||
import cookie from 'koa-cookie'
|
import cookie from 'koa-cookie'
|
||||||
import session from 'koa-session'
|
import session from 'koa-session'
|
||||||
import bodyParser from 'koa-bodyparser'
|
import bodyParser from 'koa-bodyparser'
|
||||||
|
|
||||||
import {redirectionTable} from './config'
|
|
||||||
import {queryObject} from './routes'
|
import {queryObject} from './routes'
|
||||||
import {redirect} from './routes/redirect'
|
|
||||||
import {services as serviceKeys, authChecker} from './core/service'
|
|
||||||
import handlebars from 'handlebars'
|
import handlebars from 'handlebars'
|
||||||
|
|
||||||
const config = require('./config')
|
const {sessionConfig, view404} = require('./config')
|
||||||
const error = require('./middlewares/error')
|
const error = require('./middlewares/error')
|
||||||
const redirectFragment = require('./middlewares/redirectFragment')
|
const redirectFragment = require('./middlewares/redirectFragment')
|
||||||
|
const redirect = require('./middlewares/redirect')
|
||||||
const home = require('./routes/home')
|
const home = require('./routes/home')
|
||||||
const article = require('./routes/articles')
|
const article = require('./routes/articles')
|
||||||
const albums = require('./routes/albums')
|
const albums = require('./routes/albums')
|
||||||
@@ -36,24 +33,7 @@ const dirPublic = path.join(__dirname, '../public')
|
|||||||
|
|
||||||
handlebars.registerHelper('raw-helper', options => options.fn())
|
handlebars.registerHelper('raw-helper', options => options.fn())
|
||||||
|
|
||||||
const collectiveOrder = [
|
export default function (config) {
|
||||||
'mg-timasheva',
|
|
||||||
'va-sarzhin',
|
|
||||||
'od-gogoleva',
|
|
||||||
'my-valkova',
|
|
||||||
'vv-voronova',
|
|
||||||
'nv-andreeva',
|
|
||||||
]
|
|
||||||
|
|
||||||
const sessionConfig = {
|
|
||||||
key: 'sid', /** (string) cookie key */
|
|
||||||
maxAge: 86400000, /** (number) maxAge in ms */
|
|
||||||
overwrite: false, /** (boolean) can overwrite or not */
|
|
||||||
httpOnly: false, /** (boolean) httpOnly or not */
|
|
||||||
signed: true, /** (boolean) signed or not */
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function (store) {
|
|
||||||
const $ = convert
|
const $ = convert
|
||||||
|
|
||||||
const app = new Koa()
|
const app = new Koa()
|
||||||
@@ -71,7 +51,7 @@ export default function (store) {
|
|||||||
app.use($(conditional()))
|
app.use($(conditional()))
|
||||||
app.use($(etag()))
|
app.use($(etag()))
|
||||||
app.use(serve(dirPublic))
|
app.use(serve(dirPublic))
|
||||||
app.use($(redirect(redirectionTable)))
|
app.use(redirect(config.redirect))
|
||||||
app.use($(helmet()))
|
app.use($(helmet()))
|
||||||
app.use(queryObject())
|
app.use(queryObject())
|
||||||
|
|
||||||
@@ -83,16 +63,16 @@ export default function (store) {
|
|||||||
await next()
|
await next()
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use(error.notFound(config.view404))
|
app.use(error.notFound(view404))
|
||||||
app.use(sitemap())
|
app.use(sitemap())
|
||||||
app.use(redirectFragment())
|
app.use(redirectFragment())
|
||||||
|
|
||||||
app.use(home.getHome(5))
|
app.use(home.getHome(config.articlesPerPage))
|
||||||
app.use(gallery.getGallery())
|
app.use(gallery.getGallery())
|
||||||
app.use(article.getArticles(5))
|
app.use(article.getArticles(config.articlesPerPage))
|
||||||
app.use(article.getArticle())
|
app.use(article.getArticle())
|
||||||
app.use(albums.getAlbum())
|
app.use(albums.getAlbum())
|
||||||
app.use(teachers.getCollective(collectiveOrder))
|
app.use(teachers.getCollective(config.collectiveOrder))
|
||||||
app.use(teachers.getTeacher())
|
app.use(teachers.getTeacher())
|
||||||
app.use(documents.getDocuments())
|
app.use(documents.getDocuments())
|
||||||
app.use(document.getDocument())
|
app.use(document.getDocument())
|
||||||
|
|||||||
Reference in new issue
Block a user