Add server command

This commit is contained in:
Jett Chen 2024-11-22 15:25:16 +08:00
parent d1a223fac9
commit 6c32ae8b68
7 changed files with 77 additions and 6 deletions

37
Cargo.lock generated
View file

@ -3309,6 +3309,7 @@ dependencies = [
"tokio",
"tonic 0.12.3",
"tonic-build",
"tonic-web",
]
[[package]]
@ -3799,6 +3800,26 @@ dependencies = [
"syn 2.0.89",
]
[[package]]
name = "tonic-web"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5299dd20801ad736dccb4a5ea0da7376e59cd98f213bf1c3d478cf53f4834b58"
dependencies = [
"base64 0.22.1",
"bytes",
"http 1.1.0",
"http-body 1.0.1",
"http-body-util",
"pin-project",
"tokio-stream",
"tonic 0.12.3",
"tower-http",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower"
version = "0.4.13"
@ -3819,6 +3840,22 @@ dependencies = [
"tracing",
]
[[package]]
name = "tower-http"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
dependencies = [
"bitflags 2.6.0",
"bytes",
"http 1.1.0",
"http-body 1.0.1",
"http-body-util",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]]
name = "tower-layer"
version = "0.3.3"

View file

@ -12,6 +12,7 @@ service BurrowWeb {
rpc CreateDevice (CreateDeviceRequest) returns (CreateDeviceResponse);
rpc DeleteDevice (JWTInfo) returns (Empty);
rpc ListDevices (JWTInfo) returns (ListDevicesResponse);
rpc Status(Empty) returns (ServerStatus);
}
message Peer {
@ -88,3 +89,7 @@ message CreateDeviceResponse {
message ListDevicesResponse {
repeated Device devices = 1;
}
message ServerStatus {
string status = 1;
}

View file

@ -31,6 +31,7 @@ dotenvy = "0.15.7"
config = "0.14.1"
prost = "0.13.3"
prost-types = "0.13.3"
tonic-web = "0.12.3"
[features]

BIN
server/server.sqlite3 Normal file

Binary file not shown.

View file

@ -2,7 +2,7 @@ pub mod client;
pub mod server;
use anyhow::Result;
use clap::{Args, Parser, Subcommand};
use server::providers::gen_keypem;
use server::{providers::gen_keypem, serve};
#[derive(Parser)]
#[command(name = "Burrow Server")]
@ -44,7 +44,9 @@ async fn main() -> Result<()> {
println!("Generated PEM:\n{pem}")
}
}
Commands::StartServer => todo!(),
Commands::StartServer => {
serve().await?;
}
};
Ok(())
}

View file

@ -4,21 +4,32 @@ use jwt_simple::prelude::Ed25519KeyPair;
use tonic::{Request, Response, Status};
use super::providers::{KeypairT, OpenIdUser};
use std::fmt::Debug;
use super::{
grpc_defs::{
burrowwebrpc::burrow_web_server::BurrowWeb, CreateDeviceRequest, CreateDeviceResponse,
Empty, JwtInfo, ListDevicesResponse, SlackAuthRequest,
Empty, JwtInfo, ListDevicesResponse, ServerStatus, SlackAuthRequest,
},
providers::slack::auth,
settings::BurrowAuthServerConfig,
};
struct BurrowGrpcServer {
#[derive(Clone)]
pub struct BurrowGrpcServer {
config: Arc<BurrowAuthServerConfig>,
jwt_keypair: Arc<KeypairT>,
}
impl Debug for BurrowGrpcServer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BurrowGrpcServer")
.field("config", &self.config)
.field("jwt_keypair", &"<redacted>")
.finish()
}
}
impl BurrowGrpcServer {
pub fn new() -> anyhow::Result<Self> {
let config = BurrowAuthServerConfig::new_dotenv()?;
@ -62,4 +73,10 @@ impl BurrowWeb for BurrowGrpcServer {
) -> Result<Response<ListDevicesResponse>, Status> {
todo!()
}
async fn status(&self, _req: Request<Empty>) -> Result<Response<ServerStatus>, Status> {
Ok(Response::new(ServerStatus {
status: "Nobody expects the Spanish Inquisition".into(),
}))
}
}

View file

@ -5,13 +5,22 @@ pub mod providers;
pub mod settings;
use anyhow::Result;
use providers::slack::auth;
use grpc_defs::burrow_web_server::BurrowWebServer;
use grpc_server::BurrowGrpcServer;
use tokio::signal;
use tonic::transport::Server;
pub async fn serve() -> Result<()> {
db::init_db()?;
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
let addr = "[::1]:8080".parse()?;
log::info!("Starting auth server on port 8080");
let burrow_grpc_server = BurrowGrpcServer::new()?;
let svc = BurrowWebServer::new(burrow_grpc_server);
Server::builder()
.accept_http1(true)
.add_service(tonic_web::enable(svc))
.serve(addr)
.await?;
Ok(())
}