blob: b47de6b4f514300c0e0e171421cc31d7938ae9e8 [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'])
Jonathan Hart40a2d132014-05-12 22:05:14 -070020def return_file(filename="/"):
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -070021 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):
Jonathan Hart40a2d132014-05-12 22:05:14 -070053 url="http://%s:%s/wm/%s" % (RestIP, RestPort, path)
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -070054 try:
55 response = urlopen(url)
56 except URLError, e:
Jonathan Hart42a08542014-05-30 23:08:31 -070057 errorString = "Could not access ONOS resource %s: %s" % (url, e.reason)
58 print errorString
59 return errorString, 500
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -070060 except HTTPError, e:
Jonathan Hart42a08542014-05-30 23:08:31 -070061 errorString = "Error during ONOS resource request %s: %s" % (url, e.code)
62 print errorString
63 return errorString, e.code
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -070064
Masayoshi Kobayashie64d44b2014-04-23 11:40:02 -070065 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)