本文代码 https://github.com/tothis/rust-record/tree/main/test/actix-web
已集成功能
- log4rs 集成
- SQLx 集成
- Actix Web CRUD
Cargo.toml
[package]
name = "actix-web-record"
version = "0.1.0"
authors = ["Li Lei <this.lilei@gmail.com>"]
edition = "2021"
[dependencies]
serde = "1.0"
actix-web = "4.0"
dotenv = "0.15"
log = "0.4"
log4rs = "1.0"
[dependencies.sqlx]
version = "0.5"
features = [
"postgres",
"runtime-actix-rustls"
]
数据库结构
-- ----------------------------
-- Sequence structure for user_id_seq
-- ----------------------------
DROP SEQUENCE IF EXISTS "public"."user_id_seq";
CREATE SEQUENCE "public"."user_id_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER SEQUENCE "public"."user_id_seq" OWNER TO "postgres";
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS "public"."user";
CREATE TABLE "public"."user" (
"id" int8 NOT NULL GENERATED BY DEFAULT AS IDENTITY (
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1
),
"created_at" timestamp(6),
"updated_at" timestamp(6),
"deleted_at" timestamp(6),
"name" varchar(255) COLLATE "pg_catalog"."default"
)
;
ALTER TABLE "public"."user" OWNER TO "postgres";
-- ----------------------------
-- Alter sequences owned by
-- ----------------------------
ALTER SEQUENCE "public"."user_id_seq"
OWNED BY "public"."user"."id";
SELECT setval('"public"."user_id_seq"', 1, false);
-- ----------------------------
-- Primary Key structure for table user
-- ----------------------------
ALTER TABLE "public"."user" ADD CONSTRAINT "user_pkey" PRIMARY KEY ("id");
.env
DATABASE_URL="postgres://postgres:123456@127.0.0.1/actix_web_record"
config/log4rs.yaml
refresh_rate: 30 seconds
appenders:
stdout:
kind: console
encoder:
pattern: "{d(%F %H:%M:%S%.f)} - {m}{n}"
main:
kind: file
path: "test/actix-web/log/main.log"
encoder: