标签:environ ubunt source started nes mit release limit span
diesel 是一个不错的rust orm框架,提供了cli ,可以方便的进行migration 操作,以及帮助代码生成
以下是一个简单的试用
注意需要最新的stable 版本,数据库使用docker-compose 运行
cargo install diesel_cli
version: "3"
services:
app:
build: ./
pg:
image: postgres:12.1-alpine
environment:
- "POSTGRES_PASSWORD:dalong"
ports:
- "5432:5432"
cargo new --lib diesel_demo
cd diesel_demo
[dependencies]
diesel = { version = "1.0.0", features = ["postgres"] }
dotenv = "0.9.0"
.env 本地开发模式使用
DATABASE_URL=postgres://postgres:dalong@localhost/diesel_demo
.env-prod: docker 生产使用
DATABASE_URL=postgres://postgres:dalong@pg/diesel_demo
docker-compose up -d pg
diesel setup
diesel migration generate create_posts
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
title VARCHAR NOT NULL,
body TEXT NOT NULL,
published BOOLEAN NOT NULL DEFAULT ‘f‘
)
down.sql
DROP TABLE posts
diesel migration run
#[macro_use]
extern crate diesel;
extern crate dotenv;
?
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
pub mod schema;
pub mod models;
pub fn establish_connection() -> PgConnection {
dotenv().ok();
?
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
#[derive(Queryable)]
pub struct Post {
pub id: i32,
pub title: String,
pub body: String,
pub published: bool,
}
table! {
posts (id) {
id -> Int4,
title -> Varchar,
body -> Text,
published -> Bool,
}
}
extern crate diesel_demo;
extern crate diesel;
?
use self::diesel_demo::*;
use self::models::*;
use self::diesel::prelude::*;
?
fn main() {
use diesel_demo::schema::posts::dsl::*;
?
let connection = establish_connection();
let results = posts.filter(published.eq(true))
.limit(5)
.load::<Post>(&connection)
.expect("Error loading posts");
?
println!("Displaying {} posts", results.len());
for post in results {
println!("{}", post.title);
println!("----------\n");
println!("{}", post.body);
}
}
INSERT INTO "public"."posts"("id","title","body","published")
VALUES
(1,E‘dalong‘,E‘demo‘,TRUE);
cargo run --bin show_posts
FROM rustlang/rust:nightly-slim as build
WORKDIR /app
RUN mkdir -p $HOME/.cargo
COPY diesel_demo/ /app
# cargo 加速
COPY diesel_demo/config $HOME/.cargo/
RUN apt-get update && apt-get install -y libpq-dev
RUN rustup --version
RUN rustc --version && \
rustup --version && \
cargo --version
RUN cargo build --bin show_posts --release
?
FROM debian:stretch-slim
WORKDIR /app
RUN apt-get update && apt-get install -y libpq5
COPY diesel_demo/.env-prod /app/.env
COPY --from=build /app/target/release/show_posts /usr/local/bin/show_posts
CMD [ "show_posts" ]
加速配置
config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = ‘ustc‘
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
从目前来说rust 以及python 还有php 等语言,使用alpine 做为基础镜像并不太好(依赖,libc 的不同,还有依赖)
使用debian,ubuntu,还有centos 作为这类语言的基础镜像会很不错,有时表象的小,可能并不是最优的方案
http://diesel.rs/guides/getting-started/
https://github.com/rongfengliang/diesel-docker-running
https://www.cnblogs.com/rongfengliang/p/11337157.html
https://lug.ustc.edu.cn/wiki/mirrors/help/rust-crates
标签:environ ubunt source started nes mit release limit span
原文地址:https://www.cnblogs.com/rongfengliang/p/12256614.html