This repository was archived by the owner on Dec 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (69 loc) · 1.84 KB
/
app.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
// Packages
const Koa = require('koa')
const BodyParser = require('koa-body')
const Router = require('koa-router')
const Session = require('koa-session')
const Next = require('next')
// Ours
const api = require('./api')
const { dev, port, secret } = require('./config/env')
const passport = require('./api/auth')
const github = require('./lib/github')
const next = Next({ dev })
const handle = next.getRequestHandler()
next.prepare().then(() => {
const app = new Koa()
const router = new Router()
// Keys
app.keys = [secret]
// Body parser
app.use(BodyParser())
// Session
app.use(Session({ key: 'session' }, app))
// Passport
app.use(passport.initialize())
app.use(passport.session())
// API routes
router.use(api.routes())
router.use(api.allowedMethods())
// Submission page
router.get('/to/:owner/:name', async ctx => {
if (!ctx.isAuthenticated()) {
// Will be used later by Passport
ctx.session.returnTo = ctx.href
ctx.redirect('/login')
} else {
// GitHub token
const { token } = ctx.state.user
const { owner, name } = ctx.params
const repo = await github.getRepo(token, owner, name)
if (!repo) {
ctx.status = 404
ctx.body = "We couldn't find the repository you're looking for!"
return
}
// Extract necessary details
const { archived, has_issues } = repo
// Will we be able to create a new issue?
if (!has_issues || archived) {
ctx.status = 412
ctx.body = 'The repository is either archived or issues are disabled!'
return
}
ctx.status = 200
await next.render(ctx.req, ctx.res, '/_form', ctx.query)
ctx.respond = false
}
})
// Other routes
router.get('*', async ctx => {
await handle(ctx.req, ctx.res)
ctx.respond = false
})
app.use(router.routes())
// Start app
app.listen(port, err => {
if (err) throw err
console.log(`> Ready on :${port}`)
})
})