Skip to content
Snippets Groups Projects
Commit 96add570 authored by Дмитрий Афанасьев's avatar Дмитрий Афанасьев
Browse files

remove http_server.lua

parent 4022c735
No related branches found
No related tags found
1 merge request!863Web-ui backend functions on rust
......@@ -99,9 +99,6 @@ fn generate_export_stubs(out_dir: &str) {
let exports = std::fs::read_to_string("src/sql/exports").unwrap();
read_symbols_into(&exports, &mut symbols);
let exports = std::fs::read_to_string("src/http_server/exports").unwrap();
read_symbols_into(&exports, &mut symbols);
let mut code = Vec::with_capacity(2048);
writeln!(code, "pub fn export_symbols() {{").unwrap();
writeln!(code, " extern \"C\" {{").unwrap();
......
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, error::Error, fmt};
use tarantool::net_box::{Conn, ConnOptions, Options};
use tarantool::proc;
use tarantool::tuple::Tuple;
use ::tarantool::net_box::{Conn, ConnOptions, Options};
use ::tarantool::tuple::Tuple;
use crate::info::VersionInfo;
use crate::instance::{GradeVariant, Instance, InstanceId};
......@@ -143,7 +142,7 @@ struct ReplicasetInfo {
//
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct TierInfo {
pub(crate) struct TierInfo {
replicasets: Vec<ReplicasetInfo>,
replicaset_count: usize,
rf: u8,
......@@ -173,7 +172,7 @@ struct TierInfo {
//
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ClusterInfo {
pub(crate) struct ClusterInfo {
capacity_usage: f64,
#[serde(rename = "currentInstaceVersion")] // for compatibility with lua version
current_instance_version: String,
......@@ -374,8 +373,8 @@ fn get_replicasets_info(
Ok(res.values().cloned().collect())
}
#[proc]
fn http_api_cluster() -> Result<ClusterInfo, Box<dyn Error>> {
//#[proc]
pub(crate) fn http_api_cluster() -> Result<ClusterInfo, Box<dyn Error>> {
let version = String::from(VersionInfo::current().picodata_version);
let storage = Clusterwide::get();
......@@ -417,8 +416,7 @@ fn http_api_cluster() -> Result<ClusterInfo, Box<dyn Error>> {
Ok(res)
}
#[proc]
fn http_api_tiers() -> Result<Vec<TierInfo>, Box<dyn Error>> {
pub(crate) fn http_api_tiers() -> Result<Vec<TierInfo>, Box<dyn Error>> {
let storage = Clusterwide::get();
let replicasets = get_replicasets_info(storage, false)?;
let tiers = get_tiers(storage)?;
......@@ -452,3 +450,34 @@ fn http_api_tiers() -> Result<Vec<TierInfo>, Box<dyn Error>> {
Ok(res.values().cloned().collect())
}
macro_rules! wrap_api_result {
($api_result:expr) => {{
let mut status = 200;
let mut content_type = "application/json";
let content: String;
match $api_result {
Ok(res) => match serde_json::to_string(&res) {
Ok(value) => content = value,
Err(err) => {
content = err.to_string();
content_type = "plain/text";
status = 500
}
},
Err(err) => {
content = err.to_string();
content_type = "plain/text";
status = 500
}
}
tlua::AsTable((
("status", status),
("body", content),
("headers", tlua::AsTable((("content-type", content_type),))),
))
}};
}
pub(crate) use wrap_api_result;
http_api_cluster
http_api_tiers
\ No newline at end of file
local json = require('json')
local function http_api_caller(fn_name)
local ok, res = pcall(
function()
return box.func[fn_name]:call({})
end
)
local http_status = 200;
local content_type = 'application/json';
if ok == false then
http_status = 500;
content_type = 'text/plain';
end
return {
status = http_status,
body = json.encode(res),
headers = {
['content-type'] = content_type,
}
}
end
local function http_api_cluster()
return http_api_caller(".http_api_cluster")
end
local function http_api_tiers()
return http_api_caller(".http_api_tiers")
end
local host, port = ...;
local httpd = require('http.server').new(host, port);
httpd:route({ method = 'GET', path = 'api/v1/tiers' }, http_api_tiers)
httpd:route({ method = 'GET', path = 'api/v1/cluster' }, http_api_cluster)
httpd:start();
_G.pico.httpd = httpd
......@@ -179,8 +179,30 @@ fn preload_http() {
fn start_http_server(Address { host, port, .. }: &Address) {
tlog!(Info, "starting http server at {host}:{port}");
let lua = ::tarantool::lua_state();
lua.exec_with(include_str!("http_server/http_server.lua"), (host, port))
.expect("failed to start http server")
lua.exec_with(
r#"
local host, port = ...;
local httpd = require('http.server').new(host, port);
httpd:start();
_G.pico.httpd = httpd
"#,
(host, port),
)
.expect("failed to start http server");
lua.exec_with(
"pico.httpd:route({method = 'GET', path = 'api/v1/tiers' }, ...)",
tlua::Function::new(|| -> _ {
http_server::wrap_api_result!(http_server::http_api_tiers())
}),
)
.expect("failed to add route api/v1/tiers to http server");
lua.exec_with(
"pico.httpd:route({method = 'GET', path = 'api/v1/cluster' }, ...)",
tlua::Function::new(|| -> _ {
http_server::wrap_api_result!(http_server::http_api_cluster())
}),
)
.expect("failed to add route api/v1/cluster to http server")
}
#[cfg(feature = "webui")]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment