Start extracting web interface from implementation

This commit is contained in:
Lucien Greathouse
2019-09-03 13:57:28 -07:00
parent f1729163cf
commit a31bfbefa7
6 changed files with 68 additions and 53 deletions

View File

@@ -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.

View File

@@ -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<HashSet<u64>>,
// 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<RbxId, InstanceWithMetadata<'a>>,
}
#[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<T: serde::Serialize>(value: T) -> Response<Body> {
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<ServeSession>,

View File

@@ -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 {

View File

@@ -1,5 +1,6 @@
mod api;
mod interface;
mod util;
use std::sync::Arc;

20
src/web/util.rs Normal file
View File

@@ -0,0 +1,20 @@
use hyper::{header::CONTENT_TYPE, Body, Response, StatusCode};
use serde::Serialize;
pub fn response_json<T: Serialize>(value: T) -> Response<Body> {
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()
}

37
src/web_interface.rs Normal file
View File

@@ -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<HashSet<u64>>,
// 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<RbxId, InstanceWithMetadata<'a>>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubscribeResponse {
pub session_id: SessionId,
// pub message_cursor: u32,
// pub messages: Cow<'a, [InstanceChanges]>,
}