<commit_before>###

Copyright (c) 2011-2012  Voicious

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/>.

###

Http        = require 'http'
Express     = require 'express'
{Schema}    = require 'jugglingdb'
Fs          = require 'fs'

Config      = require '../common/config'

class Api
    constructor     : () ->
        @app            = do Express
        @db             = new Schema 'mongodb', {
            connector   : 'mongodb'
            database    : 'testdb'
        }
        @models         = []
        @configured     = false
    
    defineGet       : (model) =>
        @app.get '/api/' + model, (req, res) =>
            res.set 'Access-Control-Allow-Origin', '*'
            # TODO set filters, expands etc.
            if req.query
                objs = {}
                for k, v of req.query
                    objs[k] = v
                try
                    @db.models[model].all {where: objs}, (err, objs) =>
                        if objs?
                            res.json objs
                        else
                            res.json []
                catch e
                    res.send 400
            else
                @db.models[model].all (err, all) =>
                    res.json all
        @app.get '/api/' + model + '/:id', (req, res) =>
            res.set 'Access-Control-Allow-Origin', '*'
            try
                @db.models[model].find req.params.id, (err, obj) =>
                    if obj?
                        res.json obj
                    else
                        res.send 404
            catch e
                res.send 400

    defineDelete    : (model) =>
        @app.delete '/api/' + model + '/:id', (req, res) =>
            res.set 'Access-Control-Allow-Origin', '*'
            try
                @db.models[model].find req.params.id, (err, obj) =>
                    if obj?
                        obj.destroy () =>
                            res.send 200
                    else
                        res.send 404
            catch e
                res.send 400
    updateOrCreate  : (model, req, res) =>
        obj = new @models[model] req.body
        obj.isValid (valid) =>
            if valid
                @models[model].updateOrCreate obj
                res.json obj
            else
                res.send 400

    definePost      : (model) =>
        @app.post '/api/' + model, (req, res) =>
            res.set 'Access-Control-Allow-Origin', '*'
            @updateOrCreate model, req, res

    definePut       : (model) =>
        @app.put '/api/' + model + '/:id', (req, res) =>
            res.set 'Access-Control-Allow-Origin', '*'
            @updateOrCreate model, req, res

    defineAllRoutes : () =>
        ressources  = []
        for model of @db.models
            ressources.push model
            @defineGet model
            @definePost model
            @defineDelete model
            @definePut model
        @app.get '/api', (req, res) => res.json ressources

    defineAllModels : () =>
        # TODO replace with path variable
        modelsPath  = '../models'
        modelsNames = Fs.readdirSync modelsPath
        for modelName in modelsNames
            if (modelName.split '.')[1] == "js"
                {
                    ModelDef
                    AfterModelDef
                }   = require '../models/' + modelName
                name            = (modelName.split '.')[0]
                @models[name]   = @db.define name, ModelDef
                AfterModelDef @models[name]

    configure       : () =>
        @app.set 'port', 8173
        @app.use Express.logger 'dev'
        @app.use do Express.bodyParser
        @app.use do Express.methodOverride
        @app.use @app.router<commit_msg>dynamically set Access-Control-Allow-Origin according to configuration in REST API<commit_after>###

Copyright (c) 2011-2012  Voicious

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/>.

###

Http        = require 'http'
Express     = require 'express'
{Schema}    = require 'jugglingdb'
Fs          = require 'fs'

Config      = require '../common/config'

class Api
    constructor     : () ->
        @app            = do Express
        @db             = new Schema 'mongodb', {
            connector   : 'mongodb'
            database    : 'testdb'
        }
        @models         = []
        @configured     = false
    
    defineGet       : (model) =>
        @app.get '/api/' + model, (req, res) =>
            # TODO set filters, expands etc.
            if req.query
                objs = {}
                for k, v of req.query
                    objs[k] = v
                try
                    @db.models[model].all {where: objs}, (err, objs) =>
                        if objs?
                            res.json objs
                        else
                            res.json []
                catch e
                    res.send 400
            else
                @db.models[model].all (err, all) =>
                    res.json all
        @app.get '/api/' + model + '/:id', (req, res) =>
            try
                @db.models[model].find req.params.id, (err, obj) =>
                    if obj?
                        res.json obj
                    else
                        res.send 404
            catch e
                res.send 400

    defineDelete    : (model) =>
        @app.delete '/api/' + model + '/:id', (req, res) =>
            try
                @db.models[model].find req.params.id, (err, obj) =>
                    if obj?
                        obj.destroy () =>
                            res.send 200
                    else
                        res.send 404
            catch e
                res.send 400
    updateOrCreate  : (model, req, res) =>
        obj = new @models[model] req.body
        obj.isValid (valid) =>
            if valid
                @models[model].updateOrCreate obj
                res.json obj
            else
                res.send 400

    definePost      : (model) =>
        @app.post '/api/' + model, (req, res) =>
            @updateOrCreate model, req, res

    definePut       : (model) =>
        @app.put '/api/' + model + '/:id', (req, res) =>
            @updateOrCreate model, req, res

    defineAllRoutes : () =>
        ressources  = []
        for model of @db.models
            ressources.push model
            @defineGet model
            @definePost model
            @defineDelete model
            @definePut model
        @app.get '/api', (req, res) => res.json ressources

    defineAllModels : () =>
        # TODO replace with path variable
        modelsPath  = '../models'
        modelsNames = Fs.readdirSync modelsPath
        for modelName in modelsNames
            if (modelName.split '.')[1] == "js"
                {
                    ModelDef
                    AfterModelDef
                }   = require '../models/' + modelName
                name            = (modelName.split '.')[0]
                @models[name]   = @db.define name, ModelDef
                AfterModelDef @models[name]

    configure       : () =>
        @app.set 'port', Config.RestAPI.Port
        @app.use Express.logger 'dev'
        @app.use do Express.bodyParser
        @app.use do Express.methodOverride
        @app.use (req, res, next) ->
            if req.headers.origin?
                if req.headers.origin in Config.RestAPI.AllowedHosts or
                '*' in Config.RestAPI.AllowedHosts
                    res.set 'Access-Control-Allow-Origin', req.headers.origin
            do next
        @app.use @app.router
{'fraction_non_alphanumeric': 0.10059171597633136, 'fraction_numerical': 0.007737824305871643, 'mean_word_length': 1.2917861799217731, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5406472', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2232, 'n_words': 719}