code from issues #3 and #4 added. Thank you @tomus85

pull/8/head
Claus Matzinger 2020-04-25 13:56:51 +02:00
rodzic ba2138f77d
commit 562a88e956
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7B183BCF2DBEC988
4 zmienionych plików z 26 dodań i 23 usunięć

Wyświetl plik

@ -8,6 +8,5 @@ edition = "2018"
[dependencies]
actix-web = "1"
serde = "1"
serde_derive = "1"
serde = { version = "1.0", features = ["derive"] }
env_logger = "0.6"

Wyświetl plik

@ -4,7 +4,7 @@ extern crate actix_web;
use actix_web::{
guard, http::Method, middleware, web, App, HttpResponse, HttpServer,
};
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use std::env;
#[derive(Debug, Clone, Serialize, Deserialize)]

Wyświetl plik

@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "1"
actix-web = "2"
env_logger = "0.6"
actix-files = "0"
actix-files = "0"
actix-rt = "1"

Wyświetl plik

@ -3,36 +3,39 @@ extern crate actix_web;
use actix_web::{web, App, middleware, HttpServer, Responder, Result};
use std::{env};
use actix_files as fs;
use actix_files::NamedFile;
fn index() -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open("static/index.html")?)
async fn index() -> Result<NamedFile> {
Ok(NamedFile::open("static/index.html")?)
}
fn path_parser(info: web::Path<(String, i32)>) -> impl Responder {
async fn path_parser(info: web::Path<(String, i32)>) -> impl Responder {
format!("You tried to reach '{}/{}'", info.0, info.1)
}
fn rust_cookbook() -> impl Responder {
async fn rust_cookbook() -> impl Responder {
format!("Welcome to the Rust Cookbook")
}
#[get("/foxes")]
fn foxes() -> Result<fs::NamedFile> {
Ok(fs::NamedFile::open("static/foxes.jpg")?)
async fn foxes() -> Result<NamedFile> {
Ok(NamedFile::open("static/foxes.jpg")?)
}
fn main() -> std::io::Result<()> {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init();
HttpServer::new(
|| App::new()
.wrap(middleware::Logger::default())
.service(foxes)
.service(web::resource("/").to(index))
.service(web::resource("/welcome").to(rust_cookbook))
.service(web::resource("/{path}/{id}").to(path_parser)))
.bind("127.0.0.1:8081")?
.run()
}
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.route("/", web::get().to(index))
.route("/welcome", web::get().to(rust_cookbook))
.route("/{path}/{id}", web::get().to(path_parser))
.service(foxes)
})
.bind("127.0.0.1:8088")?
.run()
.await
}