2 * Copyright 2013, 2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 var express = require('express');
17 var fs = require("fs");
18 var events = require("./events");
19 var path = require("path");
21 var icon_paths = [path.resolve(__dirname + '/../public/icons')];
23 var settings; // settings has to be global, otherwise variable not in scope for express
25 events.on("node-icon-dir",function(dir) {
26 icon_paths.push(path.resolve(dir));
30 function setupUI(_settings,app) {
34 // Need to ensure the url ends with a '/' so the static serving works
35 // with relative paths
36 app.get("/",function(req,res) {
37 if (req.originalUrl.slice(-1) != "/") {
38 res.redirect(req.originalUrl+"/");
45 //TODO: create a default icon
46 var defaultIcon = path.resolve(__dirname + '/../public/icons/arrow-in.png');
48 app.get("/icons/:icon",function(req,res) {
49 if (iconCache[req.params.icon]) {
50 res.sendfile(iconCache[req.params.icon]); // if not found, express prints this to the console and serves 404
52 for (var p=0;p<icon_paths.length;p++) {
53 var iconPath = path.join(icon_paths[p],req.params.icon);
54 if (fs.existsSync(iconPath)) {
55 res.sendfile(iconPath);
56 iconCache[req.params.icon] = iconPath;
60 res.sendfile(defaultIcon);
64 app.get("/settings", function(req,res) {
66 httpNodeRoot: settings.httpNodeRoot,
67 version: settings.version
69 res.json(safeSettings);
72 app.use("/",express.static(__dirname + '/../public'));
77 module.exports = setupUI;