当前位置: 首页 > news >正文

网站建设费用资本化湖北搜索引擎优化

网站建设费用资本化,湖北搜索引擎优化,房屋设计平面图效果图,深圳优化公司义高粱seoActix-web 学习 请求处理分为两个阶段。 首先调用处理程序对象,返回实现Responder trait的任何对象。 然后,在返回的对象上调用respond_to(),将其自身转换为AsyncResult或Error。 默认情况下,actix为一些标准类型提供响应器实现&…

Actix-web 学习

请求处理分为两个阶段。
首先调用处理程序对象,返回实现Responder trait的任何对象。
然后,在返回的对象上调用respond_to(),将其自身转换为AsyncResult或Error。
默认情况下,actix为一些标准类型提供响应器实现,比如静态str、字符串等。要查看完整的实现列表,请查看响应器文档。

基础的Demo

extern crate actix_web;
extern crate futures;use actix_web::{server, App, HttpRequest, Responder, HttpResponse, Body};
use std::cell::Cell;
use std::io::Error;
use futures::future::{Future, result};
use futures::stream::once;
use bytes::Bytes;struct AppState {counter: Cell<usize>,
}// 有状态
fn index2(req: &HttpRequest<AppState>) -> String {let count = req.state().counter.get() + 1; // <- get countreq.state().counter.set(count); // <- store new count in stateformat!("Request number: {}", count) // <- response with count
}// 静态文本
fn index1(_req: &HttpRequest) -> impl Responder {"Hello world!"
}fn index11(_req: &HttpRequest) -> &'static str {"Hello world!"
}// 异步内容
fn index12(req: &HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {result(Ok(HttpResponse::Ok().content_type("text/html").body(format!("Hello!")))).responder()
}fn index13(req: &HttpRequest) -> Box<Future<Item=&'static str, Error=Error>> {result(Ok("Welcome!")).responder()
}fn index14(req: &HttpRequest) -> HttpResponse {let body = once(Ok(Bytes::from_static(b"test")));HttpResponse::Ok().content_type("application/json").body(Body::Streaming(Box::new(body)))
}// 文件
// fn f1(req: &HttpRequest) -> io::Result<fs::NamedFile> {
//     Ok(fs::NamedFile::open("static/index.html")?)
// }fn main() {let addr = "0.0.0.0:8888";let s = server::new(|| {vec![App::new().prefix("/app1").resource("/", |r| r.f(index1)).resource("/2", |r| r.f(index11)).resource("/3", |r| r.f(index12)).resource("/4", |r| r.f(index13)).resource("/5", |r| r.f(index14)).boxed(),App::with_state(AppState { counter: Cell::new(0) }).prefix("/app2").resource("/", |r| r.f(index2)).boxed(),]}).bind(addr).unwrap();println!("server run: {}", addr);s.run();
}

请求内容解析

