Skip to content

Commit 6633d3e

Browse files
committedFeb 13, 2022
Initial commit, with minimal gRPC server
0 parents  commit 6633d3e

File tree

17 files changed

+1178
-0
lines changed

17 files changed

+1178
-0
lines changed
 

‎.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[*]
2+
end_of_line = lf
3+
insert_final_newline = true
4+
trim_trailing_whitespace = true
5+
6+
[*.rs]
7+
tab_width = 4
8+
9+
[*.{js,jsx,ts,tsx,html,css,svelte,proto}]
10+
tab_width = 2

‎.github/workflows/ci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
rustfmt:
7+
name: Rust format
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
12+
- uses: actions-rs/toolchain@v1
13+
with:
14+
profile: minimal
15+
toolchain: nightly
16+
components: rustfmt
17+
18+
- run: cargo fmt -- --check
19+
20+
rust:
21+
name: Rust lint and test
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v2
25+
26+
- uses: actions-rs/toolchain@v1
27+
with:
28+
toolchain: stable
29+
30+
- run: cargo clippy --all-targets -- -D warnings
31+
32+
- run: cargo test

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
.vscode/

‎Cargo.lock

+973
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[workspace]
2+
members = ["crates/*"]

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# sshx
2+
3+
Visit [sshx.io](https://sshx.io) to learn more about this tool.

‎crates/sshx-core/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "sshx-core"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
prost = "0.9.0"
8+
tonic = "0.6.2"
9+
10+
[build-dependencies]
11+
tonic-build = "0.6.2"

‎crates/sshx-core/build.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::{env, path::PathBuf};
2+
3+
fn main() -> Result<(), Box<dyn std::error::Error>> {
4+
let descriptor_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("sshx.bin");
5+
tonic_build::configure()
6+
.file_descriptor_set_path(&descriptor_path)
7+
.format(true)
8+
.compile(&["proto/sshx.proto"], &["proto/"])?;
9+
Ok(())
10+
}

‎crates/sshx-core/proto/sshx.proto

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
syntax = "proto3";
2+
package sshx;
3+
4+
service Greeter {
5+
// Our SayHello rpc accepts HelloRequests and returns HelloReplies
6+
rpc SayHello(HelloRequest) returns (HelloReply);
7+
}
8+
9+
// A request for the SayHello rpc.
10+
message HelloRequest {
11+
// Request message contains the name to be greeted
12+
string name = 1;
13+
}
14+
15+
// A reply for the SayHello rpc.
16+
message HelloReply {
17+
// Reply contains the greeting message
18+
string message = 1;
19+
}

‎crates/sshx-core/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! The core crate for shared code used in the sshx application.
2+
3+
#![forbid(unsafe_code)]
4+
#![warn(missing_docs)]
5+
6+
/// Protocol buffer and gRPC definitions, automatically generated by Tonic.
7+
pub mod proto {
8+
tonic::include_proto!("sshx");
9+
10+
/// File descriptor set used for gRPC reflection.
11+
pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("sshx");
12+
}

‎crates/sshx-server/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "sshx-server"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0.53"
8+
sshx-core = { path = "../sshx-core" }
9+
tokio = { version = "1.16.1", features = ["full"] }
10+
tonic = "0.6.2"
11+
tonic-reflection = "0.3.0"

‎crates/sshx-server/src/grpc.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Defines gRPC routes and application request logic.
2+
3+
use sshx_core::proto::{greeter_server::Greeter, HelloReply, HelloRequest};
4+
use tonic::{Request, Response, Status};
5+
6+
/// Server that handles gRPC requests from the sshx command-line client.
7+
pub struct GrpcServer;
8+
9+
#[tonic::async_trait]
10+
impl Greeter for GrpcServer {
11+
async fn say_hello(
12+
&self,
13+
request: Request<HelloRequest>,
14+
) -> Result<Response<HelloReply>, Status> {
15+
println!("Got a request: {:?}", request);
16+
17+
let reply = HelloReply {
18+
message: format!("Hello {}!", request.get_ref().name),
19+
};
20+
21+
Ok(Response::new(reply))
22+
}
23+
}

‎crates/sshx-server/src/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! The sshx server, which coordinates terminal sharing.
2+
//!
3+
//! Requests are communicated to the server via gRPC (for command-line sharing
4+
//! clients) and WebSocket connections (for web listeners). The server is built
5+
//! using a hybrid Hyper service, split between a Tonic gRPC handler and an Axum
6+
//! web listener.
7+
//!
8+
//! Most web requests are routed directly to static files located in the `dist/`
9+
//! folder relative to where this binary is running, allowing the frontend to be
10+
//! separately developed from the server.
11+
12+
#![forbid(unsafe_code)]
13+
#![warn(missing_docs)]
14+
15+
pub mod grpc;
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use tonic::Request;
20+
21+
#[tokio::test]
22+
async fn test_rpc() -> Result<(), Box<dyn std::error::Error>> {
23+
use sshx_core::proto::*;
24+
25+
let req = Request::new(HelloRequest {
26+
name: "adam".into(),
27+
});
28+
let _ = req;
29+
// let mut client = greeter_client::GreeterClient::connect("http://[::1]:8051").await?;
30+
// let resp = client.say_hello(req).await?;
31+
// println!("resp={:?}", resp);
32+
Ok(())
33+
}
34+
}

‎crates/sshx-server/src/main.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use sshx_core::proto::{greeter_server::GreeterServer, FILE_DESCRIPTOR_SET};
2+
use sshx_server::grpc::GrpcServer;
3+
use tonic::transport::Server;
4+
5+
#[tokio::main]
6+
async fn main() -> anyhow::Result<()> {
7+
let grpc_service = GreeterServer::new(GrpcServer);
8+
let reflection_service = tonic_reflection::server::Builder::configure()
9+
.register_encoded_file_descriptor_set(FILE_DESCRIPTOR_SET)
10+
.build()
11+
.unwrap();
12+
13+
let addr = "[::1]:8051".parse()?;
14+
Server::builder()
15+
.add_service(grpc_service)
16+
.add_service(reflection_service)
17+
.serve(addr)
18+
.await?;
19+
Ok(())
20+
}

‎crates/sshx/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "sshx"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sshx-core = { path = "../sshx-core" }

‎crates/sshx/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

‎rustfmt.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
unstable_features = true
2+
group_imports = "StdExternalCrate"
3+
wrap_comments = true
4+
format_strings = true
5+
normalize_comments = true
6+
reorder_impl_items = true

0 commit comments

Comments
 (0)
Please sign in to comment.