-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import tornado.web | ||
from data_watchtower.utils import json_dumps | ||
|
||
|
||
class BaseHandler(tornado.web.RequestHandler): | ||
def initialize(self, *args, **kwargs): | ||
self.database = self.settings.get('database') | ||
|
||
@staticmethod | ||
def json_dumps(data): | ||
return json_dumps(data) | ||
|
||
def json(self, data=None, error=None): | ||
if data is None: | ||
data = [] | ||
if error is None: | ||
error = dict( | ||
err_code=0, | ||
err_msg="", | ||
) | ||
data = { | ||
'data': data, | ||
'error': error, | ||
} | ||
self.write(self.json_dumps(data)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from .base import BaseHandler | ||
|
||
|
||
class WatchtowerHandler(BaseHandler): | ||
def get(self): | ||
name = self.get_argument('name') | ||
wt = self.database.get_watchtower(name) | ||
if not wt: | ||
self.json(error={'err_code': 1001, 'err_msg': 'watchtower not found'}) | ||
return | ||
return self.json(wt.to_dict()) | ||
|
||
|
||
class WatchtowerListHandler(BaseHandler): | ||
def get(self): | ||
data = self.database.get_watchtowers() | ||
self.json(data) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import asyncio | ||
|
||
import tornado.web | ||
|
||
from tornado.options import define, options | ||
from data_watchtower import DbServices | ||
from data_watchtower.api.url import URLS | ||
|
||
default_db_url = os.getenv('DW_DB_URL', "sqlite:///data.db") | ||
define("port", default=8888, type=int, help="port to listen on") | ||
define("db_url", default=default_db_url, help="db url") | ||
|
||
|
||
def make_app(): | ||
database = DbServices(options.db_url) | ||
app = tornado.web.Application( | ||
URLS, | ||
database=database | ||
) | ||
return app | ||
|
||
|
||
async def main(): | ||
options.parse_command_line() | ||
app = make_app() | ||
app.listen(options.port) | ||
await asyncio.Event().wait() | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from .handlers import watchtower | ||
|
||
URLS = [ | ||
(r"/", watchtower.WatchtowerHandler), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters