Commit b7b6cb1f authored by Bryan Tong's avatar Bryan Tong

Upgrade to latest application structure.

parent ad19f4a0
......@@ -33,6 +33,7 @@ if (process.env.command !== 'session' && cluster.isMaster()) {
// declare our application
const Application = require('kado/lib/Application')
const HyperText = require('kado/lib/HyperText')
const Main = require('./lib/Main')
const Parser = require('kado/lib/Parser')
const Session = require('kado/lib/Session')
const VersionSwitch = require('./lib/VersionSwitch')
......@@ -46,11 +47,11 @@ if (process.env.command !== 'session' && cluster.isMaster()) {
http.setHost(cfg.main.host)
app.http.addEngine('http', http.createServer(app.router))
// add a view engine
const viewFolder = fs.path.join(__dirname, 'main/views')
const viewFolder = fs.path.join(__dirname, 'view')
app.view.addEngine('mustache', new ViewMustache(viewFolder))
app.view.activateEngine('mustache')
// add a static server
const staticRoot = fs.path.join(__dirname, 'main/public')
const staticRoot = fs.path.join(__dirname, 'public')
app.use(HyperText.StaticServer.getMiddleware(staticRoot))
app.use((req) => {
req.locals._appTitle = cfg.appTitle
......@@ -68,8 +69,7 @@ if (process.env.command !== 'session' && cluster.isMaster()) {
// set the version to session if its in the query string and then redirect
app.use(VersionSwitch.getMiddleware(cfg.main.versionList))
// install routes
const route = require('./main/route')
route.register(app)
Main.register(app)
// initiate communications with the master and startup
Cluster.child(
http.getEngine(),
......
'use strict'
const Assert = require('kado/lib/Assert')
const fs = require('kado/lib/FileSystem')
const info = require('../views/info.json')
const info = require('../view/info.json')
const Validate = require('kado/lib/Validate')
class Route {
class Main {
static register (app) {
const route = new Main(
fs.path.join(__dirname, '../view'),
info.latestVersion
)
const redirectPath = (type) => {
return (req, res) => {
if (!type || !req.params.doc) return false
res.statusCode = 302
let uri = `/${type}/`
if (req.params.version) uri += `${req.params.version}/`
uri += `${req.params.doc}/`
res.redirect(uri)
}
}
app.get('/', route.index.bind(route))
app.get('/:type/:version/:doc/:link.md', route.linkTo.bind(route))
app.get('/:type/:doc/:link.md', route.linkTo.bind(route))
app.get('/article/:version/:doc/', route.article.bind(route))
app.get('/article/:link.md', route.linkTo.bind(route))
app.get('/article/:doc/', route.article.bind(route))
app.get('/doc/:link.md', route.linkTo.bind(route))
app.get('/doc/:version/:doc/', route.doc.bind(route))
app.get('/doc/:doc/', route.doc.bind(route))
app.get('/download/', route.download.bind(route))
app.get('/guide/:version/:link.md', route.linkTo.bind(route))
app.get('/guide/:link.md', route.linkTo.bind(route))
app.get('/guide/:doc/', route.guide.bind(route))
app.get('/info/:version/:link.md', route.linkTo.bind(route))
app.get('/info/:link.md', route.linkTo.bind(route))
app.get('/info/:doc/', route.info.bind(route))
app.get('/license/', route.license.bind(route))
app.get('/article/:doc', redirectPath('article'))
app.get('/doc/:version/:doc', redirectPath('doc'))
app.get('/guide/:version/:doc', redirectPath('guide'))
app.get('/doc/:doc', redirectPath('doc'))
app.get('/guide/:doc', redirectPath('guide'))
}
static titleCaseToUri (input) {
let rv = ''
for (let i = 0; i < input.length; i++) {
......@@ -21,6 +60,50 @@ class Route {
this.viewFolder = viewFolder
}
article (req, res) {
this.renderDoc(req.params.doc, res, req.params.version)
}
doc (req, res) {
this.renderDoc(req.params.doc, res, req.params.version)
}
download (req, res) { this.renderDoc('download', res) }
guide (req, res) { this.renderDoc(req.params.doc, res) }
info (req, res) { this.renderDoc(req.params.doc, res) }
index (req, res) { this.renderDoc('home', res) }
license (req, res) { res.redirect('/info/license/') }
linkTo (req, res) {
if (!req.params.link) {
res.statusCode = 500
return res.end(`Invalid link type: ${req.url}`)
}
const [majorVersion = '0'] = (
'' + (req.params.version || this.version)
).split('.')
const docInfo = info['v' + majorVersion]
let doc = null
const link = Route.titleCaseToUri(req.params.link)
if (docInfo.docMap[link]) doc = docInfo.docMap[link]
if (Validate.isType('string', doc)) doc = { url: doc }
if (!doc) {
res.statusCode = 500
return res.end(`No document found to link to: ${req.url} from ${link}`)
}
let url = '/'
if (doc.type) url += `${doc.type}/`
if (req.params.version) url += `${docInfo.docVersion}/`
url += `${doc.name}/`
res.statusCode = 302
res.setHeader('Location', url)
return res.end()
}
renderDoc (name = '', res = {}, version = info.latestVersion) {
const vString = `v${version.split('.')[0]}`
Assert.isType('string', name)
......@@ -50,112 +133,5 @@ class Route {
if (e instanceof Error) res.render('error', { message: e.message })
})
}
article () {
return (req, res) => {
this.renderDoc(req.params.doc, res, req.params.version)
}
}
doc () {
return (req, res) => {
this.renderDoc(req.params.doc, res, req.params.version)
}
}
download () {
return (req, res) => { this.renderDoc('download', res) }
}
guide () {
return (req, res) => {
this.renderDoc(req.params.doc, res)
}
}
info () {
return (req, res) => {
this.renderDoc(req.params.doc, res)
}
}
index () {
return (req, res) => { this.renderDoc('home', res) }
}
license () {
return (req, res) => {
res.statusCode = 301
res.setHeader('Location', '/info/license/')
return res.end()
}
}
linkDoc () {
return (req, res) => {
if (!req.params.link) {
res.statusCode = 500
return res.end(`Invalid link type: ${req.url}`)
}
const [majorVersion = '0'] = (
'' + (req.params.version || this.version)
).split('.')
const docInfo = info['v' + majorVersion]
let doc = null
const link = Route.titleCaseToUri(req.params.link)
if (docInfo.docMap[link]) doc = docInfo.docMap[link]
if (Validate.isType('string', doc)) doc = { url: doc }
if (!doc) {
res.statusCode = 500
return res.end(`No document found to link to: ${req.url} from ${link}`)
}
let url = '/'
if (doc.type) url += `${doc.type}/`
if (req.params.version) url += `${docInfo.docVersion}/`
url += `${doc.name}/`
res.statusCode = 302
res.setHeader('Location', url)
return res.end()
}
}
}
Route.register = (app) => {
const route = new Route(
fs.path.join(__dirname, '../views'),
info.latestVersion
)
const redirectPath = (type) => {
return (req, res) => {
if (!type || !req.params.doc) return false
res.statusCode = 302
let uri = `/${type}/`
if (req.params.version) uri += `${req.params.version}/`
uri += `${req.params.doc}/`
res.setHeader('Location', uri)
res.end()
}
}
app.get('/', route.index())
app.get('/:type/:version/:doc/:link.md', route.linkDoc())
app.get('/:type/:doc/:link.md', route.linkDoc())
app.get('/article/:version/:doc/', route.article())
app.get('/article/:link.md', route.linkDoc())
app.get('/article/:doc/', route.article())
app.get('/doc/:link.md', route.linkDoc())
app.get('/doc/:version/:doc/', route.doc())
app.get('/doc/:doc/', route.doc())
app.get('/download/', route.download())
app.get('/guide/:version/:link.md', route.linkDoc())
app.get('/guide/:link.md', route.linkDoc())
app.get('/guide/:doc/', route.guide())
app.get('/info/:version/:link.md', route.linkDoc())
app.get('/info/:link.md', route.linkDoc())
app.get('/info/:doc/', route.info())
app.get('/license/', route.license())
app.get('/article/:doc', redirectPath('article'))
app.get('/doc/:version/:doc', redirectPath('doc'))
app.get('/guide/:version/:doc', redirectPath('guide'))
app.get('/doc/:doc', redirectPath('doc'))
app.get('/guide/:doc', redirectPath('guide'))
}
module.exports = Route
module.exports = Main
'use strict'
const info = require('../main/views/info.json')
const info = require('../view/info.json')
class VersionSwitch {
static generateVersion (versionName) {
const [major = '0', minor = '0', security = '0'] = versionName.split('.')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment