Actix hello world
This commit is contained in:
parent
ba901c7873
commit
0a4d05cdc8
11 changed files with 859 additions and 7 deletions
783
Cargo.lock
generated
783
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -5,13 +5,15 @@ authors = ["Antoine Gersant <antoine.gersant@lesforges.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["service-rocket"]
|
default = ["service-actix"]
|
||||||
ui = []
|
ui = []
|
||||||
profile-index = ["flame", "flamer"]
|
profile-index = ["flame", "flamer"]
|
||||||
service-actix = []
|
service-actix = ["actix-rt", "actix-web"]
|
||||||
service-rocket = ["rocket", "rocket_contrib"]
|
service-rocket = ["rocket", "rocket_contrib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
actix-web = { version = "2.0", optional = true }
|
||||||
|
actix-rt = { version = "1.0", optional = true }
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
ape = "0.2.0"
|
ape = "0.2.0"
|
||||||
app_dirs = "1.1.1"
|
app_dirs = "1.1.1"
|
||||||
|
|
|
@ -220,7 +220,7 @@ fn main() -> Result<()> {
|
||||||
.parse()
|
.parse()
|
||||||
.with_context(|| "Invalid port number")?;
|
.with_context(|| "Invalid port number")?;
|
||||||
|
|
||||||
let server = service::server::get_server(
|
service::server::run(
|
||||||
port,
|
port,
|
||||||
Some(auth_secret.as_slice()),
|
Some(auth_secret.as_slice()),
|
||||||
&api_url,
|
&api_url,
|
||||||
|
@ -231,9 +231,6 @@ fn main() -> Result<()> {
|
||||||
db.clone(),
|
db.clone(),
|
||||||
command_sender,
|
command_sender,
|
||||||
)?;
|
)?;
|
||||||
std::thread::spawn(move || {
|
|
||||||
server.launch();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Start DDNS updates
|
// Start DDNS updates
|
||||||
let db_ddns = db.clone();
|
let db_ddns = db.clone();
|
||||||
|
|
4
src/service/actix/mod.rs
Normal file
4
src/service/actix/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
pub mod server;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
32
src/service/actix/server.rs
Normal file
32
src/service/actix/server.rs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||||
|
use anyhow::*;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::db::DB;
|
||||||
|
use crate::index::CommandSender;
|
||||||
|
|
||||||
|
async fn index() -> impl Responder {
|
||||||
|
HttpResponse::Ok().body("hello world!")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[actix_rt::main]
|
||||||
|
pub async fn run(
|
||||||
|
port: u16,
|
||||||
|
auth_secret: Option<&[u8]>,
|
||||||
|
api_url: &str,
|
||||||
|
web_url: &str,
|
||||||
|
web_dir_path: &PathBuf,
|
||||||
|
swagger_url: &str,
|
||||||
|
swagger_dir_path: &PathBuf,
|
||||||
|
db: Arc<DB>,
|
||||||
|
command_sender: Arc<CommandSender>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let app = App::new();
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
.bind(format!("127.0.0.1:{}", port))?
|
||||||
|
.run();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
0
src/service/actix/tests/api.rs
Normal file
0
src/service/actix/tests/api.rs
Normal file
1
src/service/actix/tests/mod.rs
Normal file
1
src/service/actix/tests/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
|
0
src/service/actix/tests/swagger.rs
Normal file
0
src/service/actix/tests/swagger.rs
Normal file
0
src/service/actix/tests/web.rs
Normal file
0
src/service/actix/tests/web.rs
Normal file
|
@ -1,4 +1,9 @@
|
||||||
mod rocket;
|
#[cfg(feature = "service-actix")]
|
||||||
|
mod actix;
|
||||||
|
#[cfg(feature = "service-actix")]
|
||||||
|
pub use self::actix::*;
|
||||||
|
|
||||||
|
#[cfg(feature = "service-rocket")]
|
||||||
|
mod rocket;
|
||||||
#[cfg(feature = "service-rocket")]
|
#[cfg(feature = "service-rocket")]
|
||||||
pub use self::rocket::*;
|
pub use self::rocket::*;
|
||||||
|
|
|
@ -47,3 +47,31 @@ pub fn get_server(
|
||||||
StaticFiles::from(web_dir_path).rank(web_routes_rank),
|
StaticFiles::from(web_dir_path).rank(web_routes_rank),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
port: u16,
|
||||||
|
auth_secret: Option<&[u8]>,
|
||||||
|
api_url: &str,
|
||||||
|
web_url: &str,
|
||||||
|
web_dir_path: &PathBuf,
|
||||||
|
swagger_url: &str,
|
||||||
|
swagger_dir_path: &PathBuf,
|
||||||
|
db: Arc<DB>,
|
||||||
|
command_sender: Arc<CommandSender>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let server = get_server(
|
||||||
|
port,
|
||||||
|
auth_secret,
|
||||||
|
api_url,
|
||||||
|
web_url,
|
||||||
|
web_dir_path,
|
||||||
|
swagger_url,
|
||||||
|
swagger_dir_path,
|
||||||
|
db,
|
||||||
|
command_sender,
|
||||||
|
)?;
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
server.launch();
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue