mirror of
https://github.com/balkian/experiments.git
synced 2025-09-02 20:02:20 +00:00
first commit
This commit is contained in:
20
rust/actix/src/main-old.rs
Normal file
20
rust/actix/src/main-old.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use futures::future::Future;
|
||||
use actix_service::Service;
|
||||
use actix_web::{web, App};
|
||||
use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
||||
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.wrap_fn(|req, srv|
|
||||
srv.call(req).map(|mut res| {
|
||||
res.headers_mut().insert(
|
||||
CONTENT_TYPE, HeaderValue::from_static("text/plain"),
|
||||
);
|
||||
res
|
||||
}))
|
||||
.route(
|
||||
"/index.html",
|
||||
web::get().to(|| "Hello, middleware!"),
|
||||
);
|
||||
}
|
45
rust/actix/src/main.rs
Normal file
45
rust/actix/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use futures::future::Future;
|
||||
use actix_service::{Service};
|
||||
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
|
||||
use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
|
||||
|
||||
|
||||
fn greet(req: HttpRequest) -> impl Responder {
|
||||
let name = req.match_info().get("name").unwrap_or("World");
|
||||
format!("Hello {}!", &name)
|
||||
|
||||
}
|
||||
|
||||
fn p404(req: HttpRequest) -> impl Responder {
|
||||
println!("{:?}", req);
|
||||
actix_web::HttpResponse::NotFound()
|
||||
.content_type("text/plain")
|
||||
.body("Thou shall not pass")
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(greet))
|
||||
.route("/{name}", web::get().to(greet))
|
||||
.default_service(
|
||||
web::route().to(p404))
|
||||
.wrap_fn(|req, srv| {
|
||||
println!("{:?}", req);
|
||||
srv.call(req).map(|mut res| {
|
||||
res.headers_mut().insert(
|
||||
CONTENT_TYPE,
|
||||
HeaderValue::from_static("0001"),
|
||||
);
|
||||
res
|
||||
})
|
||||
})
|
||||
})
|
||||
.bind("127.0.0.1:8000")
|
||||
.expect("Can not bind to port 8000")
|
||||
.run()
|
||||
.unwrap();
|
||||
|
||||
}
|
Reference in New Issue
Block a user