<commit_before>express = require 'express'
config = require 'config'
git = require 'git-rev'
jade = require 'jade'
http = require 'http'

server = express()

for key of config
    val = config[key]
    if typeof val == 'function'
        continue
    console.log "#{key}: #{val}"

serverPort = config.server_port or 9001
delete config.server_port

console.log "Listening on port #{serverPort}"

git.short (commitId) ->
    config.git_commit_id = commitId

STATIC_URL = config.static_path
ALLOWED_URLS = [
    /^\/$/
    /^\/unit\/\d+\/?$/,
    /^\/service\/\d+\/?$/,
    /^\/search\/$/,
    /^\/address\/[^\/]+\/[^\/]+$/
]

staticFileHelper = (fpath) ->
    STATIC_URL + fpath

requestHandler = (req, res, next) ->
    match = false
    for pattern in ALLOWED_URLS
        if req.path.match pattern
            match = true
            break
    if not match
        next()
        return

    vars =
        configJson: JSON.stringify config
        config: config
        staticFile: staticFileHelper
        pageMeta: req._context or {}
        siteName:
            fi: 'Pääkaupunkiseudun palvelukartta'
            sv: 'Servicekarta'
            en: 'Service Map'

    res.render 'home.jade', vars

embeddedHandler = (req, res, next) ->
    # TODO: enable
    # match = false
    # for pattern in ALLOWED_URLS
    #     if req.path.match pattern
    #         match = true
    #         break
    # if not match
    #     next()
    #     return

    vars =
        configJson: JSON.stringify config
        config: config
        staticFile: staticFileHelper
        pageMeta: req._context or {}
        siteName:
            fi: 'Pääkaupunkiseudun palvelukartta'
            sv: 'Servicekarta'
            en: 'Service Map'

    res.render 'embed.jade', vars

handleUnit = (req, res, next) ->
    pattern = /^\/(\d+)\/?$/
    r = req.path.match pattern
    if not r or r.length < 2
        res.redirect config.urlPrefix
        return

    unitId = r[1]
    url = config.service_map_backend + '/unit/' + unitId + '/'
    unitInfo = null

    sendResponse = ->
        if unitInfo
            context =
                title: unitInfo.name.fi
                description: unitInfo.description
                picture: unitInfo.picture_url
                url: req.protocol + '://' + req.get('host') + req.originalUrl
        else
            context = null
        req._context = context
        next()

    timeout = setTimeout sendResponse, 2000

    http.get url, (httpResp) ->
        respData = ''
        httpResp.on 'data', (data) ->
            respData += data
        httpResp.on 'end', ->
            unitInfo = JSON.parse respData
            clearTimeout timeout
            sendResponse()

server.configure ->
    staticDir = __dirname + '/../static'
    @locals.pretty = true
    @engine '.jade', jade.__express

    if false
        # Setup request logging
        @use (req, res, next) ->
            console.log '%s %s', req.method, req.url
            next()<commit_msg>Handle missing units in node server.<commit_after>express = require 'express'
config = require 'config'
git = require 'git-rev'
jade = require 'jade'
http = require 'http'

server = express()

for key of config
    val = config[key]
    if typeof val == 'function'
        continue
    console.log "#{key}: #{val}"

serverPort = config.server_port or 9001
delete config.server_port

console.log "Listening on port #{serverPort}"

git.short (commitId) ->
    config.git_commit_id = commitId

STATIC_URL = config.static_path
ALLOWED_URLS = [
    /^\/$/
    /^\/unit\/\d+\/?$/,
    /^\/service\/\d+\/?$/,
    /^\/search\/$/,
    /^\/address\/[^\/]+\/[^\/]+$/
]

staticFileHelper = (fpath) ->
    STATIC_URL + fpath

requestHandler = (req, res, next) ->
    match = false
    for pattern in ALLOWED_URLS
        if req.path.match pattern
            match = true
            break
    if not match
        next()
        return

    vars =
        configJson: JSON.stringify config
        config: config
        staticFile: staticFileHelper
        pageMeta: req._context or {}
        siteName:
            fi: 'Pääkaupunkiseudun palvelukartta'
            sv: 'Servicekarta'
            en: 'Service Map'

    res.render 'home.jade', vars

embeddedHandler = (req, res, next) ->
    # TODO: enable
    # match = false
    # for pattern in ALLOWED_URLS
    #     if req.path.match pattern
    #         match = true
    #         break
    # if not match
    #     next()
    #     return

    vars =
        configJson: JSON.stringify config
        config: config
        staticFile: staticFileHelper
        pageMeta: req._context or {}
        siteName:
            fi: 'Pääkaupunkiseudun palvelukartta'
            sv: 'Servicekarta'
            en: 'Service Map'

    res.render 'embed.jade', vars

handleUnit = (req, res, next) ->
    pattern = /^\/(\d+)\/?$/
    r = req.path.match pattern
    if not r or r.length < 2
        res.redirect config.urlPrefix
        return

    unitId = r[1]
    url = config.service_map_backend + '/unit/' + unitId + '/'
    unitInfo = null

    sendResponse = ->
        if unitInfo and unitInfo.name
            context =
                title: unitInfo.name.fi
                description: unitInfo.description
                picture: unitInfo.picture_url
                url: req.protocol + '://' + req.get('host') + req.originalUrl
        else
            context = null
        req._context = context
        next()

    timeout = setTimeout sendResponse, 2000

    http.get url, (httpResp) ->
        if httpResp.statusCode != 200
            clearTimeout timeout
            sendResponse()
            return

        respData = ''
        httpResp.on 'data', (data) ->
            respData += data
        httpResp.on 'end', ->
            unitInfo = JSON.parse respData
            clearTimeout timeout
            sendResponse()

server.configure ->
    staticDir = __dirname + '/../static'
    @locals.pretty = true
    @engine '.jade', jade.__express

    if false
        # Setup request logging
        @use (req, res, next) ->
            console.log '%s %s', req.method, req.url
            next()
{'fraction_non_alphanumeric': 0.11855586274190925, 'fraction_numerical': 0.0037404456009107172, 'mean_word_length': 1.7553763440860215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '197491', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1827, 'n_words': 489}