#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Romain Gauthier <romain.gauthier@masteri2l.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Import from the standard library
from sys import argv, exit
from wsgiref.simple_server import make_server

# Import from mynus
from mynus import Mynus

def start():
    """
    Starts the Mynus application.

    This runs by default a HTTP server on port 8000.
    """
    # Default settings
    host = argv[1] if len(argv) >= 2 else 'localhost'
    port = int(argv[2]) if len(argv) >= 3 else 8000
    mynus_app = Mynus()

    # Running the WSGI server:
    httpd = make_server(host, port, mynus_app)
    print('Serving HTTP on http://%s:%d/ ...' % (host, port))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print('Stop the server gracefully')
        exit()


if __name__ == '__main__':
    start()

