mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 22:25:26 +00:00
Start extracting web interface from implementation
This commit is contained in:
@@ -4,8 +4,8 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod impl_from;
|
mod impl_from;
|
||||||
|
|
||||||
// Other modules
|
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
|
pub mod web_interface;
|
||||||
|
|
||||||
// This module is only public for the purpose of testing right now, and won't be
|
// 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.
|
// part of the first version of the Rojo API.
|
||||||
|
|||||||
@@ -1,62 +1,20 @@
|
|||||||
//! Defines Rojo's HTTP API, all under /api. These endpoints generally return
|
//! Defines Rojo's HTTP API, all under /api. These endpoints generally return
|
||||||
//! JSON.
|
//! JSON.
|
||||||
|
|
||||||
use std::{collections::HashSet, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use futures::{future, Future};
|
use futures::{future, Future};
|
||||||
|
|
||||||
use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode};
|
use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode};
|
||||||
use rbx_dom_weak::RbxId;
|
use rbx_dom_weak::RbxId;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::{serve_session::ServeSession, session_id::SessionId};
|
use crate::{
|
||||||
|
serve_session::ServeSession,
|
||||||
const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
web::util::response_json,
|
||||||
const PROTOCOL_VERSION: u64 = 3;
|
web_interface::{
|
||||||
|
ReadResponse, ServerInfoResponse, SubscribeResponse, PROTOCOL_VERSION, SERVER_VERSION,
|
||||||
#[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()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct ApiService {
|
pub struct ApiService {
|
||||||
serve_session: Arc<ServeSession>,
|
serve_session: Arc<ServeSession>,
|
||||||
|
|||||||
@@ -6,9 +6,8 @@ use futures::{future, Future};
|
|||||||
use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode};
|
use hyper::{header, service::Service, Body, Method, Request, Response, StatusCode};
|
||||||
use ritz::html;
|
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");
|
static HOME_CSS: &str = include_str!("../../assets/index.css");
|
||||||
|
|
||||||
pub struct InterfaceService {
|
pub struct InterfaceService {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
mod api;
|
mod api;
|
||||||
mod interface;
|
mod interface;
|
||||||
|
mod util;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|||||||
20
src/web/util.rs
Normal file
20
src/web/util.rs
Normal 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
37
src/web_interface.rs
Normal 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]>,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user