Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Commit

Permalink
First pass, server-side bones stuff only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Young Hahn committed May 27, 2011
1 parent 96184e2 commit 05895ec
Show file tree
Hide file tree
Showing 34 changed files with 1,118 additions and 1,176 deletions.
64 changes: 36 additions & 28 deletions server/bootstrap.js → commands/global.bones
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
// Application bootstrap. Ensures that files directories exist at server start.
var fs = require('fs'),
path = require('path'),
Step = require('step');
Step = require('step'),
settings = Bones.plugin.config;

module.exports = function(app, settings) {
try {
fs.statSync(settings.files);
} catch (Exception) {
console.log('Creating files dir %s', settings.files);
fs.mkdirSync(settings.files, 0777);
}
Bones.Command.options['port'] = {
'title': 'port=[port]',
'description': 'Server port.',
'default': 8889
};

try {
fs.statSync(settings.mapfile_dir);
} catch (Exception) {
console.log('Creating mapfile dir %s', settings.mapfile_dir);
fs.mkdirSync(settings.mapfile_dir, 0777);
Bones.Command.options['files'] = {
'title': 'files=[path]',
'description': 'Path to files directory.',
'default': path.join(process.cwd(), 'files')
};

Bones.Command.options['export'] = {
'title': 'export=[path]',
'description': 'Path to export directory.',
'default': path.join(process.cwd(), 'files', 'export')
};

Bones.Command.augment({
bootstrap: function(parent, plugin, callback) {
parent.call(this, plugin, function() {
bootstrap(callback);
});
}
});

var bootstrap = function(callback) {
try {
fs.statSync(settings.data_dir);
fs.statSync(settings.files);
} catch (Exception) {
console.log('Creating data dir %s', settings.data_dir);
fs.mkdirSync(settings.data_dir, 0777);
console.log('Creating files dir %s', settings.files);
fs.mkdirSync(settings.files, 0777);
}

try {
fs.statSync(settings.export_dir);
fs.statSync(settings.export);
} catch (Exception) {
console.log('Creating export dir %s', settings.export_dir);
fs.mkdirSync(settings.export_dir, 0777);
console.log('Creating export dir %s', settings.export);
fs.mkdirSync(settings.export, 0777);
}

// @TODO: Better infrastructure for handling updates.

// Update 1: Migrate to new backbone-dirty key format.
try {
var db = fs.readFileSync(settings.files + '/app.db', 'utf8');
Expand All @@ -47,13 +57,10 @@ module.exports = function(app, settings) {
} catch (Exception) {}

// Apply server-side mixins/overrides.
var Backbone = require('backbone');
var sync = require('backbone-dirty')(settings.files + '/app.db').sync;
Backbone.sync = sync;
require('models-server');

// Create a default library for the local data directory.
var models = require('models');
var data = new models.Library({
id: 'data',
name: 'Local data',
Expand All @@ -66,16 +73,17 @@ module.exports = function(app, settings) {
function() {
if (!data.get('directory_path')) {
data.save({
'directory_path': path.join(__dirname, '..', 'files', 'data')
directory_path: path.join(settings.files, 'data')
});
}
}
);
// Process any waiting exports.
(new models.ExportList).fetch({success: function(collection) {
(new models.Exports).fetch({success: function(collection) {
collection.each(function(model) {
model.get('status') === 'waiting' && model.process();
});
}});
}
callback();
};

4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env node
process.title = 'tilemill';
require('bones').load(__dirname);
!module.parent && require('bones').start();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions models/Asset.bones
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Asset
// -----
// Model. Single external asset, e.g. a shapefile, image, etc.
model = Backbone.Model.extend({
schema: {
'type': 'object',
'properties': {
'id': {
'type': 'string',
'required': true
},
'url': {
'type': 'string',
'required': true
},
'bytes': {
'type': 'string'
}
}
},
extension: function() {
return this.id.split('.').pop();
}
});

50 changes: 50 additions & 0 deletions models/Assets.bones
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// AssetList
// ---------
// Collection. List of all assets for a given Library. Must be given a
// Library model at `options.library` in order to determine its URL endpoint.
// The REST endpoint for a LibraryList collection must return an array of asset
// models, or may optionally return a more complex object suited for handling
// pagination:
//
// {
// page: 0, // The current page number
// pageTotal: 10, // The total number of pages
// models: [] // An array of asset models
// }
model = Backbone.Collection.extend({
model: models.Asset,
url: function() {
return 'api/Library/' + this.library.id + '/assets/' + this.page;
},
initialize: function(options) {
this.page = 0;
this.pageTotal = 1;
this.library = options.library;
},
parse: function(response) {
if (_.isArray(response)) {
return response;
} else {
this.page = response.page;
this.pageTotal = response.pageTotal;
return response.models;
}
},
hasNext: function() {
return this.page < (this.pageTotal - 1);
},
hasPrev: function() {
return this.page > 0;
},
nextPage: function(options) {
if (!this.hasNext()) return;
this.page++;
this.fetch(options);
},
prevPage: function(options) {
if (!this.hasPrev()) return;
this.page--;
this.fetch(options);
}
});

46 changes: 46 additions & 0 deletions models/AssetsS3.bones
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// AssetListS3
// -----------
// Collection. Override of AssetList for S3 library. S3 uses a marker key
// system for pagination instead of a page # system.
model = models.Assets.extend({
url: function() {
var url = 'api/Library/' + this.library.id + '/assets';
if (this.marker()) {
url += '/' + Base64.encodeURI(this.marker());
}
return url;
},
initialize: function(options) {
this.markers = [];
this.library = options.library;
},
marker: function() {
if (this.markers.length) {
return this.markers[this.markers.length - 1];
}
return false;
},
parse: function(response) {
if (this.marker() != response.marker) {
this.markers.push(response.marker);
}
return response.models;
},
hasNext: function() {
return this.marker();
},
hasPrev: function() {
return this.markers.length > 1;
},
nextPage: function(options) {
if (!this.hasNext()) return;
this.fetch(options);
},
prevPage: function(options) {
if (!this.hasPrev()) return;
this.markers.pop();
this.markers.pop();
this.fetch(options);
}
});

118 changes: 118 additions & 0 deletions models/Datasource.bones
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Datasource (read-only)
// ----------------------
// Model. Inspection metadata about a map layer. Use `fetchFeatures()` to do
// a datasource fetch that includes layer feature objects.
model = Backbone.Model.extend({
// @TODO either as a feature or a bug, object attributes are not set
// automatically when passed to the constructor. We set it manually here.
initialize: function(attributes) {
this.set({'fields': attributes.fields});
},
url: function() {
var url = 'api/Datasource';
this.getFeatures && (url += '/features');
if (typeof module === 'undefined') {
url += '?' + $.param(this.attributes);
}
return url;
},
fetchFeatures: function(options) {
this.getFeatures = true;
this.fetch(options);
}
});

/*
// FileDatasource (read-only)
// --------------------------
// Model. Inspection metadata about a map layer. Use `fetchFeatures()` to do
// a datasource fetch that includes layer feature objects.
var FileDatasource = Datasource.extend({
schema: {
'type': 'object',
'properties': {
'id': {
'type': 'string',
},
'project': {
'required': true,
'type': 'string',
'description': 'Project to which this datasource belongs.'
},
'url': {
'type': 'string',
'required': true,
'minLength': 1,
'title': 'URL',
'description': 'URL of the datasource.'
},
'fields': {
'type': 'object'
},
'features': {
'type': 'array'
},
'ds_options': {
'type': 'object'
},
'ds_type': {
'type': 'string'
},
'geometry_type': {
'type': 'string',
'enum': ['polygon', 'point', 'linestring', 'raster', 'unknown']
}
}
}
});
// PostgisDatasource (read-only)
// -----------------------------
// Model. Inspection metadata about a map layer. Use `fetchFeatures()` to do
// a datasource fetch that includes layer feature objects.
var PostgisDatasource = Datasource.extend({
schema: {
'type': 'object',
'properties': {
'id': {
'type': 'string',
},
'project': {
'required': true,
'type': 'string',
'description': 'Project to which this datasource belongs.'
},
'dbname': {
'type': 'string',
'required': true,
'minLength': 1,
'title': 'Database',
'description': 'Invalid PostGIS database.'
},
'table': {
'type': 'string',
'required': true,
'minLength': 1,
'title': 'Query',
'description': 'Invalid PostGIS query.'
},
'fields': {
'type': 'object'
},
'features': {
'type': 'array'
},
'ds_options': {
'type': 'object'
},
'ds_type': {
'type': 'string'
},
'geometry_type': {
'type': 'string',
'enum': ['polygon', 'point', 'linestring', 'raster', 'unknown']
}
}
}
});
*/
Loading

0 comments on commit 05895ec

Please sign in to comment.