Featured image of post Weekly Report 2022.10

Weekly Report 2022.10

暗自怜惜

倘若人性本为恶,那么,若能在此基础上建立起自己的真善美,岂不是一件可歌可泣的事情;其整个过程又何尝不是值得我们去追寻的宏功伟业。

# Reading

# 少年维特的烦恼

这好像是一本饱受赞誉的经典书籍,但是读完之后,第一时间只得出了俗不可耐的感叹:“舔狗不得 house”。

作为一个毫无恋爱经验的人,我就不做过多评价了;不管怎么说,生命诚可贵,爱情价更高。 若为自由故,两者皆可抛。

# 蜜蜂的寓言

从道德的角度看,以自利驱动的行为是应该受到谴责的,但如果想以“公共精神”的道德情怀来建立一种充满美德的繁荣社会,那只是一种“浪漫的奇想”,因为私欲和私欲支配的个人恶行恰恰是社会繁荣的能源,离开了这个能源,公共利益将是无源之水,无本之木。公益心和道德感这样的善之花,都将结出贫困和伪善的恶之果。

# Learning

# TLPI

I/O 层面上并没有太多新的内容,主要还是读、写、offset 相关内容;进程这块学到了很多,包括进程的组成要素、虚拟内存、堆栈、环境变量等。具体可以参见这里

# Axum Usage

裸着使用 tokio::TcpStream.write_all 明明能够骗过浏览器,却不能和 Prometheus 的 scraper 完成交互;于是乎,不得不用起开箱即用的 Web 框架,即 tokio 出品的 axum

暂时也还没学到多少,这里就展示一个比较清晰的例子吧。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#[tokio::main]
async fn main() {
    // Set the RUST_LOG, if it hasn't been explicitly defined
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "example_tokio_postgres=debug")
    }
    tracing_subscriber::fmt::init();

    let db_connection_str = std::env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string());

    // setup connection pool
    let pool = PgPoolOptions::new()
        .max_connections(5)
        .connect_timeout(Duration::from_secs(3))
        .connect(&db_connection_str)
        .await
        .expect("can connect to database");

    // build our application with some routes
    let app = Router::new()
        .route(
            "/",
            get(using_connection_pool_extractor).post(using_connection_extractor),
        )
        // 通过Extension形式可以在App中直接获取到pool
        .layer(Extension(pool));

    // run it with hyper
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    axum::Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

// we can extract the connection pool with `Extension`
async fn using_connection_pool_extractor(
    Extension(pool): Extension<PgPool>,
) -> Result<String, (StatusCode, String)> {
    sqlx::query_scalar("select 'hello world from pg'")
        .fetch_one(&pool)
        .await
        .map_err(internal_error)
}

// we can also write a custom extractor that grabs a connection from the pool
// which setup is appropriate depends on your application
struct DatabaseConnection(sqlx::pool::PoolConnection<sqlx::Postgres>);

// 通过实现FromRequest Trait可以从Request解析出当前struct
#[async_trait]
impl<B> FromRequest<B> for DatabaseConnection
where
    B: Send,
{
    type Rejection = (StatusCode, String);

    async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
        let Extension(pool) = Extension::<PgPool>::from_request(req)
            .await
            .map_err(internal_error)?;

        let conn = pool.acquire().await.map_err(internal_error)?;

        Ok(Self(conn))
    }
}

async fn using_connection_extractor(
    DatabaseConnection(conn): DatabaseConnection,
) -> Result<String, (StatusCode, String)> {
    let mut conn = conn;
    sqlx::query_scalar("select 'hello world from pg'")
        .fetch_one(&mut conn)
        .await
        .map_err(internal_error)
}

/// Utility function for mapping any error into a `500 Internal Server Error`
/// response.
fn internal_error<E>(err: E) -> (StatusCode, String)
where
    E: std::error::Error,
{
    (StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
}

# Life

本周发现,工作以外的零散时间确实不够用来完成太多事情。就拿读书来说,有些书可以随意搁置在书中任意地方,后续很轻松就能拾起来;而有些书一旦合上了,下次再打开的时候,就不知道之前看了些什么内容了,尤其是论述类的文章;这个问题需要寻找一个比较合适的解决办法。

# Quotation

再野蛮的人亦会为赞扬所陶醉;再卑劣的人亦绝不会容忍轻蔑。他们由此做出了正确结论,即恭维必是一种最有力的理由,能够用诸人类。于是,他们便利用这部令人着迷的引擎,高赞人类天性的卓越,说它高于其他一切动物,并用漫无边际的赞美,讲述人类传奇般历史上的奇迹,阐明人类知解力的广阔无垠,将千种赞誉加在人类心灵的理性上,说人类依靠它们的帮助,才取得了那些最高尚的成就。他们以这种精心构造出来的恭维,使自己悄然溜入了人心。继而,他们开始向人们灌输关于荣辱的观念,说其中一种乃是万恶之首,而另外一种则是人类能指望达到的至善。做了这一切之后,他们便向人们展示:以如此崇高的生灵之尊贵,去追求满足他们那些与禽兽共有的欲望,却不看重那些更高级的品质,而这些品质使他们比一切可见的生物都要卓越,这样的情况与人类的这种地位又是何其不相称。诚然,他们亦承认:人类天性的那些冲动乃是非常迫切的;抵抗那些冲动会引出许多麻烦,而彻底扑灭它们亦极为困难。不过,他们仅仅将这一点当作一个论据,以证明战胜这些冲动是何等光荣,而不试图削弱它们又是何等令人反感。

# Recommendation

# References

Licensed under CC BY-NC-SA 4.0
Last updated on Sep 08, 2022 09:07 CST
The older I get, the more I realize that most of life is a matter of what we pay attention to, of what we attend to [with focus].
Built with Hugo
Theme Stack designed by Jimmy