This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
forked from crackcomm/hugo-sandstorm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
208 lines (169 loc) · 4.91 KB
/
server.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import Nuxt from "nuxt"
import logger from "morgan"
import fs from 'fs'
import Express from "express"
import gitBackend from "git-http-backend"
import cloudcmd from "cloudcmd"
import io from 'socket.io'
const spawn = require("child_process").spawn
const app = new Express()
app.use(logger("dev"))
const server = require("http").createServer(app)
const host = process.env.HOST || "127.0.0.1"
const port = process.env.PORT || "8000"
app.set("port", port)
app.get("/publicId", (req, res) => {
// For those reusing getPublicId, keep in mind that the session ID from the
// headers isn't used to generate the address for a grain, only the grain's
// internal ID. Once you've generated it once, that's it.
//
// See https://github.com/sandstorm-io/sandstorm/blob/1a1f5650472904e137393af077a3d90f094cd888/shell/imports/server/hack-session.js#L334-L365
// for more information.
const sessionId = req.headers["x-sandstorm-session-id"]
let allData = ""
// TODO: needed until https://github.com/sandstorm-io/sandstorm/pull/3292 is merged
//
// If this app is running in demo mode, its data will be thrown away in an hour,
// and the public ID assigned to the grain will also go away, so it's still OK
// to keep this file around, even in a demo scenario.
const file = `/var/publicid`
const handleResult = () => {
const lines = allData.split("\n")
const publicId = lines[0]
const hostname = lines[1]
const domain = publicId+"."+hostname
const url = lines[2]
const isDemo = lines[3] == "true"
res.json({publicId, hostname, domain, url, isDemo})
}
try {
allData = fs.readFileSync(file, 'utf8').toString()
} catch (e) {
// file probably doesn't exist
}
const lines = allData.split("\n")
if (lines.length >= 4) {
handleResult()
} else {
const gpId = spawn('getPublicId', [sessionId])
allData = ""
gpId.stdout.on('data', (data) => {
fs.appendFileSync(file, data)
allData += data
})
gpId.on('error', (err) => {
return res.send(err)
})
gpId.on('close', (code) => {
if (code !== 0) {
return res.send(code)
}
handleResult()
})
}
})
app.use("/git", (req, res) => {
req.pipe(gitBackend(req.url, (err, service) => {
if(err)
return res.end(err+"\n")
res.setHeader("content-type", service.type)
console.log("cmd", service.cmd)
const ps = spawn(service.cmd, service.args.concat("/var/git"))
ps.stdout.pipe(service.createStream()).pipe(ps.stdin)
})).pipe(res)
})
const runCommand = (cmd, ...args) => {
return new Promise((resolve, reject) => {
const spawnCmd = spawn(cmd, args)
spawnCmd.stdout.on('data', (data) => {
console.log(data.toString())
})
spawnCmd.on('error', (err) => {
res.send(err)
reject(err)
})
spawnCmd.on('close', (code) => {
if (code === 0) {
resolve(true)
return true
} else {
reject(new Error(code))
}
})
})
}
app.get('/dirty', async (req, res) => {
const currentDir = process.cwd()
process.chdir('/var/git')
const spawnCmd = spawn('git', ['diff', '--exit-code'])
spawnCmd.on('close', (code) => {
res.json({ dirty: code !== 0 })
})
process.chdir(currentDir)
})
app.use('/commit', async (req, res) => {
const currentDir = process.cwd()
try {
process.chdir('/var/git')
await runCommand('git', 'add', '.')
await runCommand('git', 'commit', '-m', 'From admin')
process.chdir(currentDir)
await runCommand('/opt/app/post-receive')
res.json({ok: true})
} catch (e) {
res.send({error: e.msg})
} finally {
process.chdir(currentDir)
}
})
app.use('/reset-local', async (req, res) => {
const currentDir = process.cwd()
try {
process.chdir('/var/git')
await runCommand('git', 'reset', '--hard')
process.chdir(currentDir)
res.json({ok: true})
} catch (e) {
res.send({error: e.msg})
} finally {
process.chdir(currentDir)
}
})
const { createConfigManager, configPath } = cloudcmd
const socket = io.listen(server, { path: "/admin/socket.io"})
const cloudConfig = {
name: "Hugo admin",
root: "/var/git",
open: false,
prefix: "/admin",
console: false,
terminal: false,
oneFilePanel: true,
configDialog: false,
configAuth: false,
keysPanel: true,
}
const filePicker = {
data: { FilePicker: { key: 'key' } }
}
const cloudModules = { filePicker }
const configManager = createConfigManager({ configPath })
app.use("/admin", cloudcmd({
socket,
config: cloudConfig
}))
// Import and Set Nuxt.js options
let config = require("./nuxt.config.js")
config.dev = !(process.env.NODE_ENV === "production")
// Init Nuxt.js
const nuxt = new Nuxt(config)
app.use(nuxt.render)
// Build only in dev mode
if (config.dev) {
nuxt.build()
.catch((error) => {
console.error(error) // eslint-disable-line no-console
process.exit(1)
})
}
server.listen(port, host, () => console.log(`Server listening on ${host}:${port}`))