• Rust升级edition到2021

    2021年Rust发布了新的edition,叫做2021。此前的edition是2018年发布的,将近三年才更新了edition,但是如果在往前看的话,是2015,似乎都是三年一更新。

    如果现在项目是一个2018年或者更早的edition,推荐升级到最新的,升级的方法也很简单,先在项目下,执行以下语句

    cargo fix --edition
    

    如果不出错,就打开你的Cargo.toml文件,修改edition的值到2021

    Cargo.toml

    [package]
    name = "qttc"
    version = "1.0.3"
    authors = ["Nicholas Lee"]
    edition = "2021"
    

    ...

    READ ALL

  • Debian错误Can't locate Term/ReadLine.pm

    最近在构建Docker镜像时,总是失败在Can't locate Term/ReadLine.pm,日志如下

    Get:1 http://deb.debian.org/debian bullseye/main amd64 openssl amd64 1.1.1k-1+deb11u1 [851 kB]
    Get:2 http://deb.debian.org/debian bullseye/main amd64 ca-certificates all 20210119 [158 kB]
    debconf: delaying package configuration, since apt-utils is not installed
    Fetched 1009 kB in 25s (41.2 kB/s)
    Selecting previously unselected package openssl.
    (Reading database ... 6772 files and directories currently installed.)
    Preparing to unpack .../openssl_1.1.1k-1+deb11u1_amd64.deb ...
    Unpacking openssl (1.1.1k-1+deb11u1) ...
    Selecting previously unselected package ca-certificates.
    Preparing to unpack .../ca-certificates_20210119_all.deb ...
    Unpacking ca-certificates (20210119) ...
    Setting up openssl (1.1.1k-1+deb11u1) ...
    Setting up ca-certificates (20210119) ...
    debconf: unable to initialize frontend: Dialog
    debconf: (TERM is not set, so the dialog frontend is not usable.)
    debconf: falling back to frontend: Readline
    debconf: unable to initialize frontend: Readline
    debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.32.1 /usr/local/share/perl/5.32.1 /usr/lib/x86_64-linux-gnu/perl5/5.32 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.32 /usr/share/perl/5.32 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
    debconf: falling back to frontend: Teletype
    Updating certificates in /etc/ssl/certs...
    129 added, 0 removed; done.
    Processing triggers for ca-certificates (20210119) ...
    Updating certificates in /etc/ssl/certs...
    0 added, 0 removed; done.
    Running hooks in /etc/ca-certificates/update.d...
    done.
    Removing intermediate container a19d55490057
     ---> d439938bfcf5
    Step 9/10 : COPY --from=0 /usr/src/myapp/target/release/sso .
    invalid from flag value 0: No such image: sha256:d31713282fd49b05c2c5488678d25fc936b2abdde33bf4e71c869adbb419944f
    Build step 'Execute shell' marked build as failure
    Sending e-mails to: lizhong@uino.com
    Finished: FAILURE
    Cleaning up project directory and file based variables
    00:00
    ERROR: Job failed: command terminated with exit code 1
    

    ...

    READ ALL

  • 构建Docker镜像时全局安装NPM包提示permission denied无权限

    full Docker npm install permission denied

    使用NodeJS镜像node在构建Docker镜像时,总是在npm install -g <package_name>全局安装一些包会提权限被拒绝,虽然是以root用户构建,但仍然有这个错误。目前找到的解决方案就是把项目创建在宿主目录/root下,并多加一个环境变量ENV NPM_CONFIG_PREFIX=/root/.npm-global,然后重新构建镜像就不再提示没权限

    ...

    READ ALL

  • RxJS HTTP轮询拉取例子

    full RxJS http poll

    页面上经常需要每几秒钟轮询服务器接口更新页面状态,这种场景多发生在一些监控页面里,如果不用RxJS可能我们这写轮询代码

    const do_request = () => {
      fetch('https://www.qttc.net/api')
        .then((r) => {
          console.log(r);
        });
    };
    
    setInterval(do_request, 2000);
    

    ...

    READ ALL

  • Rust在VSCode中Debug一次经历

    full Rust debug in VSCode case

    当时正在紧张的编写一个快要必须要在下班前演示的程序,结果在读取Cookie信息时出错了,于是我使用VSCode的Debug模式调试程序,这是个灾难的开始。VSCode配置如下

    launch.json

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "(Windows) Boot",
          "type": "cppvsdbg",
          "request": "launch",
          "program": "${workspaceFolder}/target/debug/rust-debug.exe",
          "args": [],
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "environment": [],
          "console": "externalTerminal"
        }
      ]
    }
    

    ...

    READ ALL

  • Rust配置crates.io国内镜像,加速依赖更新

    在中国做软件开发,往往需要特殊的技能,这点非常麻烦。crates.io经常因为网络问题会卡在

    $ cargo update
      Updating crates.io index
    

    幸运的话可以通过,最头疼的是等了相当长的时间过后因为网络问题的终止,解决这个问题可以配置crates.io国内镜像,这里以清华大学开源软件镜像站做例子,编辑~/.cargo/config文件,添加以下内容

    [source.crates-io]
    replace-with = 'tuna'
    
    [source.tuna]
    registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
    

    ...

    READ ALL

  • 关于img标签referrerpolicy属性与请求头Referer

    在制作本博客时,有一个文章页生成二维码的一个小需求,简单来说就是传入一个URL返回一个二维码图片。于是自然的就想起了浏览器在发出HTTP请求时通常会默认把来源URL添加到HTTP的请求头Referer字段的这么一个特性

    GET /qrcode HTTP/1.1
    Host: www.qttc.net
    Referer: https://www.qttc.net/about.html
    

    于是页面上显示二维码的代码这么写

    index.html

    <img src="https://static.qttc.net/qrcode" />
    

    ...

    READ ALL

  • Nginx配置项underscores_in_headers

    full nginx logo

    遇到一个比较奇怪的问题,发现经过Nginx反向代理到服务器端时部分头字段被过滤掉了。经过查阅资料,得知HTTP头字段名称在标准下是使用中划线拼接的,例如

    Custom-Header: QTTC

    假如你想使用下划线拼接

    Custom_Header: QTTC

    这是不符合HTTP标准的,默认在Nginx下进行会被drop掉,担心跟CGI变量混淆,以下是官方说明

    missing-disappearing-http-headers

    ...

    READ ALL

  • Rust编译到WebAssembly例子

    full rust webassembly

    随着WebAssembly越来约完善,很多Web应用可以利用它解决性能计算的业务场景,甚至WebAssembly未来还有计划实现调用外部JavaScript方法的计划。

    目前官方推荐使用四种语言编写WebAssembly的源码,它们分别是

    • C/C++ 所有人都知道的语言
    • Rust 我最喜欢的语言
    • AssemblyScript 如果你写C/C++,Rust不舒服,这是最佳的选择
    • WebAssembly 直接编写WebAssembly的格式,难度比较大

    ...

    READ ALL

  • Rust中String与&str互转

    在Rust中字符串是个复杂的概念,String和&str的性质不一样,&str可以使用to_string()转换

    let str: String = "琼台博客".to_string();
    

    如果要把String转回&str,可以使用切片的引用模式

    let str: &str = &"琼台博客".to_string()[..];
    

    ...

    READ ALL