Files
gancio/server/api/index.js

106 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
const express = require('express')
const multer = require('multer')
2019-12-10 22:29:36 +01:00
const cors = require('cors')()
2019-06-06 23:54:32 +02:00
2020-01-27 00:47:03 +01:00
const { isAuth, isAdmin, hasPerm } = require('./auth')
2019-04-03 00:25:12 +02:00
const eventController = require('./controller/event')
const exportController = require('./controller/export')
const userController = require('./controller/user')
const settingsController = require('./controller/settings')
2019-12-04 01:18:05 +01:00
const instanceController = require('./controller/instance')
const apUserController = require('./controller/ap_user')
const resourceController = require('./controller/resource')
const oauthController = require('./controller/oauth')
2019-05-30 12:04:14 +02:00
const storage = require('./storage')
2019-04-03 00:25:12 +02:00
const upload = multer({ storage })
2019-07-23 01:31:43 +02:00
2019-09-11 19:12:24 +02:00
const debug = require('debug')('api')
2019-08-25 14:34:26 +02:00
const api = express.Router()
api.use(express.urlencoded({ extended: false }))
api.use(express.json())
2019-06-06 23:54:32 +02:00
2020-01-27 00:47:03 +01:00
api.get('/user', isAuth, (req, res) => res.json(res.locals.oauth.token.user))
// api.post('/user/login', userController.login)
// api.get('/user/logout', userController.logout)
2019-04-03 00:25:12 +02:00
api.post('/user/recover', userController.forgotPassword)
api.post('/user/check_recover_code', userController.checkRecoverCode)
api.post('/user/recover_password', userController.updatePasswordWithRecoverCode)
2019-06-18 14:45:04 +02:00
// register and add users
api.post('/user/register', userController.register)
2019-10-30 14:58:40 +01:00
api.post('/user', isAdmin, userController.create)
2019-06-18 14:45:04 +02:00
// update user
2020-01-27 00:47:03 +01:00
api.put('/user', hasPerm('user:update'), userController.update)
2019-04-03 00:25:12 +02:00
2019-09-11 19:12:24 +02:00
// delete user
2019-10-30 14:58:40 +01:00
api.delete('/user/:id', isAdmin, userController.remove)
2020-01-27 00:47:03 +01:00
api.delete('/user', hasPerm('user:remove'), userController.remove)
2019-06-18 15:13:13 +02:00
2019-04-03 00:25:12 +02:00
// get all users
2019-10-30 14:58:40 +01:00
api.get('/users', isAdmin, userController.getAll)
2019-04-03 00:25:12 +02:00
// update a place (modify address..)
2019-10-30 14:58:40 +01:00
api.put('/place', isAdmin, eventController.updatePlace)
2019-04-03 00:25:12 +02:00
2019-06-18 14:45:04 +02:00
// add event
2019-10-30 14:58:40 +01:00
api.post('/user/event', upload.single('image'), userController.addEvent)
2019-09-11 19:12:24 +02:00
2019-06-18 14:45:04 +02:00
// update event
2020-01-27 00:47:03 +01:00
api.put('/user/event', hasPerm('event:write'), upload.single('image'), userController.updateEvent)
2019-04-03 00:25:12 +02:00
// remove event
2020-01-27 00:47:03 +01:00
api.delete('/user/event/:id', hasPerm('event:remove'), userController.delEvent)
2019-04-03 00:25:12 +02:00
// get tags/places
api.get('/event/meta', eventController.getMeta)
// get unconfirmed events
2019-10-30 14:58:40 +01:00
api.get('/event/unconfirmed', isAdmin, eventController.getUnconfirmed)
2019-04-03 00:25:12 +02:00
2020-01-27 00:47:03 +01:00
// add event notification TODO
2019-04-03 00:25:12 +02:00
api.post('/event/notification', eventController.addNotification)
api.delete('/event/notification/:code', eventController.delNotification)
api.get('/settings', settingsController.getAllRequest)
2019-10-30 14:58:40 +01:00
api.post('/settings', isAdmin, settingsController.setRequest)
2020-01-15 23:51:09 +01:00
api.post('/settings/favicon', isAdmin, multer({ dest: 'thumb/' }).single('favicon'), settingsController.setFavicon)
2019-04-03 00:25:12 +02:00
2020-01-27 00:47:03 +01:00
// confirm event
api.get('/event/confirm/:event_id', hasPerm('event:write'), eventController.confirm)
api.get('/event/unconfirm/:event_id', hasPerm('event:write'), eventController.unconfirm)
2019-04-03 00:25:12 +02:00
2019-07-04 01:20:32 +02:00
// get event
2019-12-10 22:29:36 +01:00
api.get('/event/:event_id.:format?', cors, eventController.get)
2019-07-04 01:20:32 +02:00
2019-04-03 00:25:12 +02:00
// export events (rss/ics)
2019-12-10 22:29:36 +01:00
api.get('/export/:type', cors, exportController.export)
2019-04-03 00:25:12 +02:00
// get events in this range
2020-01-21 01:24:10 +01:00
// api.get('/event/:month/:year', cors, eventController.getAll)
2020-01-15 23:51:09 +01:00
api.get('/event', cors, eventController.select)
2019-04-03 00:25:12 +02:00
2019-12-04 01:18:05 +01:00
api.get('/instances', isAdmin, instanceController.getAll)
api.get('/instances/:instance_domain', isAdmin, instanceController.get)
api.post('/instances/toggle_block', isAdmin, instanceController.toggleBlock)
api.post('/instances/toggle_user_block', isAdmin, apUserController.toggleBlock)
api.put('/resources/:resource_id', isAdmin, resourceController.hide)
api.delete('/resources/:resource_id', isAdmin, resourceController.remove)
api.get('/resources', isAdmin, resourceController.getAll)
2019-10-30 15:01:15 +01:00
2020-01-27 00:47:03 +01:00
api.get('/clients', hasPerm('oauth:read'), oauthController.getClients)
api.get('/client/:client_id', hasPerm('oauth:read'), oauthController.getClient)
api.post('/client', oauthController.createClient)
2020-01-27 00:47:03 +01:00
api.use((req, res) => res.sendStatus(404))
2019-09-11 19:12:24 +02:00
// Handle 500
api.use((error, req, res, next) => {
2020-01-21 01:24:10 +01:00
debug(error.toString())
2019-09-11 19:12:24 +02:00
res.status(500).send('500: Internal Server Error')
})
2019-04-03 00:25:12 +02:00
module.exports = api