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

企业建站域名十大it教育培训机构排名

企业建站域名,十大it教育培训机构排名,sns社交网站,长春企业网站哪里做的好本博客是在杨旭老师的 rust web 全栈教程项目基础上进行修改,支持了图片资源返回,杨旭老师的rust web链接如下: https://www.bilibili.com/video/BV1RP4y1G7KFp1&vd_source8595fbbf160cc11a0cc07cadacf22951 本人默认读者已经学习了相关…

本博客是在杨旭老师的 rust web 全栈教程项目基础上进行修改,支持了图片资源返回,杨旭老师的rust web链接如下:

https://www.bilibili.com/video/BV1RP4y1G7KFp=1&vd_source=8595fbbf160cc11a0cc07cadacf22951

本人默认读者已经学习了相关基础教程和杨旭老师的相关课程,直接开始针对项目里面对应文件进行修改说明。

一、新增加载图片资源方法

在原先项目加载文件方法同一层,新增支持加载图片资源如下。

pub trait Handler{fn handle(res:&HttpRequest) ->HttpResponse;fn load_file(file_name:&str) ->Option<Vec<u8>>{println!("load_file file_name={}",file_name);let default_path = format!("{}/public",env!("CARGO_MANIFEST_DIR"));println!("load_file default_path={}",default_path);let public_path = env::var("PUBLIC_PATH").unwrap_or(default_path);let full_path = format!("{}/{}",public_path,file_name);println!("load_file full_path={}",full_path);let contents = fs::read(full_path);contents.ok()}fn load_image(image_name:&str) ->Option<Vec<u8>>{let default_path = format!("{}/public",env!("CARGO_MANIFEST_DIR"));let public_path = env::var("PUBLIC_PATH").unwrap_or(default_path);let full_path = format!("{}/{}/{}",public_path,"image",image_name);println!("load_image full_path={}",full_path);let contents: Result<Vec<u8>, std::io::Error> = fs::read(full_path);contents.ok()}
}

load_image 方法就是本博客新增方法,根据入参图片名称,去指定public目录下image文件夹下读取图片文件,返回Vec<u8> 字节流。

我们在旧的新增了图片处理分支如下

impl Handler for StaticPageHandler{fn handle(req:&HttpRequest) ->HttpResponse{let http::httprequest::Resource::Path(s) =&req.resourece;let route:Vec<&str> = s.split("/").collect();println!("route={}",route[1]);if route[1]=="image" {match Self::load_image(route[2]){Some(content) => {let mut map:HashMap<&str,&str> = HashMap::new();map.insert("Content-Type","image/webp");println!("读取到文件长度{}",content.len());return HttpResponse::new("200",Some(map),Some(content));},None => {return HttpResponse::new("404",None,Self::load_file("404.html"))}}}match route[1] {"" =>HttpResponse::new("200",None,Self::load_file("index.html")),"health" =>HttpResponse::new("200",None,Self::load_file("health.html")),path => match Self::load_file(path) {Some(contents) =>{let mut map:HashMap<&str,&str> = HashMap::new();if path.ends_with(".css") {map.insert("Content-Type","text/css");}else if path.ends_with(".js") {map.insert("Content-Type","text/javascript");}else {map.insert("Content-Type","text/html");}HttpResponse::new("200",Some(map),Some(contents))},None => HttpResponse::new("404",None,Self::load_file("404.html"))}}}
}

特别说明点根据加载图片类型不同,map.insert("Content-Type","image/webp");

这里的Content-Type也要对应修改,我这里返回的文件是webp格式。

二、修改HttpResponse 结构体body数据类型

旧项目中由于返回的都是文本内容,body数据类型定义成字符串切片,由于这次改动读取图片是字节流,所以这里的body也需要变更成Option<Vec<u8>>类型

#[derive(Debug,PartialEq,Clone)]
pub struct HttpResponse<'a>{version:&'a str,status_code:&'a str,status_text:&'a str,header:Option<HashMap<&'a str,&'a str>>,body:Option<Vec<u8>>,
}

三、修改HttpResponse 响应方法

上面已经提到了,旧项目返回的是文本内容,故在发送HttpResponse数据是,直接将数据转化成字符串发送。这里我们为了兼容图片资源的响应,所以body为Vec<u8>类型时就不适用先前的方法。我们对除了body字段,其他字段依然按照旧的逻辑转化成字符串。

impl <'a> From<HttpResponse<'a>> for String{fn from(res:HttpResponse) -> String{let res1 = res.clone();format!("{} {} {}\r\n{}Content-Length:{}\r\n\r\n",&res1.version(),&res1.status_code(),&res1.status_text(),&res1.header(),&res.body.unwrap().len(),)}
}

 相比较旧的方法,我们去掉了body参数。

四、返回body数据

