Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
el1t committed Jun 12, 2017
1 parent e35ad99 commit 3c71624
Showing 6 changed files with 83 additions and 121 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# bydesign (Down to the Wire)
Blog Platform for Down to the Wire. Current version is hosted at [dttw.tech](http://dttw.tech), but will eventually be moved to a DttW themed url.
# dttw (Down to the Wire)
Blog platform for Down to the Wire. Current version is hosted at [dttw.tech](https://dttw.tech).
Plans have also been made to abstract the source and provide it as an OSS blogging platform based off node and express.

## Execution
The recommended stack for running bydesign is to use `forever` to daemonize the process, `mongod` in the background to serve requests, and `nginx` in the front to serve as a reverse proxy for it.
The recommended stack for running dttw is to use `forever` to daemonize the process, `mongod` in the background to serve requests, and `nginx` in the front to serve as a reverse proxy for it.
5 changes: 2 additions & 3 deletions client/hbs/head.hbs
Original file line number Diff line number Diff line change
@@ -68,10 +68,9 @@
</script>

<!-- FONTS -->
<link href='https://fonts.googleapis.com/css?family=Exo+2:300,400,600,400italic,600italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Exo+2:300,400,600,400italic,600italic|Inconsolata:400,700|Khand'
rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link href='https://fonts.googleapis.com/css?family=Khand' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Inconsolata:400,700' rel='stylesheet' type='text/css'>

<!-- CSS -->
<link rel="stylesheet" href="/css/style.css" />
57 changes: 29 additions & 28 deletions client/js/admin.js
Original file line number Diff line number Diff line change
@@ -27,11 +27,11 @@ const submit = function() {

$("#status").text("Uploading...")

const XHR = new XMLHttpRequest()
XHR.open("POST", "")
XHR.setRequestHeader("Content-Type", "application/json")
const xhr = new XMLHttpRequest()
xhr.open("POST", "")
xhr.setRequestHeader("Content-Type", "application/json")

XHR.onload = function() {
xhr.onload = function() {
setTimeout(function(that) {
return function() {
switch (that.status) {
@@ -55,7 +55,7 @@ const submit = function() {
}(this), 200)
}

XHR.send(JSON.stringify({
xhr.send(JSON.stringify({
content: content,
tags: tags,
title: title,
@@ -64,19 +64,19 @@ const submit = function() {
}

const visible = function() {
const XHR = new XMLHttpRequest()
XHR.open("POST", "/visible")
XHR.setRequestHeader("Content-Type", "application/json")
const xhr = new XMLHttpRequest()
xhr.open("POST", "/visible")
xhr.setRequestHeader("Content-Type", "application/json")

const el = $(this)
XHR.onload = function() {
const res = JSON.parse(XHR.response)
xhr.onload = function() {
const res = JSON.parse(xhr.response)
el.removeClass("hidden")
el.removeClass("visible")
el.addClass(res.state)
el.attr("visible", res.visible)
}
XHR.send(JSON.stringify({
xhr.send(JSON.stringify({
page: el.attr("post"),
state: !JSON.parse(el.attr("visible"))
}))
@@ -124,16 +124,16 @@ const attachHandles = function() {
const form = $("#file-upload")
form.off("change")
form.on("change", () => {
const XHR = new XMLHttpRequest()
XHR.open("POST", "/static/")
const xhr = new XMLHttpRequest()
xhr.open("POST", "/static/")
const fData = new FormData(form[0].form)
XHR.onload = function() {
const out = JSON.parse(XHR.response)
xhr.onload = function() {
const out = JSON.parse(xhr.response)
$(".upload-name")
.removeClass("disabled")
.text(out.path)
}
XHR.send(fData)
xhr.send(fData)
})
form.click()
})
@@ -169,13 +169,14 @@ const attachHandles = function() {
$(".copy-path").click(function(e) {
e.stopPropagation()
const path = $(this).attr("path")
const cb = $("#copy-buffer")
cb.text(path)
const range = document.createRange()
range.selectNode(cb[0])
window.getSelection().empty ? window.getSelection().empty() : undefined
window.getSelection().removeAllRanges ? window.getSelection().removeAllRanges() : undefined
window.getSelection().addRange(range)
const $copyBuffer = $("#copy-buffer")
$copyBuffer.text(path)
let range = document.createRange()
range.selectNode($copyBuffer[0])
let selection = window.getSelection()
selection.empty && window.getSelection().empty()
selection.removeAllRanges && window.getSelection().removeAllRanges()
selection.addRange(range)
document.execCommand("copy")
$(this).addClass("copied")
$(this).outerWidth()
@@ -193,15 +194,15 @@ const attachHandles = function() {
}
const path = obj.attr("path")

const XHR = new XMLHttpRequest()
XHR.open("DELETE", path)
XHR.onload = function() {
if (XHR.status === 200 && XHR.response && JSON.parse(XHR.response).success) {
const xhr = new XMLHttpRequest()
xhr.open("DELETE", path)
xhr.onload = function() {
if (xhr.status === 200 && xhr.response && JSON.parse(xhr.response).success) {
obj.parent().addClass("deleting")
setTimeout(() => obj.parent().remove(), 500)
}
}
XHR.send()
xhr.send()
})
}

102 changes: 39 additions & 63 deletions server/api.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,39 @@
"use strict"

const denodeify = require("denodeify")

function fail(res, msg) {
res.status(503)
res.send(msg)
}

function prep(res) {
return function(data) {
res.type("application/json")
res.send(data)
}
}

function denodeify(fn, args, alt, th) {
return new Promise(function(resolve, reject) {
args[args.length] = (function(err, data) {
if (err) {
reject(err || "No Data")
} else {
resolve(alt ? alt(data) : data)
}
})
fn.apply(th, args)
})
const prep = (res) => (data) => {
res.type("application/json")
res.send(data)
}

function apiHandler(db) {
this.db = db
}
class ApiHandler {
constructor(db) {
this.db = db
}

apiHandler.prototype = {
handle: function(req, res, next) {
handle(req, res, next) {
const match = req.originalUrl.match(/\/api\/([a-z0-9]+)\/([a-z0-9]+)/)
if (match) {
let out
if (match[1] === "get") {
out = this.meta(match[2]).then(prep(res))
} else if (match[1] === "recent") {
out = this.recent(match[2]).then(prep(res))
} else if (match[1] === "author") {
out = this.author(match[2]).then(prep(res))
} else if (match[1] === "post") {
out = this.post(match[2]).then(prep(res))
} else {
fail(res, "Unknown Option")
return
}
out.catch(function(err) {
fail(res, err)
})
} else {
if (!match) {
next()
return
}
let route = this.routes[match[1]]
if (route === undefined) {
fail(res, "Unknown Option")
return
}
},
meta: function(val) {
route(match[2])
.then(prep(res))
.catch((err) => fail(res, err))
}

meta(val) {
if (val === "authors") {
return denodeify(this.db.authors.find, [{}, {
"id": 1,
@@ -63,31 +43,27 @@ apiHandler.prototype = {
} else {
return Promise.reject("Invalid Option")
}
},
recent: function(num) {
}
recent(num) {
num = parseInt(num)
if (isNaN(num)) return Promise.reject("Not an Integer")
const cursor = this.db.posts.find({}, { "_id": 0 }).sort({ "timestamp": -1 }).limit(num)
return denodeify(cursor.map, [function(doc) {
return doc.guid
}], JSON.stringify, cursor)
},
author: function(author) {
return denodeify(cursor.map, [(doc) => doc.guid], JSON.stringify, cursor)
}
author(author) {
const cursor = this.db.posts.find({ author: author }, { "_id": 0 }).sort({ "timestamp": -1 })
return denodeify(cursor.map, [function(doc) {
return doc.guid
}], JSON.stringify, cursor)
},
post: function(id) {
return denodeify(cursor.map, [(doc) => doc.guid], JSON.stringify, cursor)
}
post(id) {
return denodeify(this.db.posts.find, [{ guid: id }, { "_id": 0 }], JSON.stringify, this.db.posts)
}
}

module.exports = function(db) {
const handle = new apiHandler(db)
return (function(handle) {
return function() {
handle.handle.apply(handle, arguments)
}
})(handle)
}
ApiHandler.routes = Object.freeze({
get: ApiHandler.meta,
recent: ApiHandler.recent,
author: ApiHandler.author,
post: ApiHandler.post
})

module.exports = ApiHandler
26 changes: 5 additions & 21 deletions server/insertPost.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
#!/usr/bin/env node
"use strict"

const fs = require("fs")
const path = require("path")
const config = require("../config")
const fs = require("fs")
const denodeify = require("denodeify")
const path = require("path")
const config = require("../config")
// const shortid = require("shortid")
// const utils = require("./utils")
const logger = require("./logger")

let prompt
let prompt = undefined
const globals = {}
globals.coll = undefined
globals.path = undefined

function denodeify(fn, args, alt, th) {
return new Promise(function(resolve, reject) {
args[args.length] = (function(err, data) {
if (err) {
reject(err || "No Data")
} else {
resolve(alt ? alt(data) : data)
}
})
try {
fn.apply(th, args)
} catch (e) {
reject(e)
}
})
}

function fetchData() {
return denodeify(prompt.get, [["author", "title", "tags", "content"]])
}
8 changes: 5 additions & 3 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ module.exports = function(__dirname) {

// const insert = require("./insertPost")(db, config.paths.client)
const Renderer = require("./renderer")
const api = require("./api")(db)
const ApiHandler = require("./api")

const express = require("express")
const app = express()
@@ -42,6 +42,7 @@ module.exports = function(__dirname) {
const secret = process.env.PASSPORT_SECRET ? process.env.PASSPORT_SECRET : "testsecret"

const renderer = new Renderer(__dirname, db, handlebars)
const api = new ApiHandler(db)

/**
* Middleware Initialization
@@ -57,8 +58,6 @@ module.exports = function(__dirname) {
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(renderer.handle.bind(renderer))
app.use(api)

/**
* Annotations
@@ -122,6 +121,9 @@ module.exports = function(__dirname) {
app.use(express.static("build"))
app.use("/upload", express.static(config.paths.upload))

app.use(renderer.handle.bind(renderer))
app.use(api.handle.bind(api))

app.post("/visible", requireAdmin((req, res) => {
handleVisibility(req.body.state, req.body.page).then(() => {
res.status(200)

0 comments on commit 3c71624

Please sign in to comment.