blob: 14fa05d26c6e888f9143f9c007d3ced2f116bbb4 [file] [log] [blame]
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -07001#! /usr/bin/env python
2import json
3import argparse
4import time
5import re
6from urllib2 import Request, urlopen, URLError, HTTPError
7from flask import Flask, make_response, request
8
9## Global Var for ON.Lab local REST ##
10RestIP="localhost"
11RestPort=8080
12DEBUG=1
13
14app = Flask(__name__)
15
16### Serving Static Files ###
17@app.route('/', methods=['GET'])
18@app.route('/<filename>', methods=['GET'])
19@app.route('/js/<filename>', methods=['GET'])
20def return_file(filename):
21 if request.path == "/":
22 fullpath = "./simple-topo.html"
23 else:
24 fullpath = str(request.path)[1:]
25
26 try:
27 open(fullpath)
28 except:
29 response = make_response("Cannot find a file: %s" % (fullpath), 500)
30 response.headers["Content-type"] = "text/html"
31 return response
32
33 response = make_response(open(fullpath).read())
34 suffix = fullpath.split(".")[-1]
35
36 if suffix == "html" or suffix == "htm":
37 response.headers["Content-type"] = "text/html"
38 elif suffix == "js":
39 response.headers["Content-type"] = "application/javascript"
40 elif suffix == "css":
41 response.headers["Content-type"] = "text/css"
42 elif suffix == "png":
43 response.headers["Content-type"] = "image/png"
44 elif suffix == "svg":
45 response.headers["Content-type"] = "image/svg+xml"
46
47 return response
48
49## Proxying REST calls ###
50@app.route('/wm/', defaults={'path':''})
51@app.route('/wm/<path:path>')
52def rest(path):
53 url="http://localhost:8080/wm/" + path
54 print url
55 try:
56 response = urlopen(url)
57 except URLError, e:
58 print "ONOS REST IF %s has issue. Reason: %s" % (url, e.reason)
59 result = ""
60 except HTTPError, e:
61 print "ONOS REST IF %s has issue. Code %s" % (url, e.code)
62 result = ""
63
64 print response
65 result = response.read()
66 return result
67
68if __name__ == "__main__":
69 app.debug = True
70 app.run(threaded=True, host="0.0.0.0", port=9000)
71# app.run(threaded=False, host="0.0.0.0", port=9000)