forked from ydguala/env-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.py
80 lines (57 loc) · 1.67 KB
/
api-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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python3
# IMPORTS
import json
import os
import sys
from datetime import datetime
from flask import *
# FUNCTIONS
# These functions will get the first 3 args and set them in order to app_name,var2 and message
def set_args_as_vars():
var_list = []
for v in sys.argv[1:]:
var_list.append(v)
return var_list
def get_env_var_value(var):
if var == 'HOSTNAME':
try:
env_var = os.uname()[1]
except KeyError:
env_var = "UNKNOWN"
print("{} not found".format(var))
else:
try:
env_var = os.environ[var]
except KeyError:
env_var = "UNKNOWN"
return env_var
def create_variables_dictionary(variables_list):
vars_dict = {}
for var in variables_list:
vars_dict[var] = get_env_var_value(var)
print(str(vars_dict))
return vars_dict
# GLOBAL VARS
server_port = 8080
server_host = "0.0.0.0"
api_server = Flask('api-server')
# needed to set next vars
global variables_list
global variables_dict
variables_list = set_args_as_vars()
variables_dict = create_variables_dictionary(variables_list)
# FLASK FUNCTIONS, needed to be defined after params
@api_server.route('/', methods=['GET'])
def root():
json_response = json.dumps(variables_dict)
return json_response
@api_server.route('/status', methods=['GET'], strict_slashes=False)
def status():
response = {'status' : 'ok', 'time' : datetime.now().strftime("%Y-%m-%dT%H:%M:%S") }
json_response = json.dumps(response)
return json_response
def main():
api_server.run(host=server_host,port=server_port)
# EXECUTION
if __name__ == "__main__":
main()