-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,71 @@ | ||
import Koa from 'koa'; | ||
import Router from "koa-router" | ||
import bodyParser from "koa-bodyparser" | ||
import { BotAction } from './bot/bot-action'; | ||
import { BotManager } from './bot/bot-manager'; | ||
import Koa from 'koa' | ||
import Router from 'koa-router' | ||
import bodyParser from 'koa-bodyparser' | ||
import { BotAction } from './bot/bot-action' | ||
import { BotManager } from './bot/bot-manager' | ||
|
||
const app = new Koa(); | ||
const router = new Router(); | ||
const botManager = new BotManager(); | ||
const app = new Koa() | ||
const router = new Router() | ||
const botManager = new BotManager() | ||
|
||
app.use(bodyParser()); | ||
app.use(bodyParser()) | ||
|
||
router.get('/', (ctx) => { | ||
ctx.body = { status: 'ir-bot api is working' }; | ||
}); | ||
ctx.body = { status: 'ir-bot api is working' } | ||
}) | ||
|
||
router.get('/bots', (ctx) => { | ||
ctx.body = botManager.bots; | ||
}); | ||
ctx.body = botManager.bots | ||
}) | ||
|
||
router.get('/bots/:id', (ctx) => { | ||
const id = ctx.params.id; | ||
const bot = botManager.findBotById(id); | ||
ctx.body = bot; | ||
}); | ||
const id = ctx.params.id | ||
const bot = botManager.findBotById(id) | ||
ctx.body = bot | ||
}) | ||
|
||
router.get('/bots/actions', (ctx) => { | ||
const actions = botManager.getActions(); | ||
ctx.body = actions; | ||
}); | ||
const actions = botManager.getActions() | ||
ctx.body = actions | ||
}) | ||
|
||
router.put('/bots/:id/create', (ctx) => { | ||
const id = ctx.params.id; | ||
const body:any = ctx.request.body | ||
const name = body.name; | ||
const options = body.options; | ||
const bot = botManager.addBot(id, name, options); | ||
ctx.body = bot; | ||
}); | ||
const id = ctx.params.id | ||
const body: any = ctx.request.body | ||
const name = body.name | ||
const options = body.options | ||
const bot = botManager.addBot(id, name, options) | ||
ctx.body = bot | ||
}) | ||
|
||
router.post('/bots/:id/actions/add', (ctx) => { | ||
const id = ctx.params.id; | ||
const body:any = ctx.request.body | ||
const action: BotAction = body.action; | ||
botManager.addAction(id, action); | ||
ctx.status = 200; | ||
}); | ||
const id = ctx.params.id | ||
const body: any = ctx.request.body | ||
const action: BotAction = body.action | ||
botManager.addAction(id, action) | ||
ctx.status = 200 | ||
}) | ||
|
||
router.post('/bots/run', async (ctx) => { | ||
await botManager.run(); | ||
ctx.status = 200; | ||
}); | ||
await botManager.run() | ||
ctx.status = 200 | ||
}) | ||
|
||
router.delete('/bots/:id/delete', async (ctx) => { | ||
const id = ctx.params.id; | ||
const bot = botManager.removeBot(id); | ||
ctx.status = 200; | ||
}); | ||
const id = ctx.params.id | ||
const bot = botManager.removeBot(id) | ||
ctx.status = 200 | ||
}) | ||
|
||
router.delete('/bots/clear', async (ctx) => { | ||
await botManager.clear(); | ||
ctx.status = 200; | ||
}); | ||
await botManager.clear() | ||
ctx.status = 200 | ||
}) | ||
|
||
app.use(router.routes()); | ||
app.use(router.allowedMethods()); | ||
app.use(router.routes()) | ||
app.use(router.allowedMethods()) | ||
|
||
const PORT = process.env.PORT || 4000 | ||
app.listen(PORT, () => { | ||
console.log(`Koa Server listening on port ${PORT}`); | ||
}); | ||
console.log(`Koa Server listening on port ${PORT}`) | ||
}) |