🔧 Trying out some refactoring on socket connection to Import service
This commit is contained in:
@@ -2,13 +2,12 @@ import React from "react";
|
|||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
const Navbar: React.FunctionComponent = (props) => {
|
const Navbar: React.FunctionComponent = (props) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<nav className="navbar is-fixed-top">
|
<nav className="navbar is-fixed-top">
|
||||||
<div className="navbar-brand">
|
<div className="navbar-brand">
|
||||||
<a className="navbar-item" href="http://bulma.io">
|
<a className="navbar-item" href="http://bulma.io">
|
||||||
<img
|
<img
|
||||||
src="threetwo.png"
|
src="public/threetwo.png"
|
||||||
alt="ThreeTwo! A comic book curator"
|
alt="ThreeTwo! A comic book curator"
|
||||||
width="112"
|
width="112"
|
||||||
height="28"
|
height="28"
|
||||||
|
|||||||
@@ -39,6 +39,6 @@ export const IMPORT_SERVICE_BASE_URI = hostURIBuilder({
|
|||||||
export const SOCKET_BASE_URI = hostURIBuilder({
|
export const SOCKET_BASE_URI = hostURIBuilder({
|
||||||
protocol: "ws",
|
protocol: "ws",
|
||||||
host: process.env.DOCKER_HOST || "localhost",
|
host: process.env.DOCKER_HOST || "localhost",
|
||||||
port: "8051",
|
port: "3001",
|
||||||
apiPath: ``,
|
apiPath: ``,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ const WebSocketContext = createContext(null);
|
|||||||
export const WebSocketProvider = ({ children }): ReactElement => {
|
export const WebSocketProvider = ({ children }): ReactElement => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const socket: Socket = io(SOCKET_BASE_URI);
|
const socket: Socket = io(SOCKET_BASE_URI);
|
||||||
|
|
||||||
socket.on("connect", () => {
|
socket.on("connect", () => {
|
||||||
|
console.log("connected");
|
||||||
dispatch({
|
dispatch({
|
||||||
type: RMQ_SOCKET_CONNECTED,
|
type: RMQ_SOCKET_CONNECTED,
|
||||||
isSocketConnected: true,
|
isSocketConnected: true,
|
||||||
@@ -35,6 +35,7 @@ export const WebSocketProvider = ({ children }): ReactElement => {
|
|||||||
console.log(`disconnect`);
|
console.log(`disconnect`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.emit("bastard", { name: "puk" });
|
||||||
const ws: any = {
|
const ws: any = {
|
||||||
socket,
|
socket,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import express, { Request, Response, Router, Express } from "express";
|
import express, { Request, Response, Router, Express } from "express";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import { createServer } from "http";
|
import { createServer } from "http";
|
||||||
import { Server } from "socket.io";
|
|
||||||
import router from "./route";
|
import router from "./route";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
const amqp = require("amqplib/callback_api");
|
const amqp = require("amqplib/callback_api");
|
||||||
@@ -10,14 +10,6 @@ const amqp = require("amqplib/callback_api");
|
|||||||
const app: Express = express(); // define our app using express
|
const app: Express = express(); // define our app using express
|
||||||
app.use(cors({ origin: "*" }));
|
app.use(cors({ origin: "*" }));
|
||||||
|
|
||||||
const httpServer = createServer();
|
|
||||||
export const io = new Server(httpServer, {
|
|
||||||
cors: {
|
|
||||||
origin: "*",
|
|
||||||
methods: ["GET", "POST"],
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// configure app to use bodyParser for
|
// configure app to use bodyParser for
|
||||||
// Getting data from body of requests
|
// Getting data from body of requests
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
@@ -29,10 +21,6 @@ const port: number = Number(process.env.PORT) || 8050; // set our port
|
|||||||
const rabbitMQConnectionString =
|
const rabbitMQConnectionString =
|
||||||
process.env.RABBITMQ_URI || "amqp://localhost:5672";
|
process.env.RABBITMQ_URI || "amqp://localhost:5672";
|
||||||
|
|
||||||
// Send index.html on root request
|
|
||||||
app.use(express.static("dist"));
|
|
||||||
app.use(express.static("public"));
|
|
||||||
|
|
||||||
app.get("/", (req: Request, res: Response) => {
|
app.get("/", (req: Request, res: Response) => {
|
||||||
console.log("sending index.html");
|
console.log("sending index.html");
|
||||||
res.sendFile("/dist/index.html");
|
res.sendFile("/dist/index.html");
|
||||||
@@ -43,17 +31,12 @@ app.get("/", (req: Request, res: Response) => {
|
|||||||
const routes: Router[] = Object.values(router);
|
const routes: Router[] = Object.values(router);
|
||||||
app.use("/api", routes);
|
app.use("/api", routes);
|
||||||
|
|
||||||
|
// Send index.html on root request
|
||||||
|
app.use(express.static("dist"));
|
||||||
|
app.use(express.static("public"));
|
||||||
|
|
||||||
app.listen(port);
|
app.listen(port);
|
||||||
|
|
||||||
io.on("connection", (socket) => {
|
|
||||||
console.log("Socket connected");
|
|
||||||
|
|
||||||
//Whenever someone disconnects this piece of code executed
|
|
||||||
socket.on("disconnect", () => {
|
|
||||||
console.log("Socket disconnected");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
amqp.connect(`${rabbitMQConnectionString}`, (error0, connection) => {
|
amqp.connect(`${rabbitMQConnectionString}`, (error0, connection) => {
|
||||||
if (error0) {
|
if (error0) {
|
||||||
throw error0;
|
throw error0;
|
||||||
@@ -74,7 +57,7 @@ amqp.connect(`${rabbitMQConnectionString}`, (error0, connection) => {
|
|||||||
queue,
|
queue,
|
||||||
(data) => {
|
(data) => {
|
||||||
//Socket Trigger All Clients
|
//Socket Trigger All Clients
|
||||||
io.sockets.emit("coverExtracted", JSON.parse(data.content.toString()));
|
// io.sockets.emit("coverExtracted", JSON.parse(data.content.toString()));
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
noAck: true,
|
noAck: true,
|
||||||
@@ -83,7 +66,4 @@ amqp.connect(`${rabbitMQConnectionString}`, (error0, connection) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// socket server
|
|
||||||
httpServer.listen(8051);
|
|
||||||
console.log(`Socket server is listening on 8051`);
|
|
||||||
console.log(`Server is listening on ${port}`);
|
console.log(`Server is listening on ${port}`);
|
||||||
|
|||||||
@@ -1,20 +1,44 @@
|
|||||||
import router from "../router";
|
import router from "../router";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import axios from "axios";
|
import axios, { AxiosPromise } from "axios";
|
||||||
|
|
||||||
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
|
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
|
||||||
typeof req.body === "object" ? req.body : {};
|
typeof req.body === "object" ? req.body : {};
|
||||||
const importServiceURI = process.env.DOCKER_HOST
|
const importServiceURI = process.env.DOCKER_HOST
|
||||||
? `http://${process.env.DOCKER_HOST}:3000/api/import`
|
? `http://${process.env.DOCKER_HOST}:3000/api/import`
|
||||||
: "http://localhost:3000/api/import";
|
: "http://localhost:3000/api/import";
|
||||||
await axios.request({
|
|
||||||
url: `${importServiceURI}/processAndImportToDB`,
|
const axiosArray: AxiosPromise[] = [];
|
||||||
method: "POST",
|
for (let x = 0; x < req.body.walkedFolders.length; x++) {
|
||||||
data: {
|
const newPromise = axios({
|
||||||
extractionOptions: req.body.extractionOptions,
|
method: "POST",
|
||||||
walkedFolders: req.body.walkedFolders,
|
url: `${importServiceURI}/processAndImportToDB`,
|
||||||
},
|
data: {
|
||||||
});
|
extractionOptions: req.body.extractionOptions,
|
||||||
|
walkedFolders: req.body.walkedFolders[x],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
axiosArray.push(newPromise);
|
||||||
|
}
|
||||||
|
|
||||||
|
axios
|
||||||
|
.all(axiosArray)
|
||||||
|
.then(
|
||||||
|
axios.spread((...responses) => {
|
||||||
|
responses.forEach((res) => console.log("Success"));
|
||||||
|
console.log("submitted all axios calls");
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.catch((error) => {});
|
||||||
|
|
||||||
|
// await axios.request({
|
||||||
|
// url: `${importServiceURI}/processAndImportToDB`,
|
||||||
|
// method: "POST",
|
||||||
|
// data: {
|
||||||
|
// extractionOptions: req.body.extractionOptions,
|
||||||
|
// walkedFolders: req.body.walkedFolders,
|
||||||
|
// },
|
||||||
|
// });
|
||||||
res.send({ message: "Scan and import initiated." });
|
res.send({ message: "Scan and import initiated." });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user