"""
This is the web server that interacts with the tic tac toe engine.
Because it is loosely coupled, if we wanted to swap out
we would just need to implement a .move() that takes a list
and returns a dict with keys board, game_over, and winner
where board is a 3-list of 3-lists, game_over is a bool and
winner is 'X','O', or None
"""

from flask import Flask, request, render_template, send_file, send_from_directory
import json
import toeticer
app = Flask(__name__)

@app.route('/move', methods=['POST'])
def move():
    board = json.loads(request.data)
    new_board = toeticer.move(board)
    resp = app.make_response( json.dumps(new_board) )
    resp.content_type = "application/json"
    return resp

@app.route('/', methods=['GET'])
def index():
    return send_file("index.html")

@app.route('/<path:f>', methods=['GET'])
def source(f):
    return send_from_directory(".", f)

if __name__ == '__main__':
    app.run(debug=True)
