Skip to content

Commit

Permalink
checked build and added static route to server_client
Browse files Browse the repository at this point in the history
  • Loading branch information
Tammy Tan authored and Tammy Tan committed Jan 18, 2020
1 parent 25c1770 commit fe77e07
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 80 deletions.
71 changes: 38 additions & 33 deletions protos/output.proto
Original file line number Diff line number Diff line change
@@ -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;
}
39 changes: 20 additions & 19 deletions server_client/server_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -22,49 +24,48 @@ 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");
}
});

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,
Expand All @@ -80,7 +81,7 @@ app.listen(port, () =>

// ws.on("message", function (msg) {
// const parsedReqBody = JSON.parse(msg);

// if(parsedReqBody.wsCommand === 'sendInit'){
// grpcRequestClass.sendInit(parsedReqBody);

Expand All @@ -91,6 +92,6 @@ app.listen(port, () =>
// } else if (parsedReqBody.wsCommand === 'end') {
// if(parsedReqBody.requestInput.streamType === 'serverStreaming') {
// grpcRequestClass._call.cancel();

// } else {
// grpcRequestClass._call.end();
57 changes: 29 additions & 28 deletions webpack.dev.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
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;
const API_HOST = env ? env.API_HOST : undefined;
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
Expand All @@ -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()
]
Expand Down

0 comments on commit fe77e07

Please sign in to comment.