2017-06-06 12:40:51 +00:00
|
|
|
const Koa = require('koa')
|
|
|
|
const path = require('path')
|
|
|
|
const mongoose = require('mongoose')
|
2017-06-01 08:32:42 +00:00
|
|
|
|
2017-06-01 10:09:25 +00:00
|
|
|
// Load config
|
2017-06-06 12:40:51 +00:00
|
|
|
const config = require('./config.json')
|
2017-06-01 10:09:25 +00:00
|
|
|
// custom modules
|
2017-06-06 12:40:51 +00:00
|
|
|
const mysql = require('./libs/mysql_pool')
|
|
|
|
// const so = require('./libs/storeObject')
|
|
|
|
const mem = require('./libs/memcache_lib')
|
|
|
|
// const sendmail = require('./libs/sendmail')
|
2017-06-01 10:09:25 +00:00
|
|
|
|
2017-06-05 10:25:48 +00:00
|
|
|
// init memcached connection
|
2017-06-06 12:40:51 +00:00
|
|
|
mem.host = 'dyn.trj.tw'
|
|
|
|
mem.port = 24002
|
|
|
|
mem.connect()
|
2017-06-03 13:20:34 +00:00
|
|
|
|
2017-06-01 10:09:25 +00:00
|
|
|
// init mysql connection pool
|
2017-06-06 12:40:51 +00:00
|
|
|
mysql.database = config.db.mysql.dbname
|
|
|
|
mysql.host = config.db.mysql.host
|
|
|
|
mysql.user = config.db.mysql.user
|
|
|
|
mysql.password = config.db.mysql.pass
|
|
|
|
mysql.port = config.db.mysql.port
|
|
|
|
mysql.createPool()
|
2017-06-01 10:09:25 +00:00
|
|
|
|
2017-06-02 14:53:34 +00:00
|
|
|
// init mongodb connection
|
2017-06-06 12:40:51 +00:00
|
|
|
let mongoAuth = `${config.db.mongo.user.length > 0 && config.db.mongo.pass.length > 0 ? `${config.db.mongo.user}:${config.db.mongo.pass}@` : ''}`
|
|
|
|
let mongoUrl = `mongodb://${mongoAuth}${config.db.mongo.host}:${config.db.mongo.port}/${config.db.mongo.dbname}${mongoAuth.length > 0 ? '?authSource=admin' : ''}`
|
|
|
|
mongoose.connect(mongoUrl)
|
2017-06-02 14:53:34 +00:00
|
|
|
|
2017-06-01 08:32:42 +00:00
|
|
|
// Koa Middleware
|
2017-06-06 12:40:51 +00:00
|
|
|
const session = require('koa-session')
|
|
|
|
const Router = require('koa-router')
|
|
|
|
// const koaBody = require('koa-body')
|
|
|
|
const koaStatic = require('koa-static')
|
|
|
|
const cors = require('kcors')
|
|
|
|
const logger = require('koa-morgan')
|
2017-06-01 08:32:42 +00:00
|
|
|
|
|
|
|
// Koa Main Application
|
2017-06-06 12:40:51 +00:00
|
|
|
const app = new Koa()
|
2017-06-01 08:32:42 +00:00
|
|
|
|
2017-06-03 13:20:34 +00:00
|
|
|
const server = app.listen(config.port, () => {
|
2017-06-06 12:40:51 +00:00
|
|
|
console.log(`Server start on port ${server.address().port}`)
|
|
|
|
})
|
2017-06-01 08:32:42 +00:00
|
|
|
|
|
|
|
// Root Router
|
2017-06-06 12:40:51 +00:00
|
|
|
const router = new Router()
|
2017-06-01 08:32:42 +00:00
|
|
|
|
2017-06-06 12:40:51 +00:00
|
|
|
// set app keys
|
|
|
|
app.keys = ['44b4fa5cb8a394294361']
|
2017-06-01 14:37:51 +00:00
|
|
|
|
|
|
|
// enable logger
|
2017-06-06 12:40:51 +00:00
|
|
|
app.use(logger('dev'))
|
2017-06-01 08:32:42 +00:00
|
|
|
// enable body parser
|
2017-06-01 14:37:51 +00:00
|
|
|
// app.use(koaBody({
|
|
|
|
// multipart: true,
|
|
|
|
// // upload file size 10mb
|
|
|
|
// maxFieldSize: 10 * 1024 * 1024
|
|
|
|
// }));
|
2017-06-01 08:32:42 +00:00
|
|
|
// enable cors
|
2017-06-06 12:40:51 +00:00
|
|
|
app.use(cors())
|
2017-06-01 08:32:42 +00:00
|
|
|
// enable session
|
|
|
|
app.use(session({
|
2017-06-06 12:40:51 +00:00
|
|
|
key: 'koa:sess',
|
|
|
|
maxAge: 86400000,
|
|
|
|
overwrite: true,
|
|
|
|
httpOnly: true,
|
|
|
|
signed: true
|
|
|
|
}, app))
|
|
|
|
// enable static file
|
|
|
|
app.use(koaStatic(path.resolve(__dirname, 'public')))
|
2017-06-01 14:37:51 +00:00
|
|
|
// enable router
|
2017-06-06 12:40:51 +00:00
|
|
|
app.use(router.routes())
|
|
|
|
app.use(router.allowedMethods())
|
2017-06-01 08:32:42 +00:00
|
|
|
|
2017-06-01 10:09:25 +00:00
|
|
|
// load other route
|
2017-06-06 12:40:51 +00:00
|
|
|
const apiRoute = require('./route/api')
|
2017-06-01 10:09:25 +00:00
|
|
|
|
|
|
|
// set other route
|
2017-06-06 12:40:51 +00:00
|
|
|
router.use('/api', apiRoute.routes())
|
2017-06-01 10:09:25 +00:00
|
|
|
|
2017-06-01 08:32:42 +00:00
|
|
|
router.get('/', async(c, n) => {
|
2017-06-06 12:40:51 +00:00
|
|
|
c.body = 'Get root'
|
|
|
|
})
|