From fe77e07b3fcd76b22f4cb50426086bb09300e9d9 Mon Sep 17 00:00:00 2001 From: Tammy Tan Date: Sat, 18 Jan 2020 00:08:42 -0500 Subject: [PATCH] checked build and added static route to server_client --- protos/output.proto | 71 ++++++++++++++++++---------------- server_client/server_client.js | 39 ++++++++++--------- webpack.dev.ts | 57 +++++++++++++-------------- 3 files changed, 87 insertions(+), 80 deletions(-) diff --git a/protos/output.proto b/protos/output.proto index 23c02ea..fa315e5 100644 --- a/protos/output.proto +++ b/protos/output.proto @@ -1,59 +1,64 @@ syntax = "proto3"; -package greet; +package calculator; -service GreetService { - //unary API - rpc Greet (GreetRequest) returns (GreetResponse) {}; - - //Server streaming API - rpc GreetManyTimes (GreetManyTimesRequest) returns ( stream GreetManyTimesResponse) {}; - - // Client Streaming - rpc LongGreet (stream LongGreetRequest) returns (LongGreetResponse) {}; +service CalculatorService { + //Unary API + rpc Sum (SumRequest) returns (SumResponse) {}; - // BiDi Streaming - rpc GreetEveryone (stream GreetEveryoneRequest) returns ( stream GreetEveryoneResponse) {}; + // Streaming API + rpc PrimeNumberDecomposition (PrimeNumberDecompositionRequest) returns ( stream PrimeNumberDecompositionResponse) {}; + rpc ComputeAverage (stream ComputeAverageRequest) returns (ComputeAverageResponse){}; -} + rpc FindMaximum (stream FindMaximumRequest) returns (stream FindMaximumResponse); - -message GreetEveryoneRequest { - Greeting greet = 1; + // error handling + // this RPC wil throw an exception if the sent number is negative: -1 + rpc SquareRoot (SquareRootRequest) returns (SquareRootResponse) {}; + } -message GreetEveryoneResponse { - string result = 1; +message SquareRootRequest { + int32 number= 1; } -message LongGreetRequest { - Greeting greet = 1; +message SquareRootResponse { + double number_root = 1; } -message LongGreetResponse { - string result = 1; +message FindMaximumRequest { + int32 number = 1; } -message GreetManyTimesRequest { - Greeting greeting = 1; +message FindMaximumResponse { + int32 maximum = 1; } -message GreetManyTimesResponse { - string result = 1; +message ComputeAverageResponse { + double average = 1; } +message ComputeAverageRequest { + int32 number = 1; +} + -message Greeting { - string first_name = 1; - string last_name = 2; +message PrimeNumberDecompositionRequest { + int32 number = 1; } -message GreetRequest { - Greeting greeting = 1; +message PrimeNumberDecompositionResponse { + int32 prime_factor = 1; +} + +message SumRequest { + int32 first_number = 1; + int32 second_number = 2; + } -message GreetResponse { - string result = 1; +message SumResponse { + int32 sum_result = 1; } \ No newline at end of file diff --git a/server_client/server_client.js b/server_client/server_client.js index 7fab65d..2bbafd1 100644 --- a/server_client/server_client.js +++ b/server_client/server_client.js @@ -12,7 +12,9 @@ app.use(cors()); app.use(bodyParser.text()); app.use(cookieParser()); -app.get("/", (req, res) => res.send("🍻 Yodelay World 🍻")); +app.use(express.static("build")); + +// app.get("/", (req, res) => res.send("🍻 Yodelay World 🍻")); // * UPLOAD: app.post("/upload", async (req, res) => { const parsedReqBody = JSON.parse(req.body); @@ -22,41 +24,40 @@ app.post("/upload", async (req, res) => { //Listens for messages app.ws("/websocket", function(ws, req) { - const grpcRequestClass = new GrpcRequestClass(ws); try { - ws.on("message", function (msg) { + ws.on("message", function(msg) { let parsedReqBody; try { parsedReqBody = JSON.parse(msg); } catch { - ws.send("message", 'error parsing JSON in ws.on message') + ws.send("message", "error parsing JSON in ws.on message"); } - if (parsedReqBody.wsCommand === 'sendInit') { - console.log('sendInit') + if (parsedReqBody.wsCommand === "sendInit") { + console.log("sendInit"); grpcRequestClass.sendInit(parsedReqBody); - } else if (parsedReqBody.wsCommand === 'push') { - console.log('push') + } else if (parsedReqBody.wsCommand === "push") { + console.log("push"); let messageInput; try { messageInput = JSON.parse(parsedReqBody.messageInput); } catch { - console.log('error parsing messageInput in ws-router - "push"') + console.log('error parsing messageInput in ws-router - "push"'); } - console.log('||||||||||||||||PUSH', messageInput) + console.log("||||||||||||||||PUSH", messageInput); grpcRequestClass._call.write(messageInput); - } else if (parsedReqBody.wsCommand === 'end') { - if(parsedReqBody.requestInput.streamType === 'serverStreaming') { - grpcRequestClass._call.cancel(); - console.log('Cancel') + } else if (parsedReqBody.wsCommand === "end") { + if (parsedReqBody.requestInput.streamType === "serverStreaming") { + grpcRequestClass._call.cancel(); + console.log("Cancel"); } else { grpcRequestClass._call.end(); - console.log('end') + console.log("end"); } } }); } catch { - console.log('error in ws') + console.log("error in ws"); } }); @@ -64,7 +65,7 @@ app.use((req, res) => { res.status(404).send("Page Not Found"); }); // Global error handling: -app.use(function (err, req, res, next) { +app.use(function(err, req, res, next) { const defaultError = { log: "Express error handler caught unknown middleware error", status: 400, @@ -80,7 +81,7 @@ app.listen(port, () => // ws.on("message", function (msg) { // const parsedReqBody = JSON.parse(msg); - + // if(parsedReqBody.wsCommand === 'sendInit'){ // grpcRequestClass.sendInit(parsedReqBody); @@ -91,6 +92,6 @@ app.listen(port, () => // } else if (parsedReqBody.wsCommand === 'end') { // if(parsedReqBody.requestInput.streamType === 'serverStreaming') { // grpcRequestClass._call.cancel(); - + // } else { // grpcRequestClass._call.end(); diff --git a/webpack.dev.ts b/webpack.dev.ts index 8126a26..c9c3b14 100644 --- a/webpack.dev.ts +++ b/webpack.dev.ts @@ -1,7 +1,6 @@ -import * as webpack from 'webpack'; -import HtmlWebPackPlugin from 'html-webpack-plugin'; -import {CleanWebpackPlugin} from 'clean-webpack-plugin'; - +import * as webpack from "webpack"; +import HtmlWebPackPlugin from "html-webpack-plugin"; +import { CleanWebpackPlugin } from "clean-webpack-plugin"; const config = (env: any): webpack.Configuration => { const API_PORT = env ? env.API_PORT : undefined; @@ -9,37 +8,39 @@ const config = (env: any): webpack.Configuration => { const API_PROTOCOL = env ? env.API_PROTOCOL : undefined; const NODE_ENV = env ? env.NODE_ENV : "development"; return { - mode: 'development', - entry: './src/index.tsx', + mode: "development", + entry: "./src/index.tsx", output: { - filename: 'bundle.js', - publicPath: '/' + filename: "bundle.js", + publicPath: "/" }, resolve: { - extensions: ['.ts', '.tsx', '.jsx', '.js', '.json', '.css', '.scss'] + extensions: [".ts", ".tsx", ".jsx", ".js", ".json", ".css", ".scss"] }, - devtool: 'source-map', + devtool: "source-map", module: { rules: [ { test: /\.tsx?$/, exclude: /node_modules/, - loader: 'awesome-typescript-loader' + loader: "awesome-typescript-loader" }, { test: /\.(s*)css$/i, - use: ['style-loader', 'css-loader', 'sass-loader'] + use: ["style-loader", "css-loader", "sass-loader"] }, { test: /\.(png|svg|jpg|gif)$/, - use: [{ - loader: 'url-loader', - options: { - limit: 8000, - name: './src/scss/[hash]-[name].[ext]' + use: [ + { + loader: "url-loader", + options: { + limit: 8000, + name: "./src/scss/[hash]-[name].[ext]" + } } - }] - }, + ] + } ] }, // @ts-ignore @@ -53,21 +54,21 @@ const config = (env: any): webpack.Configuration => { new CleanWebpackPlugin(), new HtmlWebPackPlugin({ - template: './src/index.html' + template: "./src/index.html" }), new webpack.DefinePlugin({ - 'env.API_PORT': API_PORT + "env.API_PORT": API_PORT ? JSON.stringify(API_PORT) - : JSON.stringify('8000'), - 'env.API_HOST': API_HOST + : JSON.stringify("8000"), + "env.API_HOST": API_HOST ? JSON.stringify(API_HOST) - : JSON.stringify('localhost'), - 'env.API_PROTOCOL': API_PROTOCOL + : JSON.stringify("localhost"), + "env.API_PROTOCOL": API_PROTOCOL ? JSON.stringify(API_PROTOCOL) - : JSON.stringify('http'), - 'env.NODE_ENV': NODE_ENV + : JSON.stringify("http"), + "env.NODE_ENV": NODE_ENV ? JSON.stringify(NODE_ENV) - : JSON.stringify('development') + : JSON.stringify("development") }), new webpack.HotModuleReplacementPlugin() ]