<commit_before>express = require "express"
_u = require("underscore")

module.exports = class Server
    constructor: (@core) ->
        
        @server = express.createServer()
        @server.httpAllowHalfOpen = true
        @server.use (req,res,next) => @streamRouter(req,res,next)
        
    #----------
    
    listen: (port) ->
        @server.listen port
        
    #----------
        
    stopListening: ->
        @server.close()
    
    #----------
    
    streamRouter: (req,res,next) ->
        res.removeHeader("X-Powered-By");
    
        # -- default routes -- #
            
        # default URL (and also a mapping for weird stream.nsv route)
        if @core.root_route && (req.url == '/' || req.url == "/;stream.nsv")
            # pretend the request came in on the default stream
            req.url = "/#{@root_route}"
        
        # default playlist
        if @core.root_route && req.url == "/listen.pls"
            req.url = "/#{@root_route}.pls"
        
        # -- utility routes -- #
    
        # index.html
        if req.url == "/index.html"
            res.writeHead 200,
                "content-type": "text/html"
                "connection": "close"
        
            res.end """
                    <html><commit_msg>Fix root_route references in server<commit_after>express = require "express"
_u = require("underscore")

module.exports = class Server
    constructor: (@core) ->
        
        @server = express.createServer()
        @server.httpAllowHalfOpen = true
        @server.use (req,res,next) => @streamRouter(req,res,next)
        
    #----------
    
    listen: (port) ->
        @server.listen port
        
    #----------
        
    stopListening: ->
        @server.close()
    
    #----------
    
    streamRouter: (req,res,next) ->
        res.removeHeader("X-Powered-By");
    
        # -- default routes -- #
            
        # default URL (and also a mapping for weird stream.nsv route)
        if @core.root_route && (req.url == '/' || req.url == "/;stream.nsv")
            # pretend the request came in on the default stream
            req.url = "/#{@core.root_route}"
        
        # default playlist
        if @core.root_route && req.url == "/listen.pls"
            req.url = "/#{@core.root_route}.pls"
        
        # -- utility routes -- #
    
        # index.html
        if req.url == "/index.html"
            res.writeHead 200,
                "content-type": "text/html"
                "connection": "close"
        
            res.end """
                    <html>
{'fraction_non_alphanumeric': 0.1563467492260062, 'fraction_numerical': 0.0023219814241486067, 'mean_word_length': 1.3781048758049679, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5085849', 'n_tokens_mistral': 756, 'n_tokens_neox': 743, 'n_words': 155}