-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from airavata-courses/develop
Develop
- Loading branch information
Showing
70 changed files
with
4,932 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,5 @@ $RECYCLE.BIN/ | |
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk | ||
.apdisk | ||
node_modules |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const mongoose = require('mongoose') | ||
const UserSchema = new mongoose.Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
} | ||
}) | ||
module.exports = mongoose.model('User',UserSchema) |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Vulcan | ||
|
||
## API Gateway | ||
|
||
Below are the steps to run the API Gateway Service. Inside the api_gateway folder run - | ||
|
||
``` | ||
docker-compose up -d | ||
``` | ||
|
||
This will spin up the mongoDB server inside a docker container. | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
To install all the node dependencies. | ||
|
||
``` | ||
node index.js | ||
``` | ||
|
||
This will run the api-gateway server on port 3000. | ||
|
||
For detailed information on this service, visit [LINK](https://github.com/airavata-courses/vulcan/wiki/API-GATEWAY-WIKI) |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const kafka = require("kafka-node"); | ||
// const Producer = kafka.Producer; | ||
// const Consumer = kafka.Consumer; | ||
|
||
var Consumer = kafka.Consumer, | ||
client = new kafka.KafkaClient("localhost:9092"), | ||
consumer = new Consumer( | ||
client, [ { topic: "gateway", partition: 0 } ], { autoCommit: false }); | ||
|
||
|
||
var Producer = kafka.Producer, | ||
// KeyedMessage = kafka.KeyedMessage, | ||
client = new kafka.KafkaClient("localhost:9092"), | ||
producer = new Producer(client) | ||
|
||
// const ConsumerGroup = kafka.ConsumerGroup | ||
// const client = new kafka.KafkaClient({ kafkaHost: "kafka-service:9092" }); | ||
// let config = require("./config"); | ||
// producer = new Producer(client); | ||
|
||
|
||
module.exports = { producer, consumer }; | ||
|
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.7' | ||
services: | ||
mongodb_container: | ||
image: mongo:latest | ||
ports: | ||
- 27017:27017 | ||
volumes: | ||
- mongodb_data_container:/data/db | ||
|
||
volumes: | ||
mongodb_data_container: |
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const express = require('express') | ||
const mongoose = require('mongoose') | ||
const passport = require('passport') | ||
const bodyParser = require('body-parser') | ||
const User = require('./Models/User') | ||
const jwt = require('jsonwebtoken'); | ||
require('./passport') | ||
const app = express() | ||
const server = require('http').createServer(app); | ||
const cors = require('cors') | ||
const WebSocket = require('ws'); | ||
const wss = new WebSocket.Server({ server }); | ||
app.use(cors()); | ||
|
||
genToken = user => { | ||
return jwt.sign({ | ||
iss: 'Joan_Louji', | ||
sub: user.id, | ||
iat: new Date().getTime(), | ||
exp: new Date().setDate(new Date().getDate() + 1) | ||
}, 'joanlouji'); | ||
} | ||
app.use(bodyParser.json()) | ||
|
||
const consumer = require('./config/kafka-config').consumer | ||
const producer = require("./config/kafka-config").producer | ||
|
||
// This can go inside the post request when user makes the data request or when login is authenticated. | ||
wss.on('connection', function connection(ws) { | ||
console.log('A new client Connection!') | ||
// ws.send('Welcome new Client'); | ||
consumer.on("message", function (message) { | ||
console.log('received: %s', message.value); | ||
ws.send(message.value) | ||
}) | ||
|
||
ws.on('message', function incoming(message) { | ||
msg = new TextDecoder('utf-8').decode(new Uint8Array(message)) | ||
console.log(msg) | ||
let payloads = [ | ||
{ | ||
topic: "ingestor", | ||
messages: msg | ||
}]; | ||
|
||
producer.send(payloads, function (err, data) { | ||
console.error(err); | ||
}); | ||
|
||
producer.on('error', function (err) { | ||
console.error(err) | ||
}) | ||
}); | ||
}); | ||
|
||
|
||
|
||
|
||
app.post('/register', async function (req, res, next) { | ||
const { email, password } = req.body; | ||
|
||
//Check If User Exists | ||
let foundUser = await User.findOne({ email }); | ||
if (foundUser) { | ||
return res.status(403).json({ error: 'Email is already in use' }); | ||
} | ||
|
||
const newUser = new User({ email, password }) | ||
await newUser.save() | ||
// Generate JWT token | ||
const token = genToken(newUser) | ||
res.status(200).json({ token }) | ||
}); | ||
|
||
|
||
mongoose.connect("mongodb://localhost/27017", { useNewUrlParser: true, useUnifiedTopology: true }); | ||
mongoose.connection.once('open', function () { | ||
console.log('Connected to Mongo'); | ||
}).on('error', function (err) { | ||
console.log('Mongo Error', err); | ||
}) | ||
|
||
|
||
app.post('/login', async (req, res, next) => { | ||
const { email, password } = req.body; | ||
let foundUser = await User.findOne({ email, password }); | ||
if (foundUser) { | ||
return res.status(200).json("Login Succesful"); | ||
} | ||
return res.status(403).json("Invalid User Name") | ||
}) | ||
|
||
app.get('/secret', passport.authenticate('jwt', { session: false }), (req, res, next) => { | ||
res.json("Secret Data") | ||
}) | ||
|
||
|
||
server.listen(3000, () => console.log('Listenining on port : 3000')) |
Oops, something went wrong.