From a31bfbefa74007dab9a34335bb808ef2a43965a9 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Tue, 3 Sep 2019 13:57:28 -0700 Subject: [PATCH] Start extracting web interface from implementation --- src/lib.rs | 2 +- src/web/api.rs | 58 ++++++-------------------------------------- src/web/interface.rs | 3 +-- src/web/mod.rs | 1 + src/web/util.rs | 20 +++++++++++++++ src/web_interface.rs | 37 ++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 src/web/util.rs create mode 100644 src/web_interface.rs diff --git a/src/lib.rs b/src/lib.rs index a99f9114..9c4ba3f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,8 +4,8 @@ #[macro_use] mod impl_from; -// Other modules pub mod commands; +pub mod web_interface; // This module is only public for the purpose of testing right now, and won't be // part of the first version of the Rojo API. diff --git a/src/web/api.rs b/src/web/api.rs index f8bede16..d14a0020 100644 --- a/src/web/api.rs +++ b/src/web/api.rs @@ -1,62 +1,20 @@ //! Defines Rojo's HTTP API, all under /api. These endpoints generally return //! JSON. -use std::{collections::HashSet, sync::Arc}; +use std::sync::Arc; use futures::{future, Future}; use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode}; use rbx_dom_weak::RbxId; -use serde::{Deserialize, Serialize}; -use crate::{serve_session::ServeSession, session_id::SessionId}; - -const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION"); -const PROTOCOL_VERSION: u64 = 3; - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ServerInfoResponse<'a> { - pub session_id: SessionId, - pub server_version: &'a str, - pub protocol_version: u64, - pub expected_place_ids: Option>, - // pub root_instance_id: RbxId, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ReadResponse { - pub session_id: SessionId, - // pub message_cursor: u32, - // pub instances: HashMap>, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct SubscribeResponse { - pub session_id: SessionId, - // pub message_cursor: u32, - // pub messages: Cow<'a, [InstanceChanges]>, -} - -fn response_json(value: T) -> Response { - let serialized = match serde_json::to_string(&value) { - Ok(v) => v, - Err(err) => { - return Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .header(header::CONTENT_TYPE, "text/plain") - .body(Body::from(err.to_string())) - .unwrap(); - } - }; - - Response::builder() - .header(header::CONTENT_TYPE, "application/json") - .body(Body::from(serialized)) - .unwrap() -} +use crate::{ + serve_session::ServeSession, + web::util::response_json, + web_interface::{ + ReadResponse, ServerInfoResponse, SubscribeResponse, PROTOCOL_VERSION, SERVER_VERSION, + }, +}; pub struct ApiService { serve_session: Arc, diff --git a/src/web/interface.rs b/src/web/interface.rs index 3fbf18b4..9db7c109 100644 --- a/src/web/interface.rs +++ b/src/web/interface.rs @@ -6,9 +6,8 @@ use futures::{future, Future}; use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode}; use ritz::html; -use crate::serve_session::ServeSession; +use crate::{serve_session::ServeSession, web_interface::SERVER_VERSION}; -const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION"); static HOME_CSS: &str = include_str!("../../assets/index.css"); pub struct InterfaceService { diff --git a/src/web/mod.rs b/src/web/mod.rs index a5d116fc..b1be8f7f 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -1,5 +1,6 @@ mod api; mod interface; +mod util; use std::sync::Arc; diff --git a/src/web/util.rs b/src/web/util.rs new file mode 100644 index 00000000..32581c38 --- /dev/null +++ b/src/web/util.rs @@ -0,0 +1,20 @@ +use hyper::{header::CONTENT_TYPE, Body, Response, StatusCode}; +use serde::Serialize; + +pub fn response_json(value: T) -> Response { + let serialized = match serde_json::to_string(&value) { + Ok(v) => v, + Err(err) => { + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .header(CONTENT_TYPE, "text/plain") + .body(Body::from(err.to_string())) + .unwrap(); + } + }; + + Response::builder() + .header(CONTENT_TYPE, "application/json") + .body(Body::from(serialized)) + .unwrap() +} diff --git a/src/web_interface.rs b/src/web_interface.rs new file mode 100644 index 00000000..3f40b425 --- /dev/null +++ b/src/web_interface.rs @@ -0,0 +1,37 @@ +//! Defines all the structs needed to interact with the Rojo API from an +//! automation test perspective. + +use std::collections::HashSet; + +use serde::{Deserialize, Serialize}; + +use crate::session_id::SessionId; + +pub const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const PROTOCOL_VERSION: u64 = 3; + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ServerInfoResponse<'a> { + pub session_id: SessionId, + pub server_version: &'a str, + pub protocol_version: u64, + pub expected_place_ids: Option>, + // pub root_instance_id: RbxId, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ReadResponse { + pub session_id: SessionId, + // pub message_cursor: u32, + // pub instances: HashMap>, +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct SubscribeResponse { + pub session_id: SessionId, + // pub message_cursor: u32, + // pub messages: Cow<'a, [InstanceChanges]>, +}