forked from mguijarr/xrf_webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (55 loc) · 1.82 KB
/
app.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
import os
import sys
import optparse
import bottle
import socket
#import logging
import json
import helper
# patch socket module;
# by default bottle doesn't set address as reusable
# and there is no option to do it...
socket.socket._bind = socket.socket.bind
def my_socket_bind(self, *args, **kwargs):
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return socket.socket._bind(self, *args, **kwargs)
socket.socket.bind = my_socket_bind
@bottle.route('/')
def main():
return serve_static_file("app.html")
@bottle.route("/<url:path>")
def serve_static_file(url):
return bottle.static_file(url, os.path.dirname(__file__))
@bottle.post("/calculate")
def do_calculation():
user_input = json.load(bottle.request.body)
print("received:", user_input)
# we have received attenuators, peaks, beam_energy,
# multilayer, materials, detector, incoming_angle, outgoing_angle
text = "The answer is 42."
try:
text = helper.getMultilayerFluorescence(user_input)
except:
text = ("ERROR: %s" % sys.exc_info()[1])
return { "text": text }
def serve_forever(port=None):
bottle.run(host="0.0.0.0", port=port)
if __name__ == "__main__":
usage = "usage: \%prog [-p<port>]"
parser = optparse.OptionParser(usage)
parser.add_option(
'-p', '--port', dest='port', type='int',
help='Port to listen on (default 8099)', default=8099, action='store')
options, args = parser.parse_args()
#logging.basicConfig()
#logging.getLogger().setLevel(logging.DEBUG)
#print(len(sys.argv))
if len(sys.argv) > 1:
pid=os.getpid()
pidfile = sys.argv[1] #"/var/run/fisxserver.pid"
if os.path.exists(pidfile):
os.remove(pidfile)
f = open(pidfile, "w")
f.write("%d" % pid)
f.close()
serve_forever(options.port)