Home 增加webhooks自动更新blog
Post
Cancel

增加webhooks自动更新blog

前言

需要实现一个,当push代码后,实现一个github的webhooks的接口,然后拉去最新的内容,更新博客。之前由Spring boot实现过一款,但是简单的一个接口,占用的内存就比较高,所以现在改用rust来实现。这样可以使内存的占用直接控制在10mb以内。

增加依赖

1
2
[dependencies]
actix-web = "4.4.0"

代码

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
use std::process::Command;
use actix_web::{App, HttpResponse, HttpServer, Responder, post};

//webhook是post请求,所以对所有post请求都执行更新web
#[post("/")]
async fn get_pull_update() -> impl Responder {
    //执行git pull 返回shell执行的结果
    match Command::new("sh").arg("-c").arg("git pull").output() {
        Ok(res) => HttpResponse::Ok().body(format!("update success! {:?}", res)),
        Err(err) => HttpResponse::Ok().body(format!("update fail! {}", err))
    }
}

//将 async main 函数标记为 actix 系统的入口点。
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    //创建 http 服务器
    HttpServer::new(|| {
        App::new()//新建一个应用
            .service(get_pull_update)//将 hello 函数作为服务
    })
        .bind("0.0.0.0:8080")?//绑定ip和端口,这里需要使用0.0.0.0才能接收到外部请求
        .run()//开始监听
        .await
}

额外需要注意的是,在容器类需要下载git,然后配置ssh,就可以正常使用了

This post is licensed under CC BY 4.0 by the author.