forked from tilmanginzel/scrapp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapp_server.py
63 lines (49 loc) · 1.64 KB
/
scrapp_server.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import Flask
from flask import g
from flask_restful import Api
from resources.rules import Rule, RuleCollection
from resources.subscriptions import Subscription, RuleSubscriptionCollection
from resources.users import User, UserCollection
from resources.results import Result
import config
from database_manager import DatabaseManager
from flask import render_template, request
"""
Scrapp Server RESTful-Api.
Built with Flask and Flask-RESTful.
"""
app = Flask(__name__)
api = Api(app)
PREFIX = "/api/v1"
# add Resources to API
api.add_resource(RuleCollection, PREFIX+"/rules")
api.add_resource(Rule, PREFIX+"/rules/<int:rule_id>")
api.add_resource(Result, PREFIX+"/rules/<int:rule_id>/result")
api.add_resource(RuleSubscriptionCollection, PREFIX+"/rules/<int:rule_id>/subscriptions")
api.add_resource(Subscription, PREFIX+"/subscriptions/<int:subscription_id>")
api.add_resource(UserCollection, PREFIX+"/users")
api.add_resource(User, PREFIX+"/users/<int:user_id>")
@app.before_request
def before_request():
"""
Gets called before each request.
Stores DatabaseManager instance in global g object.
"""
g.db = DatabaseManager()
@app.teardown_request
def teardown_request(exception):
"""
Gets called after each request.
Closes Database Connection.
"""
db = g.get('db', None)
if db:
db.close()
@app.route('/')
def hello_world():
create_rule_url = request.base_url + PREFIX[1:] + "/rules"
return render_template('editor.html', create_rule_url=create_rule_url)
if __name__ == '__main__':
app.run(debug=True, host=config.SERVER_URL, port=config.SERVER_PORT)