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

企业建站域名天津seo网络

企业建站域名,天津seo网络,建网站的目的是什么,万网免费建企业网站本博客是在杨旭老师的 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://www.ds6.com.cn/news/62931.html

相关文章:

  • 佛山网站排名优化手机百度ai入口
  • 给网站底部做友情链接爱链网买链接
  • 有哪些做软件的网站关键词优化公司电话
  • 慧聪网网站建设策略google搜索入口
  • 淮南做网站的海南网站设计
  • 网站建设方案解救苏州久远网络网址导航怎样推广
  • 网站开发 国际网站外贸网站推广平台
  • 做深度游网站 知乎全球网站流量排名查询
  • 深圳住房建设保障局常德seo
  • 腾讯企业邮箱域名可以做网站吗2022知名品牌营销案例100例
  • 建设项目经济评价网站百度刷seo关键词排名
  • 深圳网站建设素材网站今日头条新闻头条
  • 网站外包谁报价电商营销推广方法
  • 做网站要不要花钱做店长百度云网盘下载
  • 做58同城网站需要多少钱郑州网站推广优化公司
  • 申请建设活动中心网站网站推广的作用在哪里
  • 企业网站可以免费做吗企业网络推广技巧
  • html5响应式网站建设平台昆明seo案例
  • wordpress 翻译失效针对百度关键词策划和seo的优化
  • 职业生涯规划大赛官网报名seo专员是什么职位
  • 虚拟主机网站建设步骤淘宝客怎么做推广
  • 完善网站建设天桥区seo全网宣传
  • 网站一般多长时间有什么可以做推广的软件
  • 网站建设做的好关键词搜索查询
  • 电子商务网站中最基本的系统是搜索引擎排名2021
  • 手机怎么上wap网站如何网络营销自己的产品
  • 企业建设网站的比例windows 优化大师
  • 新人做直播哪个网站好商品关键词优化的方法
  • 手机app软件开发公司排名久久seo正规吗
  • 织梦可以做导航网站品牌宣传的推广