Skip to content

Commit 340decd

Browse files
authored
Route caching bug fix, simplify routing (#763)
* move window.apiPrefix into Angular service * avoid routing by accept headers, integrate api service * Revert "Fixed over-matching of index route." This reverts commit 62e1225. * better name for index template file
1 parent cc7c6f8 commit 340decd

File tree

19 files changed

+143
-103
lines changed

19 files changed

+143
-103
lines changed

Gruntfile.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
// var config = require('insight-config.json');
4+
35
module.exports = function(grunt) {
46

57
//Load NPM tasks
@@ -10,10 +12,27 @@ module.exports = function(grunt) {
1012
grunt.loadNpmTasks('grunt-markdown');
1113
grunt.loadNpmTasks('grunt-macreload');
1214
grunt.loadNpmTasks('grunt-angular-gettext');
15+
grunt.loadNpmTasks('grunt-replace');
1316

1417
// Project Configuration
1518
grunt.initConfig({
1619
pkg: grunt.file.readJSON('package.json'),
20+
replace: {
21+
dist: {
22+
options: {
23+
patterns: [
24+
{
25+
match: 'INSIGHT_API_PREFIX',
26+
replacement: '<%= pkg.insightConfig.apiPrefix %>'
27+
}
28+
],
29+
usePrefix: false
30+
},
31+
files: [
32+
{src: ['public/src/templates/api.js'], dest: 'public/src/js/services/api.js'}
33+
]
34+
}
35+
},
1736
concat: {
1837
options: {
1938
process: function(src, filepath) {
@@ -117,13 +136,13 @@ module.exports = function(grunt) {
117136
grunt.option('force', true);
118137

119138
//Default task(s).
120-
grunt.registerTask('default', ['watch']);
139+
grunt.registerTask('default', ['replace', 'watch']);
121140

122141
//Update .pot file
123142
grunt.registerTask('translate', ['nggettext_extract']);
124143

125144
//Compile task (concat + minify)
126-
grunt.registerTask('compile', ['nggettext_compile', 'concat', 'uglify', 'cssmin']);
145+
grunt.registerTask('compile', ['replace', 'nggettext_compile', 'concat', 'uglify', 'cssmin']);
127146

128147

129148
};

README.md

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,42 @@ Open a web browser to `http://localhost:3001/insight/`
2323

2424
## Development
2525

26-
To run Insight UI locally in development mode:
26+
To build Insight UI locally:
2727

28-
Install bower dependencies:
28+
```
29+
$ npm run build
30+
```
31+
32+
A watch task is also available:
2933

3034
```
31-
$ bower install
35+
$ npm run watch
3236
```
3337

34-
To compile and minify the web application's assets:
38+
## Changing routePrefix and apiPrefix
3539

40+
By default, the `insightConfig` in `package.json` is:
41+
42+
```json
43+
"insightConfig": {
44+
"apiPrefix": "insight-api",
45+
"routePrefix": "insight"
46+
}
3647
```
37-
$ grunt compile
48+
49+
To change these routes, first make your changes to `package.json`, for example:
50+
51+
```json
52+
"insightConfig": {
53+
"apiPrefix": "api",
54+
"routePrefix": ""
55+
}
3856
```
3957

40-
There is a convenient Gruntfile.js for automation during editing the code
58+
Then rebuild the `insight-ui` service:
4159

4260
```
43-
$ grunt
61+
$ npm run build
4462
```
4563

4664
## Multilanguage support

bitcore-node/index.js

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,24 @@
33
var BaseService = require('./service');
44
var inherits = require('util').inherits;
55
var fs = require('fs');
6+
var pkg = require('../package');
67

78
var InsightUI = function(options) {
89
BaseService.call(this, options);
9-
if (typeof options.apiPrefix !== 'undefined') {
10-
this.apiPrefix = options.apiPrefix;
11-
} else {
12-
this.apiPrefix = 'insight-api';
13-
}
14-
if (typeof options.routePrefix !== 'undefined') {
15-
this.routePrefix = options.routePrefix;
16-
} else {
17-
this.routePrefix = 'insight';
18-
}
10+
// we don't use the options object for routePrefix and apiPrefix, since the
11+
// client must be rebuilt with the proper options. A future version of
12+
// Bitcore should allow for a service "build" step to make this better.
13+
this.apiPrefix = pkg.insightConfig.apiPrefix;
14+
this.routePrefix = pkg.insightConfig.routePrefix;
1915
};
2016

2117
InsightUI.dependencies = ['insight-api'];
2218

2319
inherits(InsightUI, BaseService);
2420

2521
InsightUI.prototype.start = function(callback) {
26-
27-
var self = this;
28-
29-
var indexFile = __dirname + '/../public/index.html';
30-
31-
fs.readFile(indexFile, { encoding: 'utf8' }, function(err, data) {
32-
33-
if(err) {
34-
return callback(err);
35-
}
36-
37-
self.indexFile = self.filterIndexHTML(data);
38-
callback();
39-
});
22+
this.indexFile = this.filterIndexHTML(fs.readFileSync(__dirname + '/../public/index-template.html', {encoding: 'utf8'}));
23+
setImmediate(callback);
4024
};
4125

4226
InsightUI.prototype.getRoutePrefix = function() {
@@ -45,26 +29,19 @@ InsightUI.prototype.getRoutePrefix = function() {
4529

4630
InsightUI.prototype.setupRoutes = function(app, express) {
4731
var self = this;
48-
49-
app.use('/', function(req, res, next){
50-
51-
if (req.url === '/') {
52-
res.send(self.indexFile);
53-
} else {
54-
express.static(__dirname + '/../public')(req, res, next);
55-
}
56-
32+
app.use(express.static(__dirname + '/../public'));
33+
// if not in found, fall back to indexFile (404 is handled client-side)
34+
app.use(function(req, res, next) {
35+
res.setHeader('Content-Type', 'text/html');
36+
res.send(self.indexFile);
5737
});
5838
};
5939

6040
InsightUI.prototype.filterIndexHTML = function(data) {
61-
var transformed = data
62-
.replace(/apiPrefix = '\/api'/, "apiPrefix = '/" + this.apiPrefix + "'");
63-
64-
if (this.routePrefix) {
65-
transformed = transformed.replace(/<base href=\"\/\"/, '<base href="/' + this.routePrefix + '/"');
41+
var transformed = data;
42+
if (this.routePrefix !== '') {
43+
transformed = transformed.replace('<base href="/"', '<base href="/' + this.routePrefix + '/"');
6644
}
67-
6845
return transformed;
6946
};
7047

package.json

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@
3939
"front-end"
4040
],
4141
"bitcoreNode": "bitcore-node",
42-
"dependencies": {
42+
"insightConfig": {
43+
"apiPrefix": "insight-api",
44+
"routePrefix": "insight"
4345
},
46+
"scripts": {
47+
"build": "bower install && grunt compile",
48+
"watch": "grunt"
49+
},
50+
"dependencies": {},
4451
"devDependencies": {
45-
"bower": "~1.2.8",
52+
"bower": "~1.8.0",
4653
"grunt": "~0.4.2",
54+
"grunt-angular-gettext": "^0.2.15",
4755
"grunt-cli": "~0.1.11",
48-
"grunt-contrib-jshint": "~0.8.0",
4956
"grunt-contrib-concat": "~0.3.0",
57+
"grunt-contrib-jshint": "~0.8.0",
5058
"grunt-contrib-uglify": "~0.3.2",
5159
"grunt-contrib-watch": "*",
52-
"grunt-macreload": "*",
5360
"grunt-css": "~0.5.4",
61+
"grunt-macreload": "*",
5462
"grunt-markdown": "~0.5.0",
55-
"grunt-angular-gettext": "^0.2.15"
63+
"grunt-replace": "^1.0.1"
5664
}
5765
}

public/index.html renamed to public/index-template.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<meta name="fragment" content="!">
99
<title data-ng-bind="$root.title + $root.titleDetail + ' | Insight'">Insight</title>
1010
<meta name="keywords" content="bitcoins, transactions, blocks, address, block chain, best block, mining difficulty, hash serialized">
11-
<meta name="description" content="Bitcoin Insight. View detailed information on all bitcoin transactions and block. {{ $root.title + $root.titleDetail }}">
11+
<meta name="description" content="Bitcoin Insight. View detailed information on all bitcoin transactions and blocks.">
1212
<link rel="shortcut icon" href="img/icons/favicon.ico" type="image/x-icon">
1313
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700,400italic">
1414
<link rel="stylesheet" href="css/main.min.css">
@@ -66,7 +66,6 @@ <h3 class="modal-title">Scan Code</h3>
6666
<a class="insight m10v pull-right" target="_blank" href="http://insight.is">insight <small>API v{{version}}</small></a>
6767
</div>
6868
</div>
69-
<script language="javascript">window.apiPrefix = '/api';</script>
7069
<script src="/socket.io/socket.io.js"></script>
7170
<script src="js/vendors.min.js"></script>
7271
<script src="js/angularjs-all.min.js"></script>

public/js/angularjs-all.min.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/main.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/js/vendors.min.js

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/src/js/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ angular.module('insight',[
1515
'angularMoment',
1616
'insight.system',
1717
'insight.socket',
18+
'insight.api',
1819
'insight.blocks',
1920
'insight.transactions',
2021
'insight.address',
@@ -27,6 +28,7 @@ angular.module('insight',[
2728

2829
angular.module('insight.system', []);
2930
angular.module('insight.socket', []);
31+
angular.module('insight.api', []);
3032
angular.module('insight.blocks', []);
3133
angular.module('insight.transactions', []);
3234
angular.module('insight.address', []);

public/src/js/controllers/messages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('insight.messages').controller('VerifyMessageController',
4-
function($scope, $http) {
4+
function($scope, $http, Api) {
55
$scope.message = {
66
address: '',
77
signature: '',
@@ -22,7 +22,7 @@ angular.module('insight.messages').controller('VerifyMessageController',
2222
$scope.verify = function() {
2323
$scope.verification.status = 'loading';
2424
$scope.verification.address = $scope.message.address;
25-
$http.post(window.apiPrefix + '/messages/verify', $scope.message)
25+
$http.post(Api.apiPrefix + '/messages/verify', $scope.message)
2626
.success(function(data, status, headers, config) {
2727
if(typeof(data.result) != 'boolean') {
2828
// API returned 200 but result was not true or false

0 commit comments

Comments
 (0)