-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.js
139 lines (118 loc) · 3.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const fs = require('fs')
const {find} = require('lodash')
const cors = require('cors')({ origin: true })
const express = require('express')
const { json } = require('body-parser');
console.log('CONFIGURATION', process.env.HEROKU_API_TOKEN)
// Heroku
const Heroku = require('heroku-client')
const heroku = new Heroku({ token: process.env.HEROKU_API_TOKEN })
// heroku.get('/apps/primo/config-vars').then(apps => {
// console.log(apps)
// })
// Serve primo
const { join } = require('path');
const primoDir = join(__dirname, 'public');
const primoServe = require('serve-static')(primoDir);
// Database
const {saveData,getData,connectDatabase} = require('./server/mongo')
// PostCSS
const processStyles = require('./functions/postcss')
// Building
const {buildSite,combineTailwindConfigs,buildPageCSS} = require('./functions/build')
// SSR
const renderer = require('./functions/render')
// Storage
const bucketID = process.env.AWS_S3_BUCKET
const bucketRegion = process.env.AWS_S3_REGION
const {setupS3,uploadFile} = require('./server/hosting/aws')
// // Primo
// function serve(req, res, next) {
// const editing = req.headers.host.split('.')[0] === 'edit'
// if (editing) primoServe(req, res, next)
// else next()
// }
// Authentication
const {passport,createUser} = require('./server/auth')
express()
.use(passport.initialize())
.use(passport.session())
.use(json())
.use(cors)
.use(primoServe)
.post('/__fn/postcss', async (req, res) => {
const styles = await processStyles(req.body)
res.send(styles);
})
.post('/login', (req, res, next) => {
passport.authenticate('local', function(err, user, info) {
if (err) return next(err) // not being passed in yet
if (!user) return res.send(info)
req.logIn(user, function(err) {
if (err) { return next(err); } // not being passed in yet
return res.send({ success: true });
});
})(req, res, next);
})
.post('/__fn/build', async (req, res) => {
const siteFiles = await buildSite(req.body)
const stored = await Promise.all(
siteFiles.map(uploadFile)
)
res.send(file);
})
// Database
.get('/__fn/data', async (req, res) => {
const data = await getData('site')
console.log(data)
res.send(data);
})
.post('/__fn/data', async (req, res) => {
await saveData('site', JSON.stringify(req.body))
res.send(true);
})
.post('/__fn/auth/create', async (req, res) => {
await createUser(req.body)
res.send({ success: true });
})
// SETUP
.post('/__fn/setup/db', async (req, res) => {
// TODO: set environment variable
const success = await connectDatabase(req.body.uri)
res.send({ success })
})
.post('/__fn/setup/hosting', async (req, res) => {
console.log('TODO', req.body)
const response = await setupS3(req.body)
res.send(response);
})
// Static Site Generation
// .get('/', (req, res) => {
// res.redirect(`http://${bucketID}.s3-website.${bucketRegion}.amazonaws.com/`)
// })
// .get('/:page', (req, res) => {
// const {page} = req.params
// res.redirect(`http://${bucketID}.s3-website.${bucketRegion}.amazonaws.com/${page}`)
// })
// Server-side Rendering (assets served from S3)
// .get('/:page.html', (req, res) => {
// const pageID = req.params.page
// const page = find(site.pages, ['id', pageID])
// if (page) {
// const HTML = renderer.renderPage(page, site)
// res.send(HTML);
// } else res.send('')
// })
// .get('/:page', (req, res) => {
// const {page} = req.params
// res.redirect(`http://${bucketID}.s3-website.us-east-2.amazonaws.com/${page}`)
// })
// .get('/', (req, res) => {
// const page = find(site.pages, ['id', 'index'])
// const HTML = renderer.renderPage(page, site)
// res.send(HTML);
// })
.listen(3005, err => {
if (err) throw err;
console.log(`> Running on localhost:3005`);
});