-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (97 loc) · 3.62 KB
/
main.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
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
"""Search Microservice v1.0
Primary gateway to search data from pratilipi data store
currently supports search of -
- authors
- pratilipis (content)
"""
import os
import sys
import requests
import inspect
import simplejson as json
import bottle
import time
from datetime import datetime
from src import v1
from config import config
from bottle import Bottle, route, run, request, response
from lib.commonfns import api_response, log_formatter, requested_api_version
application = Bottle()
print log_formatter(inspect.stack()[0][3], os.environ)
#fetch config
config_dict = {'solr_url': config.SOLR_URL,
'pratilipi_url': config.PRATILIPI_SERVICE_URL,
'author_url': config.AUTHOR_SERVICE_URL,
'trending_limit': config.TOP_SEARCH_LIMIT,
'trending_age': config.TOP_SEARCH_AGE_IN_MIN,
'redis_url': config.REDIS_URL,
'redis_port': config.REDIS_PORT,
'redis_db': config.REDIS_DB,
'db_host': config.DB['host'],
'db_port': config.DB['port'],
'db_name': config.DB['name'],
'db_user': config.DB['user'],
'db_pass': config.DB['pass'], }
print log_formatter(inspect.stack()[0][3], config_dict)
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
ft = "timetaken - {0:.2f} sec".format(te-ts)
print log_formatter(method.__name__, ft)
return result
return timed
@application.hook('after_request')
def enable_cors():
response.set_header('Access-Control-Allow-Origin', '*')
response.set_header('Access-Control-Allow-Credentials', 'true')
response.set_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
response.set_header('Access-Control-Allow-Headers', 'AccessToken, Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token, Version')
@application.route('/health', method=['OPTIONS', 'GET'])
@application.route('/search/health', method=['OPTIONS', 'GET'])
def health():
"""
health - to check health of the api
"""
result = [200, "OK", {"state":"healthy"}]
return api_response(result)
@application.route('/search/search', method=['OPTIONS', 'GET'])
@timeit
def search():
"""
search data
"""
st = time.time()
# set default response
result = [404, "Not Found"]
if request.method == "OPTIONS":
return api_response([200, "Success"])
elif request.method != "GET":
return api_response(result)
print log_formatter(inspect.stack()[0][3], "search start")
user_id = request.headers.get('User-Id', 0)
if requested_api_version(request.headers) == 1.0:
result = v1.search(config_dict, request.query, user_id)
print log_formatter(inspect.stack()[0][3], "search done")
return api_response(result)
@application.route('/search/trending_search', method=['OPTIONS', 'GET'])
@timeit
def trending_search():
"""
trending search
"""
# set default response
result = [404, "Not Found"]
if request.method == "OPTIONS":
return api_response([200, "Success"])
elif request.method != "GET":
return api_response(result)
print log_formatter(inspect.stack()[0][3], "trending search start")
if requested_api_version(request.headers) == 1.0:
result = v1.trending_search(config_dict, request.query)
print log_formatter(inspect.stack()[0][3], "trending search done")
return api_response(result)
if __name__ == '__main__':
print log_formatter(inspect.stack()[0][3], "start running search app")
run(application, host='127.0.0.1', port=8080)