logo
0
0
WeChat Login

Rust ervers, Services, And Apps.

章节代码对应

  • scenario1 <--> ch2

  • ezytutors/tutor-nodb <--> ch3

  • ezytutors/tutor-db <--> ch4

代码

scenario1

cd scenario1 # start tcpserverr cargo run -p tcpserver # run tcpclient cargo run -p tcpclient # run httpserver cargo run -p httpserver # home page curl http://localhost:3000 # home page curl http://localhost:3000/index.html # health page curl http://localhost:3000/health # json reponse curl http://localhost:3000/api/shipping/orders # 404 page curl http://localhost:3000/invalid-path

ezytutors

tutor-nodb
cd ezytutors # run basic-server cargo run -p tutor-nodb --bin basic-server curl http://localhost:3000/health # run tutor-service cargo run -p tutor-nodb --bin tutor-service # or cargo run -p tutor-nodb curl http://localhost:3000/health # add course curl -X POST localhost:3000/courses/ -H "Content-Type: application/json" -d '{"tutor_id": 1, "course_id": 5, "course_name": "Fourth course!"}' # get courses with tutor_id curl -X GET localhost:3000/courses/1 # get course detail curl -X GET localhost:3000/courses/1/5
tutor-db
cd ezytutors/tutor-db/ cargo sqlx prepare cargo run --bin iter1 # test iter2 cargo test --bin iter2 # test iter3 cargo test --bin iter3 # run iter3 cargo run --bin iter3 curl http://localhost:3000/health # add course curl -X POST localhost:3000/courses/ -H "Content-Type: application/json" -d '{"tutor_id": 1, "course_name": "Hello, my first course!"}' # get course curl -X GET localhost:3000/courses/1 # run iter4 cargo run --bin iter4 # run iter4 test cargo test --bin iter4 curl -v localhost:3000/courses/1/1111

Rust 相关

workspace

各个章节的代码基本采用 workspace 进行管理

需要注意 rust2024 edition 需要采用 resolver = "3" 来消除警告

# 创建一个 scenario1 项目 cargo new scenario1 # 需要手动编辑 项目中的 Cargo.toml 文件, 配置 workspace
[workspace] members = ["tcpserver", "tcpclient", "http", "httpserver"] resolver = "3"

添加子项目

# 创建一个 tcpserver 子项目 cargo new tcpserver # 创建一个 lib 类型的 http 子项目 cargo new --lib http

运行子项目

# 运行 tcpserver 子项目 cargo run -p tcpserver

Other

db

admin web

http://localhost:8080/

username postgres password 123456
# 进入 db 容器 docker exec -it workspace-db-1 bash # 使用 postgres 用户连接数据库 psql -U postgres # create database create database ezytutors; # create user create user truuser with password 'a.b.c#1#q'; # grant access privileges grant all privileges on database ezytutors to truuser; # 切换到 ezytutors 数据库 \c ezytutors; # 赋予 ezytutors.public 的权限 grant all on schema public to truuser; # use truuser login psql -U truuser -d ezytutors --password # 查看数据库列表 \list # 创建数据库 # 注意不能加 -t 参数 docker exec -i workspace-db-1 psql -U truuser -d ezytutors < database.sql docker exec -it workspace-db-1 psql -U truuser -d ezytutors --password # 查看数据 select * from ezy_course_c4;

About

Rust Servers, Services, and Apps

Language
Rust92.3%
Shell3.6%
Dockerfile3%
HTML0.9%
Others0.2%