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: |
Jonathan Hart | 42a0854 | 2014-05-30 23:08:31 -0700 | [diff] [blame] | 57 | errorString = "Could not access ONOS resource %s: %s" % (url, e.reason) |
| 58 | print errorString |
| 59 | return errorString, 500 |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 60 | except HTTPError, e: |
Jonathan Hart | 42a0854 | 2014-05-30 23:08:31 -0700 | [diff] [blame] | 61 | errorString = "Error during ONOS resource request %s: %s" % (url, e.code) |
| 62 | print errorString |
| 63 | return errorString, e.code |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 64 | |
Masayoshi Kobayashi | e64d44b | 2014-04-23 11:40:02 -0700 | [diff] [blame] | 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) |