-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebserver.py
executable file
·138 lines (111 loc) · 4.01 KB
/
webserver.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
import tornado.ioloop
import tornado.web
import tornado.options
import logging
import MySQLdb, argparse, datetime, unicodedata, time
from PyRSS2Gen import RSS2
from jinja2 import Environment, FileSystemLoader
from common import *
from config import Config
from database import DB
from databaseQueries import DBQ
from repo import Repo
from commit import Commit
from keywordsfilter import *
def getFeed():
feed = RSS2(
title = "Crypto.is Code Audit Feed",
description = "Just a thing, right?",
link = "https://crypto.is",
lastBuildDate = datetime.datetime.utcnow()
)
return feed
class RSSHandler(tornado.web.RequestHandler):
def get(self, keywords):
fiveDaysAgo = time.time() - (5 * 24 * 60 * 60)
commits = DBQ.findByKeywordsAndFulltext(keywords, fiveDaysAgo)
feed = getFeed()
for c in commits:
feed.items.append(c.toRSSItem())
self.set_header('Content-Type', 'application/rss+xml')
xml = feed.to_xml()
self.write(xml)
return
class KeywordsHandler(tornado.web.RequestHandler):
def get(self, keywords):
commits = DBQ.findByKeywords(keywords)
feed = getFeed()
for c in commits:
feed.items.append(c.toRSSItem())
self.set_header('Content-Type', 'application/rss+xml')
xml = feed.to_xml()
self.write(xml)
return
class LandingHandler(tornado.web.RequestHandler):
def get(self):
commits=[]
template = env.get_template('search.html')
html = template.render(commits=commits)
self.write(html)
return
class HallOfFameHandler(tornado.web.RequestHandler):
def get(self):
template = env.get_template('halloffame.html')
html = template.render()
self.write(html)
return
class CommitHandler(tornado.web.RequestHandler):
def get(self, project, uniqueid):
commit = DBQ.findByIDs(project, uniqueid)
if len(commit) > 1:
raise "More than one commit returned?"
if not commit:
self.write("Could not find commit")
return
commit = commit[0]
template = env.get_template('commit.html')
html = template.render(commit=commit)
self.write(html)
return
class SearchHandler(tornado.web.RequestHandler):
def get(self, keywords):
commits = []
if not keywords:
template = env.get_template('search.html')
else:
DBQ.logTerms(self.request.remote_ip, keywords)
commits = DBQ.findByKeywordsAndFulltext(keywords)
template = env.get_template('searchresults.html')
html = template.render(commits=commits, rsssearchparams=keywords)
self.write(html)
return
def post(self, keywords):
keywords = self.request.arguments['terms'][0]
DBQ.logTerms(self.request.remote_ip, keywords)
commits = DBQ.findByKeywordsAndFulltext(keywords)
template = env.get_template('searchresults.html')
html = template.render(commits=commits, rsssearchparams=keywords)
self.write(html)
return
env = Environment(autoescape=True,
loader=FileSystemLoader(Config.fsdir + 'templates'))
application = tornado.web.Application([
(r"/", LandingHandler),
(r"/search/?(.*)", SearchHandler),
(r"/commit/(.*)/(.*)", CommitHandler),
(r"/halloffame", HallOfFameHandler),
(r"/keywords/(.*)", KeywordsHandler),#Obsolete, For testing only
(r"/rss/(.*)", RSSHandler),
])
tornado.options.parse_command_line()
if __name__ == "__main__":
tlog = logging.FileHandler(Config.logfile)
logging.getLogger().addHandler(tlog)
logging.debug('Starting up...')
import socket
if socket.has_ipv6:
application.listen(Config.tornadoport)
else:
application.listen(Config.tornadoport, address="0.0.0.0")
tornado.ioloop.IOLoop.instance().start()