///|
/// The number of characters that the request of a control call can have. The longest
/// path of Windows has 32767.
let largest_request : Int32 = 32768
///|
/// The status of the server. Another program knows Mission Control by it.
fn Site::status(self : Site) -> Reply {
Reply::json(Success, {
"app": "mission-control",
"instance": self.app.folder.name().to_json(),
"version": self.app.version.to_json(),
"views": ToJson::to_json(self.views.count()),
})
}
///|
/// Makes a link for the kind of view in `surface`. The link shows the project in
/// `project`, which can be absent.
fn Site::link(self : Site, request : Request) -> Reply raise NoRandomBytes {
guard (request.query)("surface") is Some(text) &&
surface_of(text) is Some(surface) else {
return Reply::error(BadInput, "surface must be browser or desktop.")
}
let project = match (request.query)("project") {
None => None
Some(text) => {
guard project_of_text(text) is Some(id) else {
return Reply::error(
BadInput,
"project must be the ID of a project, a number from 1.",
)
}
Some(id)
}
}
let url = self.login.issue({ surface, project, })
Reply::json(Success, { "url": Json::string(url) })
}
///|
/// Reads the name that the contract gives a kind of view.
fn surface_of(text : String) -> @app.Surface? {
Some(@json.from_json(Json::string(text))) catch {
_ => None
}
}
///|
/// Reads a project ID: 1 to 10 decimal digits.
fn project_of_text(text : String) -> @app.ProjectId? {
guard text.length() is (1..<=10) && text.iter().all(c => c is ('0'..<='9')) else {
return None
}
Some(@app.ProjectId::ProjectId(@string.parse_int(text))) catch {
_ => None
}
}
///|
/// Does the command in `command` with the JSON in `request`.
async fn Site::control_call(self : Site, request : Request) -> Reply {
guard (request.query)("command") is Some(name) &&
(request.query)("request") is Some(text) else {
return Reply::error(BadInput, "A call needs command and request.")
}
guard text.length() <= largest_request else {
return Reply::error(
TooLarge,
"The request has more than \{largest_request} characters.",
)
}
let json = @json.parse(text) catch {
_ => return Reply::error(BadInput, "The request is not JSON.")
}
try self.call(name, json) catch {
Refusal(text) => Reply::error(Refused, text)
UnknownCommand(_) =>
Reply::error(NotFound, "The server does not have this command.")
BadRequest(text) =>
Reply::error(BadInput, "The request of \{name} is wrong: \{text}")
error => raise error
} noraise {
reply => Reply::json(Success, reply)
}
}