# Compile with p. command line switch (see docs).

import time

http = require ('http')

class Demo:
    texts = (
        'Welcome to the world of node.js',
        'You can have your cake and eat it',
        'Use node\'s ecosystem while programming in Python',
        'Using node.js from Transcrypt is easy',
        'Take a Python ride into the node.js world'
    )

    def __init__ (self, port):
        print ('Demo server started on port', port)
        self.server = http.createServer (self.serve)
        self.server.listen (port)
        self.oldIndex = 0
        self.newIndex = 0
        self.count = 0
        
    def serve (self, request, response): 
        time.__adapt__ (request)
    
        response.writeHead (200)
        
        print ('Serving page', self.count)
        self.count += 1
        
        while self.newIndex == self.oldIndex:
            self.newIndex = int (Math.random () * len (self.texts))
        self.oldIndex = self.newIndex
        
        response.end ('<h1>{}</h1><h1>{}</h1>'.format (
            self.texts [self.newIndex], time.localtime ()
        ))

demo = Demo (process.argv [2])  #This is where node puts its command line params
