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']) |
Jonathan Hart | 40a2d13 | 2014-05-12 22:05:14 -0700 | [diff] [blame] | 20 | def return_file(filename="/"): |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 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): |
Jonathan Hart | 40a2d13 | 2014-05-12 22:05:14 -0700 | [diff] [blame] | 53 | url="http://%s:%s/wm/%s" % (RestIP, RestPort, path) |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 54 | try: |
| 55 | response = urlopen(url) |
| 56 | except URLError, e: |
| 57 | print "ONOS REST IF %s has issue. Reason: %s" % (url, e.reason) |
| 58 | result = "" |
| 59 | except HTTPError, e: |
| 60 | print "ONOS REST IF %s has issue. Code %s" % (url, e.code) |
| 61 | result = "" |
| 62 | |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 63 | result = response.read() |
| 64 | return result |
| 65 | |
| 66 | if __name__ == "__main__": |
| 67 | app.debug = True |
| 68 | app.run(threaded=True, host="0.0.0.0", port=9000) |