在好例子网,分享、交流、成长!
您当前所在位置:首页Others 开发实例一般编程问题 → Manning.Rust.Web.Development.2022.12

Manning.Rust.Web.Development.2022.12

一般编程问题

下载此实例
  • 开发语言:Others
  • 实例大小:9.91M
  • 下载次数:4
  • 浏览次数:50
  • 发布时间:2023-02-13
  • 实例类别:一般编程问题
  • 发 布 人:jyyiii
  • 文件格式:.pdf
  • 所需积分:2
 相关标签: Development develop rust 2022 dev

实例介绍

【实例简介】Manning.Rust.Web.Development.2022.12

【实例截图】

【核心代码】

contents

preface xiii
acknowledgments xv
about this book xvii
about the author xx
about the cover illustration xxi
P ART 1 I NTRODUCTION TO R UST ....................................1
1
Why Rust? 3
1.1 Batteries included: Rust’s tooling 4
1.2 The Rust compiler 9
1.3 Rust for web services 11
1.4 Maintainability of Rust applications 16
2
Laying the foundation 18
2.1 Following the Rust playbook 19
Modeling your resources with structs 21

Understanding
options 23

Using documentation to solve errors 24
Handling strings in Rust 29

Taking an excursion into
moving, borrowing, and ownership 31

Using and
implementing traits 34

Handling results 42
CONTENTS ix
2.2 Creating our web server 43
Handling multiple requests at once 44

Rust’s asynchronous
environment 45

Rust’s handling of async/await 46
Using Rust’s Future type 48

Choosing a runtime 48
Choosing a web framework 50
P ART 2 G ETTING STARTED ............................................55
3
Create your first route handler 57
3.1 Getting to know our web framework: Warp 58
What is included in Warp 59

Warp’s filter system 59
3.2 GET your first JSON response 60
Align with your framework’s way of thinking 61

Handle the
success route 62

Get help from Serde 64

Handle errors
gracefully 65
3.3 Handling CORS headers 70
Returning CORS headers on the application level 72

Testing
CORS responses 73
4
Implement a RESTful API 77
4.1 GET questions from in-memory 79
Setting up a mock database 79

Preparing a set of test data 83
Reading from the fake database 85

Parsing query
parameters 89

Returning custom errors 93
4.2 POST, PUT, and DELETE questions 97
Updating our data in a thread-safe way 98

Adding a
question 102

Updating a question 104

Handling
malformed requests 106

Removing questions from the
storage 108
4.3 POST answers via url-form-encoded 110
Difference between url-form-encoded and JSON 110

Adding
answers via url-form-encoded 111
5
Clean up your codebase 115
5.1 Modularizing your code 116
Using Rust’s built-in mod system 116

Practical folder
structure for different use cases 122

Creating libraries
and sub-crates 127
CONTENTS x
5.2 Documenting your code 131
Using doc comments and private comments 131

Adding code in
your comments 133
5.3 Linting and formatting your codebase 136
Installing and using Clippy 136

Formatting your code with
Rustfmt 139
6
Logging, tracing, and debugging 141
6.1 Logging in your Rust application 142
Implementing logging in your web service 145

Logging incoming
HTTP requests 151

Creating structured logs 155
6.2 Tracing in asynchronous applications 162
Introducing the Tracing crate 163

Integrating tracing in our
application 164
6.3 Debugging Rust applications 168
Using GDB on the command line 170

Debugging our web service
with LLDB 171

Using Visual Studio Code and LLDB 173
7
Add a database to your application 177
7.1 Setting up our example database 178
7.2 Creating our first tables 179
7.3 Working with a database crate 181
Adding SQLx into our project 184

Connecting Store to our
database 185
7.4 Reimplementing our route handlers 189
Adding the database to get_questions 190

Reimplementing the
add_question route handler 197

Adjusting the update and delete
questions handler 200

Updating the add_answer route 202
7.5 Error handling and tracing database interactions 204
7.6 Integrating SQL migrations 210
7.7 Case study: Switching database management systems 214
8
Integrate third-party APIs 218
8.1 Preparing the codebase 221
Picking an API 221

Getting to know our HTTP crate 223
Adding an example HTTP call with Reqwest 225

Handling
errors for external API requests 227
CONTENTS xi
8.2 Deserializing JSON responses to structs 234
Gathering API response information 235

Creating types for our
API responses 236
8.3 Sending questions and answers to the API 241
Refactoring the add_question route handler 241

Making
profanity checks for updating questions 244

Updating the
add_answer route handler 245
8.4 Handling timeouts and multiple requests at once 246
Implementing a retry for external HTTP calls 247

Executing
futures concurrently or in parallel 251
P ART 3 B RING IT INTO PRODUCTION ...........................255
9
Add authentication and authorization 257
9.1 Adding authentication to our web service 258
Creating the user concept 260

Migrating the database 262
Adding the registration endpoint 264

Hashing the
password 267

Handling duplicate account errors 269
Stateful vs. stateless authentication 275

Adding the login
endpoint 276

Adding an expiry date to tokens 280
9.2 Adding authorization middleware 282
Migrating the database tables 283

Creating token validation
middleware 283

Extending existing routes to handle
account IDs 288
9.3 What we didn’t cover 293
10
Deploy your application 295
10.1 Setting up your application through environment
variables 296
Set up config files 297

Accept command-line inputs for your
application 300

Read and parse environment variables into
your web service 302
10.2 Compiling your web service for different
environments 308
Development vs. release flag when building your binary 308
Cross-compile your binary for different environments 309
10.3 Using build.rs in your build process 311
CONTENTS xii
10.4 Creating the right Docker image for your web service 314
Create a statically linked Docker image 315

Set up a local Docker
environment with docker-compose 316

Extract the configuration
of the web server into a new module 319
11
Testing your Rust application 325
11.1 Unit testing our business logic 327
Testing the pagination logic and dealing with custom errors 327
Testing the Config module with environment variables 331
Testing the profanity module with a newly created mock server 335
11.2 Testing our Warp filters 343
11.3 Creating an integration testing setup 347
Splitting up the codebase into a lib.rs and a binary 349

Creating
the integration-test crate and the oneshot server implementation 352
Adding the registration test 355

Unwinding in case of an
error 359

Testing the login and posting questions 360
appendix Thinking about security 364
index 369


网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警