refactor the main structure
This commit is contained in:
9
src/api/error.rs
Normal file
9
src/api/error.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use derive_more::{Display, Error};
|
||||||
|
|
||||||
|
impl actix_web::error::ResponseError for ReturnedError{}
|
||||||
|
|
||||||
|
#[derive(Debug, Display, Error)]
|
||||||
|
#[display(fmt = "my error: {}", name)]
|
||||||
|
pub struct ReturnedError {
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
2
src/api/mod.rs
Normal file
2
src/api/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod error;
|
||||||
|
pub mod routes;
|
||||||
@@ -1,19 +1,8 @@
|
|||||||
use actix_web::{get, patch, post, put, delete, web, App, HttpResponse, HttpServer, Responder};
|
use actix_web::{patch, put, delete, web, App, HttpServer, Responder};
|
||||||
use bollard::Docker;
|
use crate::api::error::ReturnedError;
|
||||||
use derive_more::{Display, Error};
|
|
||||||
use log::info;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use env_logger;
|
|
||||||
|
|
||||||
use crate::controller::Controller;
|
use crate::controller::Controller;
|
||||||
|
use crate::deploy::starter;
|
||||||
#[derive(Debug, Display, Error)]
|
use serde::Deserialize;
|
||||||
#[display(fmt = "my error: {}", name)]
|
|
||||||
pub struct ReturnedError {
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl actix_web::error::ResponseError for ReturnedError{}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct ConainerParams {
|
struct ConainerParams {
|
||||||
@@ -27,11 +16,6 @@ struct ConainerStopParams {
|
|||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
|
||||||
async fn hello() -> impl Responder {
|
|
||||||
HttpResponse::Ok().body("Hello world!")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[put("/container")]
|
#[put("/container")]
|
||||||
async fn create_container(controller: web::Data<Controller>,
|
async fn create_container(controller: web::Data<Controller>,
|
||||||
params: web::Query<ConainerParams>) -> impl Responder {
|
params: web::Query<ConainerParams>) -> impl Responder {
|
||||||
@@ -56,45 +40,18 @@ async fn delete_container(controller: web::Data<Controller>,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/echo")]
|
|
||||||
async fn echo(req_body: String) -> impl Responder {
|
|
||||||
HttpResponse::Ok().body(req_body)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn manual_hello() -> impl Responder {
|
|
||||||
HttpResponse::Ok().body("Hey there!")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn start() -> std::io::Result<()> {
|
pub async fn start() -> std::io::Result<()> {
|
||||||
let docker = match Docker::connect_with_local_defaults() {
|
let controller = starter::start_docker().await;
|
||||||
Ok(d) => d,
|
|
||||||
Err(e) => panic!("error:{}",e.to_string()),
|
|
||||||
};
|
|
||||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));
|
|
||||||
let controller = match Controller::new(docker,
|
|
||||||
"customnetwork".to_string(),
|
|
||||||
"172.20.0.0/24".to_string()).await {
|
|
||||||
Ok(c) => c,
|
|
||||||
Err(e) => panic!("error: {}",e),
|
|
||||||
};
|
|
||||||
controller.load_all_instances().await;
|
|
||||||
let data = web::Data::new(controller);
|
let data = web::Data::new(controller);
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(actix_web::middleware::Logger::default())
|
.wrap(actix_web::middleware::Logger::default())
|
||||||
.app_data(data.clone())
|
.app_data(data.clone())
|
||||||
.service(hello)
|
|
||||||
.service(create_container)
|
.service(create_container)
|
||||||
.service(stop_container)
|
.service(stop_container)
|
||||||
.service(delete_container)
|
.service(delete_container)
|
||||||
.service(echo)
|
|
||||||
.route("/hey", web::get().to(manual_hello))
|
|
||||||
})
|
})
|
||||||
.bind(("127.0.0.1", 8080))?
|
.bind(("127.0.0.1", 8080))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -2,8 +2,8 @@ use bollard::Docker;
|
|||||||
use bollard::errors::Error;
|
use bollard::errors::Error;
|
||||||
use crate::deploy;
|
use crate::deploy;
|
||||||
use crate::deploy::container::Container;
|
use crate::deploy::container::Container;
|
||||||
use crate::conf::storage::MemStorage;
|
use crate::database::exposer::MemStorage;
|
||||||
use crate::conf::Instance;
|
use crate::database::instance::Instance;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
@@ -140,3 +140,4 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
use rusqlite::{Connection, Result};
|
use rusqlite::{Connection, Result};
|
||||||
use fallible_iterator::FallibleIterator;
|
use fallible_iterator::FallibleIterator;
|
||||||
|
use crate::database::instance::Instance;
|
||||||
pub mod storage;
|
|
||||||
|
|
||||||
const PATH: &str = "mdeploy.db";
|
const PATH: &str = "mdeploy.db";
|
||||||
|
|
||||||
@@ -12,15 +11,6 @@ pub struct ConfServer{
|
|||||||
ip_base_limit: u8,
|
ip_base_limit: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Instance {
|
|
||||||
id: i64,
|
|
||||||
pub docker_id: String,
|
|
||||||
pub ip: String,
|
|
||||||
pub domain: String,
|
|
||||||
pub image: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct InstanceStorage {
|
pub struct InstanceStorage {
|
||||||
con: Connection,
|
con: Connection,
|
||||||
}
|
}
|
||||||
@@ -57,7 +47,7 @@ impl InstanceStorage {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_instances(&mut self) -> Result<Vec<Instance>> {
|
pub fn load_instances(&mut self) -> Result<Vec<Instance>> {
|
||||||
let mut stmt = self.con.prepare("select id, docker_id, ip, domain, image from l_instances")?;
|
let mut stmt = self.con.prepare("select id, docker_id, ip, domain, image from l_instances")?;
|
||||||
let mut rows = stmt.query([])?;
|
let mut rows = stmt.query([])?;
|
||||||
rows.map(|r| Ok(Instance{
|
rows.map(|r| Ok(Instance{
|
||||||
@@ -69,7 +59,7 @@ impl InstanceStorage {
|
|||||||
})).collect()
|
})).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_images(&mut self) -> Result<Vec<(String, i64)>> {
|
pub fn load_images(&mut self) -> Result<Vec<(String, i64)>> {
|
||||||
let mut stmt = self.con.prepare("select id, name from images")?;
|
let mut stmt = self.con.prepare("select id, name from images")?;
|
||||||
let mut rows = stmt.query([])?;
|
let mut rows = stmt.query([])?;
|
||||||
rows.map(|row| Ok((row.get::<_, String>(1)?, row.get::<_, i64>(0)?))).collect()
|
rows.map(|row| Ok((row.get::<_, String>(1)?, row.get::<_, i64>(0)?))).collect()
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::collections::hash_map::HashMap;
|
use std::collections::hash_map::HashMap;
|
||||||
use rusqlite::Error;
|
use rusqlite::Error;
|
||||||
use crate::deploy::container::Container;
|
use crate::deploy::container::Container;
|
||||||
use crate::conf::InstanceStorage;
|
use crate::database::direct_access::InstanceStorage;
|
||||||
use crate::conf::Instance;
|
use crate::database::instance::Instance;
|
||||||
|
|
||||||
pub struct MemStorage {
|
pub struct MemStorage {
|
||||||
containers: Vec<(Instance, Option<Container>)>,
|
containers: Vec<(Instance, Option<Container>)>,
|
||||||
8
src/database/instance.rs
Normal file
8
src/database/instance.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Instance {
|
||||||
|
pub id: i64,
|
||||||
|
pub docker_id: String,
|
||||||
|
pub ip: String,
|
||||||
|
pub domain: String,
|
||||||
|
pub image: i64,
|
||||||
|
}
|
||||||
3
src/database/mod.rs
Normal file
3
src/database/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod exposer;
|
||||||
|
pub mod instance;
|
||||||
|
pub(in crate::database) mod direct_access;
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod container;
|
pub mod container;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
pub mod starter;
|
||||||
mod bind;
|
mod bind;
|
||||||
|
|||||||
18
src/deploy/starter.rs
Normal file
18
src/deploy/starter.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
use bollard::Docker;
|
||||||
|
use crate::controller::Controller;
|
||||||
|
|
||||||
|
pub async fn start_docker() -> Controller{
|
||||||
|
let docker = match Docker::connect_with_local_defaults() {
|
||||||
|
Ok(d) => d,
|
||||||
|
Err(e) => panic!("error:{}",e.to_string()),
|
||||||
|
};
|
||||||
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));
|
||||||
|
let controller = match Controller::new(docker,
|
||||||
|
"customnetwork".to_string(),
|
||||||
|
"172.20.0.0/24".to_string()).await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => panic!("error: {}",e),
|
||||||
|
};
|
||||||
|
controller.load_all_instances().await;
|
||||||
|
return controller;
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
mod deploy;
|
mod deploy;
|
||||||
mod server;
|
mod api;
|
||||||
mod conf;
|
mod database;
|
||||||
mod controller;
|
mod controller;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
server::start().await;
|
api::routes::start().await;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user