diff --git a/app/api/chat.js b/app/api/chat.js new file mode 100644 index 0000000000000000000000000000000000000000..7ad36840748b6d60a9cf4e0f37ad64558662f8c6 --- /dev/null +++ b/app/api/chat.js @@ -0,0 +1,54 @@ +/* + * Tugas Besar 3 WBD - AMEN + */ + +'use strict' + +const express = require('express') + +const cloud = require('../service/cloud') +const relay = require('../service/relay') + +let app = express.Router() + +// Administration. + +app.post('/:id/token', function (req, res) { + let id = req.params.id + let token = req.body.token + + cloud.register(id, token) + + res.json({ status: 'ok' }) +}) + +app.delete('/:id', function (req, res) { + let id = req.params.id + + cloud.unregister(id) + relay.disassociate(id) + + res.json({ status: 'ok' }) +}) + +// Messaging. + +app.post('/:id/message', async function (req, res) { + let id = req.params.id + let message = req.body.message + + try { + await relay.sendAsync(id, message) + + res.json({ status: 'ok' }) + } catch (e) { + console.log(e) + + res.json({ + status: 'error', + message: e.message + }) + } +}) + +module.exports = app diff --git a/app/api/driver.js b/app/api/driver.js index ff5745ce7892fd35b01fa832e49e6f947661ad51..2fc73aa75908207cf2652f01882f60eb4f3e2851 100644 --- a/app/api/driver.js +++ b/app/api/driver.js @@ -7,6 +7,7 @@ const express = require('express') const hub = require('../service/hub') +const relay = require('../service/relay') let app = express.Router() @@ -39,9 +40,17 @@ app.post('/pick', function (req, res) { let driverId = req.body.driver_id let userId = req.body.user_id - res.json({ - 'result': hub.pick(driverId, userId) - }) + try { + // Pick driver by user. + hub.pick(driverId, userId) + + // Associate user with driver. + relay.associate(userId, driverId) + + res.json({ 'status': 'ok' }) + } catch (e) { + res.status(500).json({ 'status': 'error' }) + } }) module.exports = app diff --git a/app/index.js b/app/index.js index d4ff4a280f2a41a3acf573f848b8cf80d07e00e2..dcd0d277b53b985ef9b3b8dacd763fdea6b89f01 100644 --- a/app/index.js +++ b/app/index.js @@ -4,8 +4,8 @@ 'use strict' -// Load .env file early. -// require('dotenv').config() +// Load config. file early. +require('dotenv').config({ path: 'config/env.conf' }) const express = require('express') const http = require('http') @@ -19,7 +19,7 @@ app.use('/', api) // Begin serving. let httpServer = http.createServer(app) -let port = process.env.PORT || 8081 +let port = process.env.CHAT_PORT || 8081 httpServer.listen(port, function (err) { if (err) { diff --git a/app/service/cloud.js b/app/service/cloud.js index d6996a3a36e9172fdc530eddb8cd8846ed303c6e..2c817106b4059874e700844fcd2ecdab874c43ec 100644 --- a/app/service/cloud.js +++ b/app/service/cloud.js @@ -4,7 +4,13 @@ 'use strict' -const messaging = require('firebase-admin').messaging() +const admin = require('firebase-admin') + +// Initialize Firebase. +let serviceAccount = require(`../../${process.env.FIREBASE_CERT}`) +admin.initializeApp({ credential: admin.credential.cert(serviceAccount) }) + +let messaging = admin.messaging() let db = {} diff --git a/config/env.conf.example b/config/env.conf.example new file mode 100644 index 0000000000000000000000000000000000000000..ef37282094d6db490c32ae11d4ae2f5df4d419f9 --- /dev/null +++ b/config/env.conf.example @@ -0,0 +1,2 @@ +CHAT_PORT=8081 +FIREBASE_CERT=config/firebase.json