Skip to content

Commit cf68e52

Browse files
authored
Merge pull request #1 from pandermatt/feature/add-wordcloud
2 parents cff61c7 + 438b40f commit cf68e52

14 files changed

+170811
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
application.yml
132+
data/result
133+
application.yml

api/auth.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask_httpauth import HTTPTokenAuth
2+
3+
from api.error_handler import unauthorized_response
4+
from config import config
5+
6+
token_auth = HTTPTokenAuth()
7+
8+
9+
@token_auth.verify_token
10+
def verify_token(token):
11+
try:
12+
return token == config.get_env('API_TOKEN')
13+
except KeyError:
14+
token_auth_error()
15+
16+
17+
@token_auth.error_handler
18+
def token_auth_error():
19+
return unauthorized_response()

api/error_handler.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from werkzeug.exceptions import BadRequest, Unauthorized, NotFound
2+
3+
from util.logger import log
4+
5+
6+
def unauthorized_response():
7+
log.error(f'Unauthorized')
8+
raise Unauthorized()

app.py

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from flask_cors import CORS
55
from flask_restx import Api, Resource
66

7+
from api.auth import token_auth
8+
from config import config
79
from util.logger import log
810

911
app = Flask(__name__)
@@ -22,6 +24,8 @@
2224
}
2325

2426
swagger_ui_enabled = '/'
27+
if config.get_env('PRODUCTION') == 'Y':
28+
swagger_ui_enabled = False
2529

2630
api = Api(app, version='0.0.1', title='HateyBot API',
2731
description='API for HateyBot',
@@ -35,6 +39,7 @@
3539

3640
@auth.route('/')
3741
class AuthHandler(Resource):
42+
@token_auth.login_required
3843
def get(self):
3944
return 'successful'
4045

application.example.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
API_TOKEN: "1234567890"
2+
PRODUCTION: Y

config.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
from os.path import dirname, abspath, join, exists
3+
4+
import yaml
5+
6+
from util.logger import log
7+
8+
9+
def get_or_create(dir_path):
10+
if not exists(dir_path):
11+
os.makedirs(dir_path)
12+
log.info("Creating: " + dir_path)
13+
return dir_path
14+
15+
16+
class Config:
17+
SAVE_TO_FILE = True
18+
19+
__root_dir = dirname(abspath(__file__))
20+
__application_config_path = join(__root_dir, 'application.yml')
21+
__config_file = None
22+
23+
def data_dir(self, folder):
24+
directory = get_or_create(abspath(join(self.__root_dir, 'data')))
25+
return get_or_create(join(directory, folder))
26+
27+
def result_file(self, file_name):
28+
return join(self.data_dir('result'), file_name)
29+
30+
def input_file(self, file_name):
31+
return join(self.data_dir('input'), file_name)
32+
33+
def get_env(self, var):
34+
return self.__get_var(var)
35+
36+
def __get_var(self, var):
37+
if not self.__config_file:
38+
try:
39+
with open(self.__application_config_path, 'r') as stream:
40+
self.__config_file = yaml.safe_load(stream)
41+
log.info("Config Loaded")
42+
except FileNotFoundError:
43+
log.info("Config not found, using ENV Var")
44+
return os.environ.get(var)
45+
try:
46+
return os.environ.get(var) or self.__config_file[var]
47+
except KeyError:
48+
log.error('Can not find ENV var: %s' % var)
49+
50+
51+
config = Config()

0 commit comments

Comments
 (0)