Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions core/CrimsonLib.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
// Use of this source code is governed by a MIT style licence
// can be found in the LICENSE file.
// website:
library crimson_core;

import '../../log4dart/lib/log4dart.dart';
import 'dart:io';
import 'dart:uri';
import 'dart:isolate' as isolate;

#library("crimson:core");

#import('../../log4dart/LogLib.dart');
#import("dart:io");
#import("dart:uri");
#import("dart:isolate", prefix:"isolate");

#source('crimson.dart');
#source('crimson_impl.dart');
#source('crimson_utils.dart');
#source('crimsonPrivate.dart');
#source('crimsonHttpServer.dart');
#source('crimsonModule.dart');
part 'crimson.dart';
part 'crimson_impl.dart';
part 'crimson_utils.dart';
part 'crimsonPrivate.dart';
part 'crimsonHttpServer.dart';
part 'crimsonModule.dart';
106 changes: 105 additions & 1 deletion core/crimson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,111 @@ interface CrimsonEndpoint extends CrimsonHandler {

}

interface CrimsonData extends Map {
interface CrimsonData<K, V> extends Map<K, V>
default _CrimsonData<K, V> {
/** Constructor with default implementation */
CrimsonData();

}

/** An default implementation */
class _CrimsonData<K, V> implements CrimsonData {
/** a delegate that implements a given methods */
Map<K, V> _delegate;

_CrimsonData() {
_delegate = new Map();
}

/**
* Returns whether this map contains the given [value].
*/
bool containsValue(V value) {
return _delegate.containsValue(value);
}

/**
* Returns whether this map contains the given [key].
*/
bool containsKey(K key) {
return _delegate.containsKey(key);
}

/**
* Returns the value for the given [key] or null if [key] is not
* in the map. Because null values are supported, one should either
* use containsKey to distinguish between an absent key and a null
* value, or use the [putIfAbsent] method.
*/
V operator [](K key) {
return _delegate[key];
}

/**
* Associates the [key] with the given [value].
*/
void operator []=(K key, V value) {
_delegate[key] = value;
}

/**
* If [key] is not associated to a value, calls [ifAbsent] and
* updates the map by mapping [key] to the value returned by
* [ifAbsent]. Returns the value in the map.
*/
V putIfAbsent(K key, V ifAbsent()) {
_delegate.putIfAbsent(key, ifAbsent);
}

/**
* Removes the association for the given [key]. Returns the value for
* [key] in the map or null if [key] is not in the map. Note that values
* can be null and a returned null value does not always imply that the
* key is absent.
*/
V remove(K key) {
_delegate.remove(key);
}

/**
* Removes all pairs from the map.
*/
void clear() {
_delegate.clear();
}

/**
* Applies [f] to each {key, value} pair of the map.
*/
void forEach(void f(K key, V value)) {
_delegate.forEach(f);
}

/**
* Returns a collection containing all the keys in the map.
*/
Collection<K> getKeys() {
return _delegate.getKeys();
}

/**
* Returns a collection containing all the values in the map.
*/
Collection<V> getValues() {
return _delegate.getValues();
}

/**
* The number of {key, value} pairs in the map.
*/
int get length => _delegate.length;

/**
* Returns true if there is no {key, value} pair in the map.
*/
bool isEmpty() {
return _delegate.isEmpty();
}

}

Expand Down
45 changes: 16 additions & 29 deletions core/crimsonHttpServer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
class _CrimsonHttpServer implements CrimsonHttpServer {

/// Contains the [Logger]
Logger logger;

Logger logger;

/// Constructor
_CrimsonHttpServer([HttpServer httpServer]) :
Expand All @@ -26,9 +25,7 @@ class _CrimsonHttpServer implements CrimsonHttpServer {
// start the server listening
_httpServer.listen(host, port);
print("Listening on ${host}:${port}");
}


}

/// This is the core of method of [CrimsonHttpServer]
/// It first loops through each of the [filters] in turn, calling handle
Expand All @@ -54,51 +51,41 @@ class _CrimsonHttpServer implements CrimsonHttpServer {
module = modules["*"];
}

res.persistentConnection = true; //we need this line for nginx proxy.
if (module != null) {
try {
Future<CrimsonData> handled = module.handle(req,res);
handled.then((result) {
res.outputStream.close();
try {
res.outputStream.close();
}
catch (ex, stack) {
print("${ex}, ${stack}");
}
});
handled.handleException((error) {
res.outputStream.close();
_httpErrorHandler(error);
return true; //this consider a given error to be handled.
});
}
catch (var ex, var stack) {
catch (ex, stack) {
logger.error("${ex}, ${stack}");
}
}
else {
logger.debug("404, not found");
res.statusCode = HttpStatus.NOT_FOUND;
res.outputStream.close();
}

}

}
}

_httpErrorHandler(Exception error) {
print("we invocked http error handler");
this.logger.error(error.toString());
//CrimsonHttpException ex = new CrimsonHttpException(HttpStatus.INTERNAL_SERVER_ERROR, error);
//_crimsonErrorHandler(ex, null, null);
}

// _crimsonErrorHandler(CrimsonHttpException error, HttpRequest req, HttpResponse res) {
// res.statusCode = error.status;
// res.setHeader("Content-Type", "text/plain");
// res.outputStream.writeString("CrimsonHttp: Error"
// "${error}"
// "Method: ${req.method}: ${req.uri}");
// res.outputStream.close();
// //TODO: If an error handler filter has been registered, then use that.
// //Might be better to have an errorHandler collection in the same
// //way that we have filters and endpoints.
// }


}

LinkedHashMap<String,CrimsonModule> get modules() => _modules;
LinkedHashMap<String,CrimsonModule> get modules => _modules;

final HttpServer _httpServer;
final LinkedHashMap<String, CrimsonModule> _modules;
Expand Down
Loading