from flask import Flask, request
from cmd import cmd_repl, create_fifo, PIPENAME

app = Flask(__name__)
app.config['DEBUG'] = True

fifo = create_fifo(PIPENAME)

@app.route("/")
def hello():
    msg = """
    <html><head><title>Cheezoid</title></head>
    <body><h3>cheezoid httpd controller</h3>
    RESTful endpoints, POST to send, GET to receive<br>
    For example:<br>
    <code>
    curl -X POST --data 'reset' -H 'Content-type: text/plain' localhost:5000/cmd
    </code>
    or <br>
    <code>
    curl -X POST --data 'reset\n pen down\n move (13, 97)\n pen up' -H 'Content-type: text/plain' localhost:5000/cmd
    </code>
    <ul>
    <li>/status to view cheezoid status</li>
    <li>/cmd to send command</li>
        <ul>
        <li>move (relative angle in degree, relative distance in cm)</li>
        <li>pen (up/down) </li>
        <li>reset</li>
        </ul>
    <li>/svg to send svg</li>
    <li>/canvas to GUI</li>
    </ul>
    </body>
    </html>
"""
    return msg

@app.route("/cmd", methods=['POST', 'GET'])
def cmd_process():
    global fifo
    if request.method == 'GET':
        return "Please use POST\n"
    elif request.method == 'POST':
        cmd_repl(fifo, request.data)
        return "%s\n" % (request.data)

@app.route("/status")
def status_process():
    return "status\n"

@app.route("/svg")
def svg_process():
    return "svg\n"

@app.route("/canvas")
def canvas_process():
    return "canvas"

if __name__ == "__main__":
    app.run(host="0.0.0.0")
    fifo.close()

@@ -73,16 +73,30 @@
 PIPENAME
+%0Aimport socket
 %0A%0Aapp = 
@@ -167,16 +167,48 @@
 IPENAME)
+%0Ahostname = socket.gethostname()
 %0A%0A@app.r
@@ -482,33 +482,26 @@
 text/plain' 
-localhost
+%25s
 :5000/cmd%0A  
@@ -564,16 +564,17 @@
  'reset%5C
+%5C
 n pen do
@@ -576,16 +576,17 @@
 en down%5C
+%5C
 n move (
@@ -597,16 +597,17 @@
 97)%5C
+%5C
 n pen up
 ' -H
@@ -602,16 +602,19 @@
 n pen up
+%5C%5Cn
 ' -H 'Co
@@ -641,17 +641,10 @@
 in' 
-localhost
+%25s
 :500
@@ -1015,16 +1015,39 @@
 turn msg
+ %25 (hostname, hostname)
 %0A%0A@app.r

add hostname substitution