Actix hello world

This commit is contained in:
Antoine Gersant 2020-01-13 23:56:46 -08:00
parent ba901c7873
commit 0a4d05cdc8
11 changed files with 859 additions and 7 deletions

783
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,13 +5,15 @@ authors = ["Antoine Gersant <antoine.gersant@lesforges.org>"]
edition = "2018"
[features]
default = ["service-rocket"]
default = ["service-actix"]
ui = []
profile-index = ["flame", "flamer"]
service-actix = []
service-actix = ["actix-rt", "actix-web"]
service-rocket = ["rocket", "rocket_contrib"]
[dependencies]
actix-web = { version = "2.0", optional = true }
actix-rt = { version = "1.0", optional = true }
anyhow = "1.0"
ape = "0.2.0"
app_dirs = "1.1.1"

View file

@ -220,7 +220,7 @@ fn main() -> Result<()> {
.parse()
.with_context(|| "Invalid port number")?;
let server = service::server::get_server(
service::server::run(
port,
Some(auth_secret.as_slice()),
&api_url,
@ -231,9 +231,6 @@ fn main() -> Result<()> {
db.clone(),
command_sender,
)?;
std::thread::spawn(move || {
server.launch();
});
// Start DDNS updates
let db_ddns = db.clone();

4
src/service/actix/mod.rs Normal file
View file

@ -0,0 +1,4 @@
pub mod server;
#[cfg(test)]
mod tests;

View 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(())
}

View file

View file

@ -0,0 +1 @@

View file

View file

View 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")]
pub use self::rocket::*;

View file

@ -47,3 +47,31 @@ pub fn get_server(
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(())
}