-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (62 loc) · 3.2 KB
/
app.py
File metadata and controls
81 lines (62 loc) · 3.2 KB
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
import logging
import argparse
from flask import Flask, render_template, jsonify, request
from datetime import datetime
from registration import Registration
from registrator import Registrator
from configuration import Configuration
from myservice import myservice
# default config file (use -c parameter on command line specify a custom config file)
configfile = "app.conf"
# endpoint for Web page containing information about the service
aboutendpoint = "/about"
# endpoint for health information of the service required for Spring Boot Admin server callback
healthendpoint = "/health"
# initialize Flask app and add the externalized service information
app = Flask(__name__)
app.register_blueprint(myservice)
# holds the configuration
configuration = None
@app.route(healthendpoint, methods=['GET'])
def health():
"""required health endpoint for callback of Spring Boot Admin server"""
return "alive"
@app.route(aboutendpoint)
def about():
"""optional endpoint for serving a web page with information about the web service"""
return render_template("about.html", configuration=configuration)
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
# allow configuration of the configfile via command line parameters
argparser = argparse.ArgumentParser(description='You might provide a configuration file, otherwise "%s" is used.' % (configfile) )
argparser.add_argument('-c', '--configfile', action='store', dest='configfile', default=configfile, help='overwrite the default configfile "%s"' % (configfile))
configfile = argparser.parse_args().configfile
configuration = Configuration(configfile, ['springbootadminserverurl', 'springbootadminserveruser', 'springbootadminserverpassword', 'servicehost', 'serviceport', 'servicename', 'servicedescription'])
try:
configuration.serviceport = int(configuration.serviceport) # ensure an int value for the server port
except Exception as e:
logging.error("in configfile '%s': serviceport '%s' is not valid (%s)" % (configfile, configuration.serviceport, e) )
# define metadata that will be shown in the Spring Boot Admin server UI
metadata = {
"start": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"description": configuration.servicedescription,
"about": "%s:%d%s" % (configuration.servicehost, configuration.serviceport, aboutendpoint),
"written in": "Python"
}
# initialize the registation object, to be send to the Spring Boot Admin server
myRegistration = Registration(
name=configuration.servicename,
serviceUrl="%s:%d" % (configuration.servicehost, configuration.serviceport),
healthUrl="%s:%d%s" % (configuration.servicehost, configuration.serviceport, healthendpoint),
metadata=metadata
)
# start a thread that will contact iteratively the Spring Boot Admin server
registratorThread = Registrator(
configuration.springbootadminserverurl,
configuration.springbootadminserveruser,
configuration.springbootadminserverpassword,
myRegistration
)
registratorThread.start()
# start the web service
app.run(debug=True, port=configuration.serviceport)