Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Render homepage as well

  • Loading branch information...
valueof committed Oct 5, 2017
1 parent bf6b791 commit 535ee51682c94e7b75a0e73f44b8ac8b2b3b59f6
Showing with 36 additions and 26 deletions.
  1. +0 −19 bin/build
  2. +36 −7 src/server/charted.js
@@ -3,7 +3,6 @@

const sh = require('shelljs/global')
const babel = require('babel-core')
const handlebars = require('handlebars')
const requirejs = require('requirejs')
const uglify = require('uglify-js')
const path = require('path')
@@ -61,10 +60,6 @@ function main(args) {
let dest = cmd.replace('public/', '')
cp('-rf', `src/${cmd}`, `out/client/${dest}`)
}

if (cmd.startsWith('templates') && cmd.endsWith('.html')) {
template(`src/${cmd}`)
}
})

return
@@ -93,12 +88,6 @@ function main(args) {
echo('Compiling LESS')
less('src/styles/charted.less', 'out/client/charted.css')

// Compile templates
ls('-R', 'src/templates/*.html').forEach((file) => {
echo(`Compiling ${file}`)
template(file)
})

// Compile client-side JavaScript
ls('-R', 'src/client/*.js').forEach((file) => {
echo(`Compiling ${file}`)
@@ -160,14 +149,6 @@ function transpile(file, type, isShared) {
res.code.to(`${dest}/${name}`)
}

function template(file) {
let name = file.replace('src/templates/', '')
let code = cat(file)
let res = handlebars.compile(code)({ENV: ENV})
res.to(`out/client/${name}`)
}


// Get to the root of the project
while (!test('-f', 'package.json')) { cd('..') }
main(process.argv)
@@ -6,14 +6,19 @@ import url from "url"
import path from "path"
import request from "request"
import express from "express"
import sh from "shelljs"
import fs from 'fs'
import handlebars from "handlebars"
import bodyParser from "body-parser"
import prepare from "./prepare"
import FileDb from "./db.js"
import sha1 from "../shared/sha1"
import * as utils from "../shared/utils"

type TemplateParams = {
ENV?: Object,
dataUrl?: string
}

export default class ChartedServer {
address: any;
staticRoot: string;
@@ -79,6 +84,7 @@ export default class ChartedServer {
app.use(bodyParser.json())
app.use(express.static(this.staticRoot))

app.get('/', this.getHome.bind(this))
app.get('/c/:id', this.getChart.bind(this))
app.get('/embed/:id', this.getChart.bind(this))
app.post('/c/:id', this.saveChart.bind(this))
@@ -92,20 +98,43 @@ export default class ChartedServer {
})
}

render(name: string, options: ?TemplateParams): Promise<string> {
return new Promise((resolve, reject) => {
const fp = path.join(__dirname, '..', 'templates', name)

fs.readFile(fp, 'utf8', (err, data) => {
if (err) {
reject(err)
return
}

options = options || {}
options.ENV = this.env
const html = handlebars.compile(data)(options)
resolve(html)
})
})
}

getHome(req: express$Request, res: express$Response) {
this.render('index.html')
.then((html) => {
res.status(200).send(html)
})
}

getChart(req: express$Request, res: express$Response) {
this.store.get(req.params.id).then((params) => {
if (!params) {
this.notFound(res, `chart ${req.params.id} was not found.`)
return
}

let code = sh.cat(path.join(__dirname, '..', 'templates', 'index.html'))
let html = handlebars.compile(code)({
ENV: this.env,
dataUrl: params.dataUrl
})

res.status(200).send(html)
this.render('index.html', {dataUrl: params.dataUrl})
.then((html) => {
res.status(200).send(html)
})
})
}

0 comments on commit 535ee51

Please sign in to comment.
You can’t perform that action at this time.