203 lines
5.0 KiB
Rust
203 lines
5.0 KiB
Rust
#![feature(proc_macro_hygiene, decl_macro)]
|
|
#[macro_use] extern crate rocket;
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
use rocket::response::status;
|
|
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
|
|
|
|
#[derive(Serialize, Deserialize, Hash)]
|
|
pub struct OpenGitParam
|
|
{
|
|
inquirer: String,
|
|
clone_url: 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>")]
|
|
fn v1_open_inquirer_git(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<IdeState>> {
|
|
let param = OpenGitParam {
|
|
inquirer,
|
|
clone_url,
|
|
ref_name
|
|
};
|
|
|
|
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>")]
|
|
fn v1_open_inquirer_gitea(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<OpenGitParam>> {
|
|
let param = OpenGitParam {
|
|
inquirer,
|
|
clone_url,
|
|
ref_name
|
|
};
|
|
status::Accepted(Some(Json(param)))
|
|
}
|
|
|
|
#[get("/open/<inquirer>/gitlab?<clone_url>&<ref_name>")]
|
|
fn v1_open_inquirer_gitlab(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<OpenGitParam>> {
|
|
let param = OpenGitParam {
|
|
inquirer,
|
|
clone_url,
|
|
ref_name
|
|
};
|
|
status::Accepted(Some(Json(param)))
|
|
}
|
|
|
|
#[get("/open/<inquirer>/github?<clone_url>&<ref_name>")]
|
|
fn v1_open_inquirer_github(inquirer: String, clone_url: String, ref_name: String) -> status::Accepted<Json<OpenGitParam>> {
|
|
let param = OpenGitParam {
|
|
inquirer,
|
|
clone_url,
|
|
ref_name
|
|
};
|
|
status::Accepted(Some(Json(param)))
|
|
}
|
|
|
|
#[get("/open/gitlab?<clone_url>&<ref_name>")]
|
|
fn v1_open_gitlab(clone_url: String, ref_name: String) -> status::Accepted<Json<OpenGitParam>> {
|
|
let param = OpenGitParam {
|
|
inquirer : "foo".to_owned(),
|
|
clone_url,
|
|
ref_name
|
|
};
|
|
status::Accepted(Some(Json(param)))
|
|
}
|
|
|
|
use rocket::fairing::AdHoc;
|
|
|
|
fn main() {
|
|
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();
|
|
}
|