extern crate actix_web;
extern crate futures;
#[macro_use]
extern crate serde_derive;use actix_web::*;
use std::cell::Cell;
use std::io::Error;
use futures::future::{Future, result};
use futures::stream::once;
use bytes::Bytes;// url 解析
#[derive(Debug, Serialize, Deserialize)]
struct Info {userid: u32,friend: String,
}fn index(info: Path<Info>) -> String {format!("Welcome {}!", info.friend)
}fn index2(info: Path<(u32, String)>) -> Result<String> {Ok(format!("Welcome {}! {}", info.1, info.0))
}// url 参数解析
fn index3(info: Query<Info>) -> String {format!("Welcome {} {}", info.userid, info.friend)
}// body json 解析
fn index4(info: Json<Info>) -> Result<String> {Ok(format!("Welcome {} {}", info.userid, info.friend))
}// body form 解析
fn index5(form: Form<Info>) -> Result<String> {Ok(format!("Welcome {}!", form.userid))
}// json 手动解析1
fn index6(req: &HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {req.json().from_err().and_then(|val: Info| {println!("model: {:?}", val);Ok(HttpResponse::Ok().json(val))  // <- send response}).responder()
}// Json手动解析2, 手动处理body
fn index7(req: &HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {// `concat2` will asynchronously read each chunk of the request body and// return a single, concatenated, chunkreq.concat2()// `Future::from_err` acts like `?` in that it coerces the error type from// the future into the final error type.from_err()// `Future::and_then` can be used to merge an asynchronous workflow with a// synchronous workflow.and_then(|body| {let obj = serde_json::from_slice::<Info>(&body)?;Ok(HttpResponse::Ok().json(obj))}).responder()
}// multipart
//fn index8(req: &HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {get multipart and iterate over multipart items
//    req.multipart()
//        .and_then(|item| {
//            match item {
//                multipart::MultipartItem::Field(field) => {
//                    println!("==== FIELD ==== {:?} {:?}",
//                             field.headers(),
//                             field.content_type());
//                    Either::A(
//                        field.map(|chunk| {
//                            println!("-- CHUNK: \n{}",
//                                     std::str::from_utf8(&chunk).unwrap());
//                        })
//                            .fold((), |_, _| result(Ok(()))))
//                }
//                multipart::MultipartItem::Nested(mp) => {
//                    Either::B(result(Ok(())))
//                }
//            }
//        })
//        ...
//    // https://github.com/actix/examples/tree/master/multipart/
//}// stream
//fn index9(req: &HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
//    req
//        .payload()
//        .from_err()
//        .fold((), |_, chunk| {
//            println!("Chunk: {:?}", chunk);
//            result::<_, error::PayloadError>(Ok(()))
//        })
//        .map(|_| HttpResponse::Ok().finish())
//        .responder()
//}// 通用解析
// fn index(req: &HttpRequest) -> HttpResponse {
// 	let params = Path::<(String, String)>::extract(req);
// 	let info = Json::<MyInfo>::extract(req); // 	...
// }fn main() {let addr = "0.0.0.0:8888";let s = server::new(|| {vec![App::new().prefix("/args").resource("/1/{userid}/{friend}", |r| r.method(http::Method::GET).with(index)).resource("/2/{userid}/{friend}", |r| r.with(index2)).route("/3", http::Method::GET, index3).route("/4", http::Method::POST, index4).route("/5", http::Method::POST, index5).boxed(),]}).bind(addr).unwrap();println!("server run: {}", addr);s.run();
}

Static

extern crate actix_web;use actix_web::{App};
use actix_web::fs::{StaticFileConfig, StaticFiles};#[derive(Default)]
struct MyConfig;impl StaticFileConfig for MyConfig {fn is_use_etag() -> bool {false}fn is_use_last_modifier() -> bool {false}
}fn main() {App::new().handler("/static",StaticFiles::with_config(".", MyConfig).unwrap().show_files_listing()).finish();
}

Websocket

use actix::*;
use actix_web::*;/// Define http actor
struct Ws;impl Actor for Ws {type Context = ws::WebsocketContext<Self>;
}/// Handler for ws::Message message
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {match msg {ws::Message::Ping(msg) => ctx.pong(&msg),ws::Message::Text(text) => ctx.text(text),ws::Message::Binary(bin) => ctx.binary(bin),_ => (),}}
}fn main() {App::new().resource("/ws/", |r| r.f(|req| ws::start(req, Ws))).finish();
}

middleware

用到了再说
https://actix.rs/docs/middleware/

http://www.hkea.cn/news/592151/

相关文章:

  • 企业网站制作查询百度电话怎么转人工
  • 杭州专业网站建设怎样创建网站
  • 网站建设报价表格式淘宝关键词优化技巧
  • 高端网站建设系统百度网盘登录入口官网
  • ps做网站顶部江苏网络推广公司
  • 源码做网站手机网站百度关键词排名
  • 网站关键词分隔网站链接提交
  • 福永营销型网站多少钱中国最新消息今天
  • 做网站4000-262-263网站排名优化软件有哪些
  • 网站双链接怎么做网络舆情监测平台
  • 企业网站建设制作百度网盘下载app
  • asp做一个简单网站网络营销就是seo正确吗
  • 移动wap站点公司网站设计图
  • 网站策划建设seo搜索排名影响因素主要有
  • 大型商业广场网站建设互联网推广方案怎么写
  • p2vr做的网站上传网络广告策划书范文
  • 2022年大连黄页优化搜索引擎营销
  • 宁波有几个区昭通网站seo
  • 建设企业网站方案网站优化软件哪个好
  • 郑州做网站要搜索引擎最新排名
  • wordpress建好站了打不开首页成都关键词优化排名
  • 京东网站开发需求如何做谷歌优化
  • 微信app开发诊断网站seo现状的方法
  • 做旅行网站网站seo优化多少钱
  • 上海专业网站建设咨询网络销售怎么样
  • 奶茶网页设计图片湖南seo网站多少钱
  • 家里电脑做网站服务器如何建立网址
  • 临西做网站哪里便宜seo专业培训课程
  • 高端网站设计报价表个人网上卖货的平台
  • 广州网站优化推广公司网站优化排名资源