Skip to content

Commit

Permalink
app ui updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dudash committed Aug 14, 2019
1 parent cae6fb7 commit 3ac3312
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 24 deletions.
11 changes: 10 additions & 1 deletion code/app-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ This microservice provides the main user interface into the application.

### Env Vars
- PORT, port to run the service (defaults to 8080 in PROD, 3000 in DEV)
-
- HTTP_PROTOCOL, default='http://'
- SERVICE_NAME, default='app-ui'
- BOARDS_SVC_HOST, default='boards'
- BOARDS_SVC_PORT, default='8080'
- PROFILE_SVC_HOST, default='profile'
- PROFILE_SVC_PORT, default='8080'
- SSO_SVC_HOST, default='auth-sso73-x509'
- SSO_SVC_PORT, default='8443'
- SESSION_SECRET, default='pleasechangeme'

### Local Installation / Run / Test
```bash
$ npm install
Expand Down
24 changes: 24 additions & 0 deletions code/app-ui/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@ var path = require('path')
var cookieParser = require('cookie-parser')
var cors = require('cors')
var debug = require('debug')('app')
var session = require('express-session') //Using cookie-parser may result in issues if the secret is not the same between this module and cookie-parser.
var SSO = require('keycloak-connect') // https://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter

const HTTP_PROTOCOL = process.env.HTTP_PROTOCOL || 'http://'
const SERVICE_NAME = process.env.SERVICE_NAME || 'app-ui'
const BOARDS_SVC_HOST = process.env.BOARDS_SVC_HOST || 'boards'
const BOARDS_SVC_PORT = process.env.BOARDS_SVC_PORT || '8080'
const PROFILE_SVC_HOST = process.env.PROFILE_SVC_HOST || 'profile'
const PROFILE_SVC_PORT = process.env.PROFILE_SVC_PORT || '8080'
const SSO_SVC_HOST = process.env.SSO_SVC_HOST || 'auth-sso73-x509'
const SSO_SVC_PORT = process.env.SSO_SVC_PORT || '8443'
const SESSION_SECRET = process.env.SESSION_SECRET || 'pleasechangeme'

var app = express()
app.use(cors())

// Create a session-store for keycloak middleware.
// Warning! MemoryStore, is purposely not designed for a production environment.
// It will leak memory under most conditions, does not scale past a single
// process, and is meant for debugging and developing.
var memoryStore = new session.MemoryStore();
app.use(session({
secret: SESSION_SECRET, // This is the secret used to sign the session ID cookie
resave: false,
saveUninitialized: true,
store: memoryStore
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
Expand All @@ -30,12 +46,17 @@ app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
var auth = new SSO({store: memoryStore});
app.use(auth.middleware({logout: '/logout'}));

app.use(function(req,res,next) {
req.SERVICE_NAME = SERVICE_NAME
req.debug = debug // pass along debugger for service
req.HTTP_PROTOCOL = HTTP_PROTOCOL
req.BOARDS_SVC_HOST = BOARDS_SVC_HOST
req.BOARDS_SVC_PORT = BOARDS_SVC_PORT
req.PROFILE_SVC_HOST = PROFILE_SVC_HOST
req.PROFILE_SVC_PORT = PROFILE_SVC_PORT
res.locals.ua = req.get('user-agent') // put user agent info into the response data for client side logic
next()
})
Expand All @@ -55,6 +76,9 @@ app.use('/', indexRouter)
app.use('/profile', profileRouter)
app.use('/shared', sharedRouter)
app.use('/:user/board', boardRouter)
//app.use('/:user/dashboard', auth.protect('realm:user'), dashboardRouter)
//app.use('/:user/board', auth.protect('realm:user'), boardRouter) // must be logged in and have 'user' role in the realm. TODO: switch to this when SSO is ready

app.use(function(req, res, next) {
next(createError(404)) // catch 404 and forward to error handler
})
Expand Down
121 changes: 121 additions & 0 deletions code/app-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions code/app-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
"cors": "~2.8.5",
"debug": "^2.6.9",
"express": "~4.16.4",
"express-session": "^1.16.1",
"http-errors": "~1.6.3",
"kafka-node": "^4.0.0",
"keycloak-connect": "^6.0.1",
"moment": "~2.24.0",
"pug": "^2.0.3",
"request": "^2.88.0",
Expand Down
13 changes: 6 additions & 7 deletions code/app-ui/routes/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var request = require('request-promise')
/* GET board's page. */
router.get('/:boardId', function(req, res, next) {
var user = res.locals.user
const boardsGetURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards/' + req.params.boardId
const boardsGetURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards/' + req.params.boardId
req.debug('GET from boards SVC at: ' + boardsGetURI)
var request_get_options = {
method: 'GET',
Expand All @@ -31,7 +31,6 @@ router.get('/:boardId', function(req, res, next) {
var itemsData = []
var itemListPromises = getresult.items.map(function(itemId) { return getItemRequest(req, user, itemId, itemsData)});
Promise.all(itemListPromises).then(function(itemsData) {
req.debug(itemsData)
res.render('board', { title: 'Cut and Paster', board: getresult, items: itemsData, errorWithItems: false })
})
.catch(function (err) {
Expand All @@ -48,7 +47,7 @@ router.get('/:boardId', function(req, res, next) {
})

function getItemRequest(req, user, itemId, itemsData) {
const boardsGetItemURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/items/' + itemId
const boardsGetItemURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/items/' + itemId
req.debug('GET from boards SVC at: ' + boardsGetItemURI)
var request_getitem_options = {
method: 'GET',
Expand Down Expand Up @@ -78,16 +77,16 @@ function getItemRequest(req, user, itemId, itemsData) {
})
}

/* POST (form submission) to add an item to shared items list */
/* POST (form submission) to add an item to board items list */
router.post('/:boardId/paste', function(req, res) {
var pasteData = req.body.pastedata
if (pasteData.length < 1) {
req.debug('ignoring zero length add to shared board')
req.debug('ignoring zero length add to user board')
return
}
var user = res.locals.user
var board = JSON.parse(req.body.board)
const boardsNewItemURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/items'
const boardsNewItemURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/items'
req.debug('POST to boards SVC at: ' + boardsNewItemURI)
var request_post_options = {
method: 'POST',
Expand Down Expand Up @@ -123,7 +122,7 @@ router.post('/:boardId/paste', function(req, res) {
board.items.push(itemId)
}
// req.debug(board)
const boardsUpdateURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards/' + req.params.boardId
const boardsUpdateURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards/' + req.params.boardId
req.debug('PUT to boards SVC at: ' + boardsUpdateURI)
var request_put_options = {
method: 'PUT',
Expand Down
4 changes: 2 additions & 2 deletions code/app-ui/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var request = require('request-promise')
router.get('/', function(req, res, next) {
var userboards = ''
var user = res.locals.user
const boardsURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards'
const boardsURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards'
req.debug('GET from boards SVC at: ' + boardsURI)
var request_options = {
method: 'GET',
Expand Down Expand Up @@ -51,7 +51,7 @@ router.post('/newboard', function(req, res) {
var newBoardIsPrivate = req.body.newboardprivate
var user = res.locals.user
// TODO: validate data
const boardsURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards'
const boardsURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/' + user + '/boards'
req.debug('POST to boards SVC at: ' + boardsURI)
var request_options = {
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions code/app-ui/routes/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var request = require('request-promise')

/* GET shared page. */
router.get('/', function(req, res, next) {
const boardsURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/shareditems'
const boardsURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/shareditems'
req.debug('GET from boards SVC at: ' + boardsURI)
var request_options = {
method: 'GET',
Expand Down Expand Up @@ -43,7 +43,7 @@ router.post('/paste', function(req, res) {
req.debug('ignoring zero length add to shared board')
return
}
const boardsURI = 'http://' + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/shareditems'
const boardsURI = req.HTTP_PROTOCOL + req.BOARDS_SVC_HOST + ':' + req.BOARDS_SVC_PORT + '/shareditems'
req.debug('POST to boards SVC at: ' + boardsURI)
var request_options = {
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion code/app-ui/views/dashboard.pug
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ block content
.album.py-5.bg-light
.container
.row
- var count = Math.min(30, boards.length) // limit the max displayed items
- var count = Math.min(50, boards.length) // limit the max displayed boards
- var iter = 0
while iter < count
if boards[iter].id != null && boards[iter].id.length > 0
Expand Down
Loading

0 comments on commit 3ac3312

Please sign in to comment.