Skip to content

Commit

Permalink
Se modifica el directorio del notifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano-baffetti committed Feb 29, 2024
1 parent b020238 commit 4275208
Show file tree
Hide file tree
Showing 7 changed files with 1,519 additions and 6 deletions.
10 changes: 5 additions & 5 deletions NotifierDockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
FROM node:18.16-alpine AS dependencies

WORKDIR /notifier
COPY delta_notifier/package.json delta_notifier/yarn.lock ./
RUN yarn
COPY notifier/package.json ./
RUN npm install


##
FROM node:18.16-alpine AS build

WORKDIR /notifier
COPY delta_notifier ./
COPY notifier ./
RUN apk add --update --no-cache openssl
RUN yarn install
RUN npm install

###
FROM node:18.16-alpine AS deploy
Expand All @@ -20,7 +20,7 @@ WORKDIR /notifier

ENV NODE_ENV production

COPY delta_notifier ./
COPY notifier ./

EXPOSE 3001

Expand Down
1 change: 0 additions & 1 deletion delta_notifier
Submodule delta_notifier deleted from a606af
4 changes: 4 additions & 0 deletions notifier/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SECRET=secret
PRIVATE_KEY=123
WS_PORT=8080
PORT=3001
70 changes: 70 additions & 0 deletions notifier/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require('dotenv').config()
const express = require('express')
const jwt = require('jsonwebtoken');
const WebSocket = require("ws");


const app = express()
const port = 3001

const webSocketPort = 8080;
const wss = new WebSocket.Server({port: webSocketPort});
console.log(`[WebSocket] Starting WebSocket server on localhost:${webSocketPort}`);

app.use(express.json());

app.post('/onUpdate', authenticate, (req, res) => {
res.send(`deviceId: ${req.body.deviceId} latitude: ${req.body.latitude} longitude: ${req.body.longitude}`);

console.log(`${wss.clients.size}`);
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(req.body));
}
});
})

app.post('/auth', (req, res) => {
const {body} = req
const {privateKey} = body

if (privateKey && privateKey === process.env.PRIVATE_KEY) {
const token = jwt.sign(
{id: 'DeltaApp'},
process.env.SECRET,
{
expiresIn: 60 * 60 * 24
}
)
res.send({token})
} else {
res.status(401).json({
error: 'Invalid credentials'
})
}
})

function authenticate(req, res, next) {
const authorization = req.get('authorization')
let token = ''

if (authorization && authorization.toLowerCase().startsWith('bearer')) {
token = authorization.substring(7)
}
try {
jwt.verify(token, process.env.SECRET)
} catch (e) {
return res.status(401).json({error: 'token missing or invalid'})
}

next()
}


app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

wss.on("connection", (ws, request) => {
console.log('[WebSocket] has connected');
});
Loading

0 comments on commit 4275208

Please sign in to comment.