Add Gist support

main
Mike Fix 8 years ago
parent 820cd3b13a
commit b16b0d6d78

@ -1,8 +1,13 @@
import axios from 'axios' import axios from 'axios'
export default { const gistClient = axios.create({
uploadImage baseURL: 'https://api.github.com',
} timeout: 5000,
headers: {
Accept: 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
}
});
async function uploadImage (encodedImage) { async function uploadImage (encodedImage) {
// upload image // upload image
@ -29,3 +34,18 @@ async function uploadImage (encodedImage) {
console.log(e) console.log(e)
} }
} }
const getGist = (id) => {
const uid = id.split('/').pop()
return gistClient.get(`/gists/${uid}`)
.then(res => res.data)
.then(gist => gist.files)
.then(files => files[Object.keys(files)[0]])
.then(file => file.content)
}
export default {
uploadImage,
getGist
}

@ -5,7 +5,8 @@
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "node server.js", "dev": "node server.js",
"next": "next" "build": "next build",
"start": "next start"
}, },
"dependencies": { "dependencies": {
"axios": "^0.16.2", "axios": "^0.16.2",

@ -2,7 +2,7 @@ import React from 'react'
import Axios from 'axios' import Axios from 'axios'
import CodeImage from '../components/codeImage' import CodeImage from '../components/codeImage'
import api from '../api' import api from '../lib/api'
const code = ` const code = `
const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj) const pluckDeep = key => obj => key.split('.').reduce((accum, key) => accum[key], obj)
@ -17,22 +17,34 @@ const unfold = (f, seed) => {
return go(f, seed, []) return go(f, seed, [])
}` }`
export default (props) => { export default class extends React.Component {
return ( /* pathname, asPath, err, req, res */
<div> static async getInitialProps ({ asPath }) {
<style jsx>{` try {
div { const content = await api.getGist(asPath)
display: flex; return { content }
flex-direction: column; } catch (e) {
justify-content: center; console.log(e)
align-items: center; return {}
} }
`} }
</style> render () {
<h1> Welcome to Code Image</h1> return (
<CodeImage> <div>
{code} <style jsx>{`
</CodeImage> div {
</div> display: flex;
) flex-direction: column;
justify-content: center;
align-items: center;
}
`}
</style>
<h1>Welcome to Code Image</h1>
<CodeImage>
{this.props.content || code}
</CodeImage>
</div>
)
}
} }

@ -22,17 +22,15 @@ app.prepare()
server.use(morgan('tiny')) server.use(morgan('tiny'))
// if root, render webpage from next // if root, render webpage from next
server.get('/', (req, res) => { server.get('*', (req, res) =>
return app.render(req, res, '/', req.query) app.render(req, res, '/', req.query))
})
// otherwise, try and get gist
server.get('*', handle)
// api endpoints // api endpoints
server.post('/upload', bodyParser.json(), wrap(require('./handlers/upload'))) server.post('/upload', bodyParser.json(), wrap(require('./handlers/upload')))
// otherwise, try and get gist
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, (err) => { server.listen(3000, (err) => {
if (err) throw err if (err) throw err

Loading…
Cancel
Save