add manageservice enge concept
This commit is contained in:
@@ -4,9 +4,15 @@
|
|||||||
|
|
||||||
use rocket::response::status;
|
use rocket::response::status;
|
||||||
use rocket_contrib::json::Json;
|
use rocket_contrib::json::Json;
|
||||||
|
|
||||||
|
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
|
|
||||||
// podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh
|
// podman create --name test -p 3000:3000 -v $PWD:/nixide -w /nixide docker.io/nixos/nix nix-shell --pure start-ide.nix --run run_ide.sh
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, Deserialize, Hash)]
|
||||||
pub struct OpenGitParam
|
pub struct OpenGitParam
|
||||||
{
|
{
|
||||||
inquirer: String,
|
inquirer: String,
|
||||||
@@ -14,14 +20,124 @@ pub struct OpenGitParam
|
|||||||
ref_name: String
|
ref_name: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Hash)]
|
||||||
|
pub struct IdeState
|
||||||
|
{
|
||||||
|
ide_id: String,
|
||||||
|
state: String
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DummyEngine{}
|
||||||
|
|
||||||
|
pub trait NixIdeManageServiceEngine {
|
||||||
|
fn info(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NixIdeManageServiceEngine for DummyEngine {
|
||||||
|
fn info(&self) -> String {
|
||||||
|
"infoC".to_owned()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NixIdeManageService {
|
||||||
|
eng :Box<dyn NixIdeManageServiceEngine>
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for NixIdeManageService {}
|
||||||
|
unsafe impl Sync for NixIdeManageService {}
|
||||||
|
|
||||||
|
impl NixIdeManageService {
|
||||||
|
fn info(&self) -> String {
|
||||||
|
self.eng.info()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Hash)]
|
||||||
|
pub struct NixIdeServerParam {
|
||||||
|
#[serde(default = "default_listen_address")]
|
||||||
|
listen_address: String,
|
||||||
|
#[serde(default = "default_listen_port")]
|
||||||
|
listen_port: u32,
|
||||||
|
#[serde(default = "default_app_folder")]
|
||||||
|
app_folder: String,
|
||||||
|
#[serde(default = "default_project_folder")]
|
||||||
|
project_folder: String,
|
||||||
|
#[serde(default = "default_working_folder")]
|
||||||
|
working_folder: String
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_listen_address() -> String {
|
||||||
|
"0.0.0.0".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_listen_port() -> u32 {
|
||||||
|
3000
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_app_folder() -> String {
|
||||||
|
"_theiaideApp".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_project_folder() -> String {
|
||||||
|
"$PWD".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_working_folder() -> String {
|
||||||
|
"$PWD".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[get("/open/<inquirer>/git?<clone_url>&<ref_name>")]
|
#[get("/open/<inquirer>/git?<clone_url>&<ref_name>")]
|
||||||
fn v1_open_inquirer_git(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<OpenGitParam>> {
|
fn v1_open_inquirer_git(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<IdeState>> {
|
||||||
let param = OpenGitParam {
|
let param = OpenGitParam {
|
||||||
inquirer,
|
inquirer,
|
||||||
clone_url,
|
clone_url,
|
||||||
ref_name
|
ref_name
|
||||||
};
|
};
|
||||||
status::Accepted(Some(Json(param)))
|
|
||||||
|
let mut s = DefaultHasher::new();
|
||||||
|
param.hash(&mut s);
|
||||||
|
let hash = s.finish();
|
||||||
|
|
||||||
|
// println!("foo {:x}", hash);
|
||||||
|
// let out = Command::new("podman")
|
||||||
|
// .arg("create")
|
||||||
|
// .arg("--name")
|
||||||
|
// .arg(format!("{:x}", hash))
|
||||||
|
// .arg("-p")
|
||||||
|
// .arg("3000:3000")
|
||||||
|
// .arg("docker.io/nixos/nix")
|
||||||
|
// // .arg(format!("git clone {}", param.clone_url))
|
||||||
|
// .output()
|
||||||
|
// .expect("create container");
|
||||||
|
// String::from_utf8(out.stderr).unwrap()
|
||||||
|
|
||||||
|
status::Accepted(
|
||||||
|
Some(
|
||||||
|
Json(
|
||||||
|
IdeState {
|
||||||
|
ide_id :format!("{:x}", hash),
|
||||||
|
state : "opening".to_owned()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
use rocket::State;
|
||||||
|
#[get("/state/<ide_ide>")]
|
||||||
|
fn v1_ide_state(ide_ide: String, c : State<NixIdeManageService>) -> status::Accepted<Json<IdeState>>
|
||||||
|
{
|
||||||
|
status::Accepted(
|
||||||
|
Some(
|
||||||
|
Json(
|
||||||
|
IdeState{
|
||||||
|
ide_id: ide_ide,
|
||||||
|
state : c.info()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/open/<inquirer>/gitea?<clone_url>&<ref_name>")]
|
#[get("/open/<inquirer>/gitea?<clone_url>&<ref_name>")]
|
||||||
@@ -64,6 +180,23 @@ fn v1_open_gitlab(clone_url: String, ref_name: String) -> status::Accepted<Json<
|
|||||||
status::Accepted(Some(Json(param)))
|
status::Accepted(Some(Json(param)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use rocket::fairing::AdHoc;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite().mount("/api/v1/", routes![v1_open_inquirer_git, v1_open_inquirer_gitea, v1_open_gitlab, v1_open_inquirer_github, v1_open_inquirer_gitlab]).launch();
|
let eng = DummyEngine{};
|
||||||
|
let exectuor = NixIdeManageService{eng:Box::new(eng)};
|
||||||
|
rocket::ignite()
|
||||||
|
.mount("/api/v1/", routes![
|
||||||
|
v1_open_inquirer_git,
|
||||||
|
v1_open_inquirer_gitea,
|
||||||
|
v1_open_gitlab,
|
||||||
|
v1_open_inquirer_github,
|
||||||
|
v1_open_inquirer_gitlab,
|
||||||
|
v1_ide_state
|
||||||
|
])
|
||||||
|
.manage(exectuor)
|
||||||
|
.attach(AdHoc::on_launch("Launch Printer", |_| {
|
||||||
|
println!("Rocket is about to launch! Exciting! Here we go...");
|
||||||
|
}))
|
||||||
|
.launch();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user