|
@@ -0,0 +1,58 @@
|
|
|
+// [dependencies]
|
|
|
+// actix-web = "2"
|
|
|
+// actix-rt = "1"
|
|
|
+// actix-files = "0.2"
|
|
|
+//
|
|
|
+// env_logger = "0.7"
|
|
|
+// serde = { version = "1.0", features = ["derive"] }
|
|
|
+// serde_json = "1.0"
|
|
|
+// bytes = "0.5"
|
|
|
+
|
|
|
+mod controllers;
|
|
|
+mod models;
|
|
|
+
|
|
|
+// #[macro_use]
|
|
|
+// extern crate actix_web;
|
|
|
+
|
|
|
+use actix_files as fs;
|
|
|
+use actix_web::http::{StatusCode};
|
|
|
+use actix_web::web::{resource as r, route, get};
|
|
|
+use actix_web::{get, guard, middleware, App, HttpResponse, HttpServer, Result};
|
|
|
+
|
|
|
+/// favicon handler
|
|
|
+#[get("/favicon")]
|
|
|
+async fn favicon() -> Result<fs::NamedFile> {
|
|
|
+ Ok(fs::NamedFile::open("static/favicon.ico")?)
|
|
|
+}
|
|
|
+
|
|
|
+/// 404 handler
|
|
|
+async fn p404() -> Result<fs::NamedFile> {
|
|
|
+ Ok(fs::NamedFile::open("static/404.html")?.set_status_code(StatusCode::NOT_FOUND))
|
|
|
+}
|
|
|
+
|
|
|
+#[actix_rt::main]
|
|
|
+pub async fn http_listener() -> std::io::Result<()> {
|
|
|
+ std::env::set_var("RUST_LOG", "actix_web=info,actix_server=info");
|
|
|
+ env_logger::init();
|
|
|
+
|
|
|
+ HttpServer::new(|| {
|
|
|
+ App::new()
|
|
|
+ .wrap(middleware::Logger::default())
|
|
|
+ .service(favicon)
|
|
|
+ .service(fs::Files::new("/static", "static").index_file("/"))
|
|
|
+ // default
|
|
|
+ .default_service(
|
|
|
+ // 404 for GET request
|
|
|
+ r("")
|
|
|
+ .route(get().to(p404))
|
|
|
+ // all requests that are not `GET`
|
|
|
+ .route(
|
|
|
+ route()
|
|
|
+ .guard(guard::Not(guard::Get()))
|
|
|
+ .to(HttpResponse::MethodNotAllowed),
|
|
|
+ ),
|
|
|
+ )
|
|
|
+ .configure(controllers::test_controller::config)
|
|
|
+ .configure(controllers::home_controller::config)
|
|
|
+ }).bind("127.0.0.1:8080")?.run().await
|
|
|
+}
|