-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
45 lines (30 loc) · 992 Bytes
/
app.py
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
from flask import abort, Flask, jsonify, make_response, request
import crawler.cfg as cfg
from crawler.common import logger as logger
from crawler.crawl import Crawler
# Flask api server
app = Flask(__name__)
# Not Found
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
# Health check
@app.route('/_health')
def health():
return jsonify({'msg':"I am ok!"})
# Crawler endpoint
@app.route('/crawl', methods=['POST'])
def create_task():
# Validate request body
if not request.json or not 'url' in request.json:
abort(400)
# url to crawl
url = request.json.get('url')
logger.info("Request received to crawl: '%s'" % url)
# Initialize crawler
crawler = Crawler(url)
# Start crawling
sitemap, failed = crawler.start()
return jsonify({'url': url, 'failed': failed, 'sitemap': sitemap}), 201
if __name__ == '__main__':
app.run(port=cfg.PORT, debug=True, host='0.0.0.0')