Skip to content

Commit

Permalink
feature: REST API to serve posts from mobile apps or others
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Mar 26, 2017
1 parent 7576965 commit 9f3725d
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/cli/users/utils.js
Expand Up @@ -196,6 +196,7 @@ export function isAbeRestrictedUrl(currentRoute) {
if( currentRoute.indexOf('/abe/users/forgot') > -1
|| currentRoute.indexOf('/abe/users/login') > -1
|| currentRoute.indexOf('/abe/users/reset') > -1
|| currentRoute.indexOf('/abe/rest/') > -1
|| !/^\/abe\//.test(currentRoute)) {
return false
}
Expand Down
54 changes: 29 additions & 25 deletions src/server/controllers/index.js
@@ -1,31 +1,32 @@
import express from 'express'
import * as abe from '../../cli'
import {
postCreate
,postDuplicate
,postUpdate
,getListUrl
,getListWorkflow
,postListUrlSave
,getListHooks
,getMain
,getPage
,postPage
,getGeneratePost
,operations
,getSaveConfig
,postUpload
,postSqlRequest
,postReference
,getReference
,postStructure
,getStructure
,getPaginate
,getThumbs
,getImage
,users
,getHome
,postProfile
postCreate,
postDuplicate,
postUpdate,
getListUrl,
getListWorkflow,
postListUrlSave,
getListHooks,
getMain,
getPage,
postPage,
getGeneratePost,
operations,
getSaveConfig,
postUpload,
postSqlRequest,
postReference,
getReference,
postStructure,
getStructure,
getPaginate,
getThumbs,
getImage,
users,
getHome,
postProfile,
rest
} from '../routes'

import {
Expand All @@ -38,6 +39,9 @@ var router = express.Router()
abeExtend.hooks.instance.trigger('afterHandlebarsHelpers', Handlebars)
abeExtend.hooks.instance.trigger('beforeAddRoute', router)

router.get('/abe/rest/posts*', rest.posts)
router.get('/abe/rest/post*', rest.post)

router.get('/abe/users/forgot', users.getForgot)
router.get('/abe/users/list', users.getList)
router.get('/abe/users/login', users.getLogin)
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-home.js
Expand Up @@ -10,6 +10,13 @@ import {
User
} from '../../cli'

/**
* This route returns the homepage HTML
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next) {
var manager = {}
manager.home = {files: []}
Expand Down
6 changes: 6 additions & 0 deletions src/server/routes/get-image.js
Expand Up @@ -2,6 +2,12 @@ import {
cmsMedia
} from '../../cli'

/**
* This route returns all the images associated with a name. JSON format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
var route = function(req, res){
res.set('Content-Type', 'application/json')
res.send(JSON.stringify(cmsMedia.image.getAssociatedImageFileFromThumb(req.query.name)))
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-list-hooks.js
Expand Up @@ -7,6 +7,13 @@ import {
,coreUtils
} from '../../cli'

/**
* This route returns the hooks list as html
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next) {
var html = ''

Expand Down
8 changes: 8 additions & 0 deletions src/server/routes/get-list-url.js
Expand Up @@ -7,6 +7,14 @@ import {
,config
} from '../../cli'

/**
* This route returns all the available routes in HTML format
* @param {[type]} router [description]
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(router, req, res, next) {
abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
var routes = router.stack
Expand Down
8 changes: 8 additions & 0 deletions src/server/routes/get-list-workflow.js
Expand Up @@ -7,6 +7,14 @@ import {
,config
} from '../../cli'

/**
* this route returns the workflow list in HTML format
* @param {[type]} router [description]
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(router, req, res, next) {
abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
var routes = router.stack
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-main.js
Expand Up @@ -80,6 +80,13 @@ function renderAbeAdmin(EditorVariables, obj, filePath) {
}
}

/**
* This route returns the editor page as HTML
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next) {
var filePath = req.originalUrl.replace('/abe/editor', '')
if (filePath === '' || filePath === '/') {
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-page.js
Expand Up @@ -4,6 +4,13 @@ import {

import pageHelper from '../helpers/page'

/**
* This route returns the post as draft in HTML format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next) {
abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
if(typeof res._header !== 'undefined' && res._header !== null) return
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-paginate.js
Expand Up @@ -4,6 +4,13 @@ import {
Manager
} from '../../cli'

/**
* This route returns filtered list of posts in JSON format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next){
var start = 0
var length = 25
Expand Down
6 changes: 6 additions & 0 deletions src/server/routes/get-reference.js
Expand Up @@ -8,6 +8,12 @@ import {
Handlebars
} from '../../cli'

/**
* This route returns the references files in HTML format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
var route = function(req, res){
var manager = {}
manager.home = {files: []}
Expand Down
7 changes: 7 additions & 0 deletions src/server/routes/get-save-config.js
Expand Up @@ -3,6 +3,13 @@ import {
abeExtend
} from '../../cli'

/**
* This route saves the config en returns result as JSON
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next){
abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
if(typeof res._header !== 'undefined' && res._header !== null) return
Expand Down
6 changes: 6 additions & 0 deletions src/server/routes/get-structure.js
Expand Up @@ -8,6 +8,12 @@ import {
Handlebars
} from '../../cli'

/**
* This route returns the structure as HTML
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
var route = function(req, res){
var manager = {}
manager.home = {files: []}
Expand Down
6 changes: 6 additions & 0 deletions src/server/routes/get-thumbs.js
Expand Up @@ -2,6 +2,12 @@ import {
Manager
} from '../../cli'

/**
* This route returns the list of uploaded images in JSON format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
var route = function(req, res){
res.set('Content-Type', 'application/json')
res.send(JSON.stringify({thumbs: Manager.instance.getThumbsList()}))
Expand Down
4 changes: 3 additions & 1 deletion src/server/routes/index.js
Expand Up @@ -22,6 +22,7 @@ import getImage from './get-image'
import getHome from './get-home'
import * as users from './users'
import * as operations from './operations'
import * as rest from './rest'

export {
getHome,
Expand All @@ -47,5 +48,6 @@ export {
getThumbs,
getImage,
users,
operations
operations,
rest
}
7 changes: 7 additions & 0 deletions src/server/routes/rest/index.js
@@ -0,0 +1,7 @@
import post from './post'
import posts from './posts'

export {
post,
posts
}
42 changes: 42 additions & 0 deletions src/server/routes/rest/post.js
@@ -0,0 +1,42 @@
import path from 'path'
import xss from 'xss'

import {
cmsData,
coreUtils,
abeExtend
} from '../../../cli'

/**
* This route returns the editor page as HTML
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next) {
console.log('Bonne route')
abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
if(typeof res._header !== 'undefined' && res._header !== null) return

var filePath = req.originalUrl.replace('/abe/rest/post', '')

console.log(filePath)
if(filePath !== '' && filePath !== '/' && filePath != null) {
var filePathTest = cmsData.revision.getDocumentRevision(filePath)
if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
var jsonPath = filePathTest.path
console.log(jsonPath)
if(coreUtils.file.exist(jsonPath)) {
var json = cmsData.file.get(jsonPath, 'utf8')
res.set('Content-Type', 'application/json')
res.send(JSON.stringify(json))
}
}
}

// Error

}

export default route
58 changes: 58 additions & 0 deletions src/server/routes/rest/posts.js
@@ -0,0 +1,58 @@
import {
config,
abeExtend,
Manager
} from '../../../cli'

/**
* This route returns filtered list of posts in JSON format
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
var route = function(req, res, next){
var start = 0
var length = 25
var sortField = 'date'
var sortOrder = -1
var search = ''

var values = ['date', 'abe_meta.link', 'abe_meta.template', 'date']
Array.prototype.forEach.call(config.users.workflow, (flow) => {
values[i] = 'abe_meta.' + flow
++i
})

abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
if(typeof res._header !== 'undefined' && res._header !== null) return

if (typeof req.query.start !== 'undefined') {
start = +req.query.start
}

if (typeof req.query.length !== 'undefined') {
length = +req.query.length
}

var i = 4
if (typeof req.query.order !== 'undefined') {
sortField = values[req.query.order[0]['column']]
sortOrder = (req.query.order[0]['dir'] === 'desc')? -1:1
}

if (typeof req.query.search !== 'undefined' && req.query.search.value !== '') {
search = req.query.search.value
}

var list = Manager.instance.getPage(start, length, sortField, sortOrder, search)

if (typeof req.query.draw !== 'undefined') {
list['draw'] = req.query.draw
}

res.set('Content-Type', 'application/json')
res.send(JSON.stringify(list))
}

export default route
12 changes: 1 addition & 11 deletions src/server/views/partials/home.html
Expand Up @@ -118,28 +118,18 @@
<div class="manager-right">
<div class="container">
<div class="jumbotron">
<h1><span class="fa fa-bullhorn"></span> Welcome to AbeCMS <a class="btn btn-lg btn-success" href="/abe/editor" role="button">Go to the manager</a></h1>
<p class="lead" style="font-size: 20px;font-weight: 400;"><br/>

<br/><br/>
<img src="/abecms/image/dark_logo_transparent_background_small.png" style="float: left;margin-right: 30px;">
AbeJS becomes AbeCMS and is now hosted on its own repository in github : <a href="https://github.com/abecms">https://github.com/abecms</a>
<br/>
This brand new version is full of new features and has been redesigned to be more comfortable and easy to use.
<br/><br/><br/>
We hope you'll have as much fun creating your content as we've had working on AbeCMS
<br/><br/>
This page will be your dashboard. We're working hard to cook you awesome widgets... Stay tuned !
</p>
<p></p>
</div>

<div class="tweet-title">
Title tweet abe
</div>

<div class="tweet-subtitle">
Subtitle tweet abe
Last News from @abe_cms team
</div>

<div id="tweets-slider" class="swiper-container">
Expand Down

0 comments on commit 9f3725d

Please sign in to comment.