summaryrefslogtreecommitdiff
path: root/index.js
blob: fa6e49568d9e0ee414bfeba625caaeefd75eccc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').load()

var fs = require('fs')
var app, express = require('express')
var http = require('http')
var bodyParser = require('body-parser')
var path = require('path')

var multer = require('multer')
var upload = multer({ dest: 'uploads/' })

var app, server

const db = require('./lib/db')

var site = module.exports = {}

site.init = function(){
  app = express()
  app.use(express.static(path.join(__dirname, './public')))
  app.use(bodyParser.json())
  app.use(bodyParser.urlencoded({ extended: false }))

  app.use(express.query())

  server = http.createServer(app).listen(process.env.PORT, function () {
    console.log('Cortex listening at http://localhost:%s', server.address().port)
  })

  const folders = crud(app, 'folder', db.Folder)
  const files = crud(app, 'file', db.File)
  const jobs = crud(app, 'job', db.Job)
  const tasks = crud(app, 'task', db.Task)

  app.post('/folders/:id', upload.array('file'), function(req, res){
    // move files to data/id
    console.log(req.files)
    req.files.forEach( (file) => {
    })
    res.send(200)
  })

  function crud(app, type_s, model){
    const type = '/' + type_s + 's/'
    const type_id = type + ':id'
  
    const crud = db.crud(type_s, model)
    
    // index
    app.get(type, (req, res) => {
      console.log('index', type)
      crud.index(req.query).then( (data) => {
        console.log(data)
        res.json(data ? data.toJSON() : [])
      })
//       }).catch( () => res.sendStatus(500) )
    })

    // show
    app.get(type_id, (req, res) => {
      console.log('show', type, req.params.id)
      crud.show(req.params.id).then( (data) => {
        console.log(data)
        res.json(data.toJSON())
      })// .catch( (err) => res.sendStatus(500) )
    })

    // create
    app.post(type, (req, res) => {
      console.log('create', type)
      crud.create(req.body).then( (data) => {
        res.json(data.toJSON())
      })// .catch( () => res.sendStatus(500) )
    })

    // update
    app.put(type_id, (req, res) => {
      console.log('update', type, req.params.id)
      crud.update(req.body.id, req.body).then( (data) => {
        res.json(data.toJSON())
      })// .catch( () => res.sendStatus(500) )
    })

    // destroy
    app.delete(type_id, (req, res) => {
      console.log('destroy', type, req.params.id)
      crud.destroy(req.params.id).then( (data) => {
        res.json(data.toJSON())
      })// .catch( () => res.sendStatus(500) )
    })
    
    return crud
  }
}
//   app.get("/p/:id", function(req, res){
//     res.sendFile("index.html", {root: './public'})
//   })
//   app.get("/get/random", function(req, res){
//     db.getRandom().then(function(img){
//       res.json(img)
//     })
//   })
//   app.get("/get/latest", function(req, res){
//     db.getLatest().then(function(img){
//       res.json(img)
//     })
//   })
//   app.get("/get/:id/hotlink", function(req, res){
//     db.getImage(req.params.id).then(function(img){
//       res.redirect(img.url)
//     })
//   })
//   app.get("/get/:id", function(req, res){
//     db.getImage(req.params.id).then(function(img){
//       res.json(img)
//     })
//   })

site.init()