This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.html
138 lines (114 loc) · 5.71 KB
/
webhook.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="zh-hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="cleartype" content="on">
<link rel="alternate" type="application/atom+xml" title="VeriPress Docs" href="/docs/feed.xml"/>
<!-- Link common CSS and JavaScript files -->
<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
<link href="/docs/static/style.css" rel="stylesheet">
<link href="/docs/static/github-markdown-style.css" rel="stylesheet">
<link href="/docs/static/highlight.css" rel="stylesheet">
<link href="/docs/static/bootstrap-theme-cosmo.min.css" rel="stylesheet">
<link rel="shortcut icon" type="image/x-icon" href="/docs/static/favicon.ico">
<title>Webhook - VeriPress Docs</title>
</head>
<body>
<div class="section">
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="row site-header-row">
<div class="col-md-8 site-header">
<h1><a href="/docs/">VeriPress Docs</a><br>
<small>Documentation of VeriPress.</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-12 nav-container">
<ul class="nav nav-tabs">
<li><a href="/docs/">简体中文</a></li>
<li><a href="/docs/en/">English</a></li>
<li><a href="https://github.com/veripress/veripress">GitHub</a></li>
</ul>
</div>
</div>
</div>
<div class="col-md-2"></div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-primary">
<div class="panel-heading"></div>
<div class="panel-body markdown-body">
<article>
<h1>Webhook</h1>
<p>VeriPress 支持通过接收 webhook 回调来在某些特定外部事件发生时执行自定义 Python 脚本,从而实现例如 GitHub 仓库发生 push 事件就自动拉取最新内容这样的用法。</p>
<p>无论运行模式是 <code>api-only</code> 还是 <code>view-only</code> 还是 <code>mixed</code>,<code>/_webhook</code> 这个 URL 都可以接收 POST 请求。收到请求后,它会在 VeriPress 实例目录中寻找 <code>webhook.py</code> 文件,如果存在,则会在 Flask 的 request context 下执行这个文件的代码(此为同步执行,如果需要进行长时间的任务,最好开新的线程或进程),不存在则跳过。无论该脚本存在与否最终都返回 <code>204 NO CONTENT</code>。</p>
<p>「在 Flask 的 request context 下执行」意味着在脚本中可以访问到 Flask 当前请求的 <code>request</code> 对象、<code>g</code> 对象、<code>current_app</code> 对象,也就是说你可以在这里对 POST 请求进行鉴权、使用甚至修改 app 的配置文件等。关于 Flask 的 request context,请参考 Flask 官方文档的 <a href="http://flask.pocoo.org/docs/0.12/reqcontext/">The Request Context</a>。</p>
<p>一段可能的自定义脚本如下:</p>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">request</span>
<span class="k">def</span> <span class="nf">check_token</span><span class="p">():</span>
<span class="c1"># 对请求进行鉴权,防止恶意请求</span>
<span class="k">return</span> <span class="bp">True</span>
<span class="k">if</span> <span class="n">check_token</span><span class="p">():</span>
<span class="n">log_file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'webhook.log'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">)</span>
<span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">([</span><span class="s1">'/bin/sh'</span><span class="p">,</span> <span class="s1">'update.sh'</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">log_file</span><span class="p">)</span>
</pre></div>
</article>
</div>
<div class="panel-footer">
<span><i class="fa fa-fw fa-user"></i>Richard Chien</span>
<span><i class="fa fa-fw fa-refresh"></i>2017.05.24</span>
</div>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
</div>
<footer class="section">
<div class="container">
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="row">
<div class="col-md-12">
<hr>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center">
<span>Copyright © 2017 VeriPress.</span>
<br class="hidden-sm hidden-md hidden-lg">
<span>Powered by <a href="https://github.com/veripress/veripress">VeriPress</a>.</span>
</div>
</div>
</div>
<div class="col-md-2"></div>
</div>
</div>
</footer>
<script type="text/javascript">
var elems = document.getElementsByTagName("a");
for (var i = 0; i < elems.length; i++) {
if (elems[i].href.indexOf(document.domain) < 0) {
elems[i].target = "_blank";
}
}
var navLinks = document.querySelectorAll(".nav-tabs li a");
for (i = 0; i < navLinks.length; i++) {
if (document.location.href === navLinks[i].href) {
navLinks[i].parentElement.classList.add("active");
}
}
</script>
</body>
</html>