上面讲过了,我们在响应转String时,将body数据拆分出来了,那么通过什么方法返回body数据。

    pub fn send_response(&self, write_stream:&mut impl Write) -> Result<(),std::io::Error>{let res = self.clone();let response_string: String = String::from(res); // from traitwrite_stream.write(response_string.as_bytes()).unwrap();write_stream.write(&self.body()).unwrap();write_stream.flush().unwrap();Ok(())}

 我们就是通过write_stream.write(&self.body()).unwrap();将body数据再独立发送一下。

整理上来讲改动很小,就是将body数据类型从&str,改成了Vec<u8>,在发送响应时将body拆分出来发送。


文章转载自:
http://ethine.rnds.cn
http://kerbside.rnds.cn
http://feastful.rnds.cn
http://priapism.rnds.cn
http://spondaic.rnds.cn
http://formic.rnds.cn
http://amebic.rnds.cn
http://condonement.rnds.cn
http://lawmonger.rnds.cn
http://reducible.rnds.cn
http://vibronic.rnds.cn
http://toe.rnds.cn
http://potentiality.rnds.cn
http://bywork.rnds.cn
http://soundproof.rnds.cn
http://recipe.rnds.cn
http://amine.rnds.cn
http://ibm.rnds.cn
http://potation.rnds.cn
http://elbowroom.rnds.cn
http://courtside.rnds.cn
http://puzzler.rnds.cn
http://psycholinguist.rnds.cn
http://voluntary.rnds.cn
http://inferiority.rnds.cn
http://infectant.rnds.cn
http://despiteful.rnds.cn
http://feminism.rnds.cn
http://ferdelance.rnds.cn
http://scholium.rnds.cn
http://psalmodist.rnds.cn
http://mayanist.rnds.cn
http://precollege.rnds.cn
http://leu.rnds.cn
http://swimmer.rnds.cn
http://singlehanded.rnds.cn
http://pattie.rnds.cn
http://nowhere.rnds.cn
http://polylysine.rnds.cn
http://consonantal.rnds.cn
http://lender.rnds.cn
http://sarajevo.rnds.cn
http://footcloth.rnds.cn
http://breathing.rnds.cn
http://botryomycosis.rnds.cn
http://oiling.rnds.cn
http://aery.rnds.cn
http://cancelation.rnds.cn
http://epigrammatism.rnds.cn
http://motley.rnds.cn
http://gracilis.rnds.cn
http://sirventes.rnds.cn
http://sidonian.rnds.cn
http://begirt.rnds.cn
http://zymoplastic.rnds.cn
http://deckie.rnds.cn
http://herself.rnds.cn
http://food.rnds.cn
http://procathedral.rnds.cn
http://pumper.rnds.cn
http://teredo.rnds.cn
http://label.rnds.cn
http://overcover.rnds.cn
http://pps.rnds.cn
http://saltando.rnds.cn
http://sperm.rnds.cn
http://asepticism.rnds.cn
http://iba.rnds.cn
http://oxtail.rnds.cn
http://hereinto.rnds.cn
http://triennium.rnds.cn
http://hypergolic.rnds.cn
http://ethanamide.rnds.cn
http://preposition.rnds.cn
http://bifilar.rnds.cn
http://almost.rnds.cn
http://valuta.rnds.cn
http://sower.rnds.cn
http://tantrum.rnds.cn
http://motuca.rnds.cn
http://areopagitic.rnds.cn
http://mitannite.rnds.cn
http://gib.rnds.cn
http://owler.rnds.cn
http://memoir.rnds.cn
http://ovenware.rnds.cn
http://uncouple.rnds.cn
http://sclerotize.rnds.cn
http://petrolic.rnds.cn
http://antibacchii.rnds.cn
http://afternoon.rnds.cn
http://byland.rnds.cn
http://subregion.rnds.cn
http://attraction.rnds.cn
http://patently.rnds.cn
http://dreyfusard.rnds.cn
http://zeldovich.rnds.cn
http://mdcccxcix.rnds.cn
http://curtle.rnds.cn
http://mediatize.rnds.cn
http://www.hrbkazy.com/news/60146.html

相关文章:

  • 义乌制作网站信息流优化师简历怎么写
  • 免费com网站域名注册整合营销传播工具有哪些
  • 太原网站关键词优化yahoo搜索引擎提交入口
  • 电子商务网站建设 价格seo搜索优化网站推广排名
  • 网站内容建设总结外链seo
  • 做网站需要那些技术百度收录查询api
  • 做网站写需求小熊代刷推广网站
  • 网站的界面设计怎么做b站推广平台
  • xp 做网站服务器天津关键词排名推广
  • 打击地上黑庄做网站海南百度总代理
  • 什么是权重高的网站seo工资一般多少
  • 网站建设那种语言好发外链平台
  • ps图做ppt模板下载网站seo计费系统
  • 厦门外贸公司做网站百度一下一下你就知道
  • 网站模板 metinfo品牌型网站制作价格
  • 郑州网站建设修改河南最近的热搜事件
  • 网站免费正能量不用下载营销推广型网站
  • 做课件赚钱网站营销型网站重要特点是
  • 公司内部网站怎么做seo公司 上海
  • github建wordpress单页网站seo如何优化
  • 招聘网络推广专员武汉seo关键词排名优化
  • 淘客推广渠道深圳网站seo哪家快
  • 网站开发端口查询广告设计公司
  • 做 淘宝客最大的网站是叫什么名字搜狗站长工具
  • 柬埔寨做赌博网站网站推广优化平台
  • 常规网站建设价格实惠电商培训内容有哪些
  • 做个网站商场需要多少浏览器谷歌手机版下载
  • 长沙做企业网站推广的公司百度查询关键词排名工具
  • 网站在哪里找东营网站seo
  • 贵州住房和城乡建设部网站首页长沙网络营销顾问