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

手机怎么做销售网站网建公司

手机怎么做销售网站,网建公司,快站网如何开始建站,网店托管公司可靠吗Actix-web Rust连接Postgres数据库​Rust1.39支持了异步async,await,Actix-web在2.0.0-alpha支持了原生异步写法,所以本文中使用的Actix-web版本为2.0.0-alpha.4。​Actix-web官方例子使用的是r2d2连接池库,这个库并不是异步库,需要用web::bl…

Actix-web Rust连接Postgres数据库

​Rust1.39支持了异步async,await,Actix-web在2.0.0-alpha支持了原生异步写法,所以本文中使用的Actix-web版本为2.0.0-alpha.4。

​Actix-web官方例子使用的是r2d2连接池库,这个库并不是异步库,需要用web::block的api,不是很方便,我找到了一个deadpool-postgres连接池库,采用tokio-postgres作为数据库连接。直接支持异步省去很多麻烦。

初始化项目

直接用cargo new pgtest来初始化一个项目

修改Cargo.toml

[package]

name = "pgtest"

version = "0.1.0"

authors = ["yuxq"]

edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

actix-web = "2.0.0-alpha.4"

# actix运行时

actix-rt = "1.0.0-alpha.3"

tokio-postgres = "0.5.0-alpha.2"

deadpool-postgres = "0.2.3"

修改默认main方法

官方运行异步actix服务器是使用actix-rt库,方法如下

#[actix_rt::main]

async fn main()-> std::io::Result {

HttpServer::new( ||

App::new()

.bind("127.0.0.1:8080")?

.start()

.await

}

创建postgres连接池

use deadpool_postgres::{Manager, Pool};

use tokio_postgres::{Config, NoTls};

#[actix_rt::main]

async fn main()-> std::io::Result {

let mut cfg = Config::new();

cfg.host("localhost");//数据库地址

cfg.user("db");//数据库用户名

cfg.password("db");//数据库密码

cfg.dbname("asynctest");//数据库名称

let mgr = Manager::new(cfg, tokio_postgres::NoTls);

let pool = Pool::new(mgr, 15);//最大15个连接

}

绑定连接池对象

actix-web官方文档对State的解释

Application state is shared with all routes and resources within the same scope. State can be accessed with the web::Data extractor. State is also available for route matching guards and middlewares.

我们可以把对象绑定进Application,同所有具有相同命名空间的路径和资源共享,之后再用web::Data提取器获取到。

use deadpool_postgres::{Manager, Pool};

use tokio_postgres::{Config, NoTls};

#[actix_rt::main]

async fn main()-> std::io::Result {

let mut cfg = Config::new();

cfg.host("localhost");//数据库地址

cfg.user("db");//数据库用户名

cfg.password("db");//数据库密码

cfg.dbname("asynctest");//数据库名称

let mgr = Manager::new(cfg, tokio_postgres::NoTls);

let pool = Pool::new(mgr, 15);//最大15个连接

HttpServer::new( move ||

App::new().data(pool.clone())

.bind("127.0.0.1:8080")?

.start()

.await

}

在handler中获取数据库连接池

首先让我们创建一个具有web::Data提取器的handler

我在本机上跑了一个docker运行postgres数据库

创建了一个users的表,字段有【id,name,age】例子只获取name

use actix_web::{web,Responder};

use deadpool_postgres::{Manager, Pool};

async fn get_user(db:web::Data)->impl Responder{

let mut conn=db.get().await.unwrap();

let rows=conn.query("select * from users",&[]).await.unwrap();

let v:String=rows[0].get("name");//get参数可以是str,也可以是i32,获取第几个。但是必须要指明获取的类型

format!("{}",v)

}

将handler绑定至server application

HttpServer::new( move ||

App::new().data(pool.clone()).route("user",web::get().to(get_user))

.bind("127.0.0.1:8080")?

.start()

.await

通过cargo run运行即可。

http://www.lbrq.cn/news/2367433.html

相关文章:

  • 海口网约车需要什么条件长沙优化排名推广
  • 江门市网站建设公司磁力狗在线引擎
  • 铜陵专业网站制作公司正规拉新推广平台有哪些
  • 做网站的的需求文档网络推广费用计入什么科目
  • 新闻网站设计原则国际最新新闻热点事件
  • 手机网站首页模板优化方法
  • wordpress 开放适配seo1现在怎么看不了
  • 用sublime text做网站google优化推广
  • 滕滕州网站建设杭州seo网站排名
  • 铜陵app网站做营销招聘信息如何在手机上开自己的网站
  • 做网站不给源代码百度推广北京总部电话
  • 镇江网站seo公司百度竞价点击神器
  • 巫山网站开发百度外推排名
  • 巴中做网站公司进入百度知道首页
  • 汕头企业网站推广方法抖音搜索引擎优化
  • 徐州住房和城乡建设局网站如何做网址
  • 鱼巴适设计师服务平台排名优化软件
  • 东莞网站建设哪里找营销型网站建设团队
  • 没有网站也可以做外贸吗营销网站建设制作
  • 做外国语上门按摩服务网站seo运营工作内容
  • 网站的备案信息域名不一样厦门小鱼网
  • asp.net 项目成本预算系统的 网站开发聊城seo
  • 网站建设实训报告要求朋友圈广告代理商官网
  • wordpress 幻灯片手机端字体大小网站排名优化培训哪家好
  • 怎么区分营销型网站软件排名工具
  • 高端的赣州网站建设关键词seo是什么
  • 网络营销与管理专业柏乡seo快排优化
  • 优惠活动制作网站电子商务网站建设方案
  • 武汉哪家做网站比较好郑州网络推广效果
  • 搞笑网站模板百度关键词搜索次数
  • RuoYi-Cloud 定制微服务
  • git基本操作【GIT-2】
  • MongoDB 与MySQL 及es的区别
  • ES组合使用must与should时的注意事项
  • Qt 将触摸事件转换为鼠标事件(Qt4和Qt5及以上版本)
  • 深入掌握Python正则表达式:re库全面指南与实战应用