This commit is contained in:
Jay 2018-06-25 18:07:10 +08:00
commit 075b68012e
9 changed files with 120 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
.env
package-lock.json

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM node:8
LABEL maintainer="Jay <jie.chen@tw.viewsonic.com>"
ENV NODE_PORT 5111
RUN mkdir -p /data
WORKDIR /data
COPY . .
RUN rm .env
RUN npm install
EXPOSE ${NODE_PORT}
CMD ["npm", "start"]

15
app.js Normal file
View File

@ -0,0 +1,15 @@
const config = require('./config')
const Koa = require('koa')
const koaLogger = require('koa-logger')
const router = require('./route')
const app = new Koa()
app.use(koaLogger())
app.use(router.allowedMethods())
app.use(router.routes())
const server = app.listen(config.port, () => {
console.log(`server start on port ${server.address().port}`)
})
module.exports = server

7
config/index.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = {
port: process.env.NODE_PORT || 10230,
line: {
secret: process.env.LINE_SECRET || '',
access: process.env.LINE_ACCESS || ''
}
}

7
index.js Normal file
View File

@ -0,0 +1,7 @@
const fs = require('fs')
const path = require('path')
try {
fs.accessSync(path.resolve(__dirname, '.env'))
require('dotenv').config()
} catch (err) {}
require('./app')

15
libs/middleware/index.js Normal file
View File

@ -0,0 +1,15 @@
const config = require('../../config')
const crypto = require('crypto')
const verifyLine = async (c, n) => {
if (!('request' in c) || !('raw' in c.request)) return c.throw(400, 'body not found')
let xSignature = c.get('x-line-signature') || ''
if (typeof xSignature !== 'string' || xSignature.length === 0) return c.throw(400, 'signature not found')
let signature = crypto.createHmac('SHA256', config.line.secret).update(c.request.raw).digest('base64')
if (signature !== xSignature) return c.throw(403, 'signature not match')
return n()
}
module.exports = {
verifyLine
}

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "mtfosbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js 2>&1 | tee runtime.txt",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "JayChen",
"license": "ISC",
"dependencies": {
"dotenv": "^6.0.0",
"koa": "^2.5.1",
"koa-body": "^4.0.3",
"koa-logger": "^3.2.0",
"koa-router": "^7.4.0"
},
"devDependencies": {
"standard": "^11.0.1"
}
}

6
route/index.js Normal file
View File

@ -0,0 +1,6 @@
const Router = require('koa-router')
const r = new Router()
r.use('/line', require('./line').routes())
module.exports = r

34
route/line/index.js Normal file
View File

@ -0,0 +1,34 @@
const Router = require('koa-router')
const r = new Router()
// const koaBody = require('koa-body')
const rawBody = require('raw-body')
const {
verifyLine
} = require('../../libs/middleware')
const getRaw = async (c, n) => {
let raw = await rawBody(c.req, {
length: c.request.length,
limit: '5mb',
encoding: c.request.charset
})
c.request.raw = raw
let txt = raw instanceof Buffer ? raw.toString() : raw
if (c.request.type === 'application/json') {
try {
c.request.body = JSON.parse(txt)
} catch (err) {
c.request.body = txt
}
}
return n()
}
r.post('/', getRaw, verifyLine, async (c, n) => {
console.log(c.request.body)
if (!('event' in c.request.body)) return c.throw(400, 'data struct error')
c.body = 'success'
c.status = 200
})
module.exports = r