Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | import json |
| 3 | import argparse |
| 4 | import time |
| 5 | import re |
| 6 | from urllib2 import Request, urlopen, URLError, HTTPError |
| 7 | from flask import Flask, make_response, request |
| 8 | |
| 9 | ## Global Var for ON.Lab local REST ## |
| 10 | RestIP="localhost" |
| 11 | RestPort=8080 |
| 12 | DEBUG=1 |
| 13 | |
| 14 | app = 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']) |
| 20 | def 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>') |
| 52 | def 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 | |
| 68 | if __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) |