Skip to content

Commit

Permalink
Using new library for serving static files that provides much better …
Browse files Browse the repository at this point in the history
…static file handling and folder listing. Also added new functions to add MimeTypes into the libraries list from Fukiya.
  • Loading branch information
daegalus committed Jun 23, 2013
1 parent 574b1cb commit 52af4e2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.1.6
- Changed static file handling to use the wonderful [library](https://github.com/DanieleSalatti/static-file-handler) created by Daniele Salatti.
- Added functions to be able to add MimeTypes to the StaticFileHandler library from Fukiya.
- Added additional test to make sure static file handling overrode any other paths.

v0.1.5
- Fixing breakage in path.pathSegments. Reverted back to path.split('/')

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Fukiya
Simple framework for making dart server applications easier to write. Long way to go, this is very simple.
Proper documentation will be written when the library matures a bit more.

For a practicle example, look at fukiya_test.dart in the test directory.
For a practical example, look at fukiya_*_test.dart tests in the test directory.

Example usage:
```dart
Expand Down
21 changes: 21 additions & 0 deletions lib/fukiya.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:json' as JSON;
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
import 'package:formler/formler.dart';
import 'package:static_file_handler/static_file_handler.dart';
part 'fukiya_request_handler.dart';
part 'fukiya_router.dart';
part 'fukiya_context.dart';
Expand Down Expand Up @@ -68,6 +69,26 @@ class Fukiya {
void staticFiles(String basePath) {
_router.useStaticFileHandling = true;
_router.staticFilePath = basePath;
_router.staticFileHandler = new StaticFileHandler(basePath);
}

/**
* Adds additional mime types to the static file handler's mime types list in addition to the existing ones.
*/
void addMimeType(String extension, String mimeType) {
if (_router.staticFileHandler != null) {
_router.staticFileHandler.addMIMETypes({ "$extension": mimeType});
}
}

/**
* Adds additional mime types to the static file handler's mime types list in addition to the existing ones.
*/
void addMimeTypes(Map<String, String> mimeTypes) {
if (_router.staticFileHandler != null) {
_router.staticFileHandler.addMIMETypes(mimeTypes);
}
print(_router.staticFileHandler);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions lib/fukiya_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class FukiyaRouter {
List<FukiyaRequestHandler> _routes;
bool useStaticFileHandling;
String staticFilePath;
StaticFileHandler staticFileHandler;

FukiyaRouter() {
_routes = new List<FukiyaRequestHandler>();
useStaticFileHandling = false;
staticFilePath = "";
staticFileHandler = new StaticFileHandler.serveFolder(staticFilePath);
}

void _addRoute(String method, String path, Function handler) {
Expand All @@ -22,17 +24,19 @@ class FukiyaRouter {

FukiyaRequestHandler finalRoute = prioritizeRouter(context, filteredRoutes);

var static = false;
/* if (finalRoute != null) {
finalRoute._handle(context);
} else if (useStaticFileHandling && context.request.method == "GET") {
staticFileHandler.handleRequest(httpRequest);
} else {
context.response.statusCode = HttpStatus.NOT_FOUND;
context.response.close();
}*/
if (useStaticFileHandling && context.request.method == "GET") {
var file = new File(staticFilePath + context.request.uri.path);
file.exists().then((exists) {
if (exists) {
file.readAsBytes().then((value) {
context.response.add(value);
context.response.done.catchError((e) => print("File Response error: ${e}"));
context.response.close();
}, onError:(error) => print(error));

staticFileHandler.handleRequest(context.request);
} else if (finalRoute != null) {
finalRoute._handle(context);
} else {
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fukiya
version: 0.1.5
version: 0.1.6
author: Yulian Kuncheff <yulian@kuncheff.com>
description: A simple framework to improve and simplify the creation of server-side dart applications.
homepage: https://github.com/Daegalus/fukiya
Expand All @@ -8,3 +8,4 @@ dependencies:
unittest: any
logging: any
crypto: any
static_file_handler: any
20 changes: 20 additions & 0 deletions test/fukiya_get_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ class FukiyaGetTests {
},
onDone: () {
expect(finalString, equals("Test"));
});
});
});
atest();
});

test('Simple GET Static File and overrides dynamic get', () {
String finalString = "";
var atest = expectAsync0(() {
client.get("127.0.0.1", 3333, "/something.txt").then((HttpClientRequest request) {
return request.close();

}).then((HttpClientResponse response) {
response.transform(new StringDecoder())
.transform(new LineTransformer())
.listen((String result) {
finalString += result;
},
onDone: () {
expect(finalString, equals("Testing 123;"));
completer.complete(true);
});
});
Expand Down
2 changes: 2 additions & 0 deletions test/fukiya_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void main() {
..post('/:userid', postDynamicHandler)
..post('/postData', postFileDataHandler)
..staticFiles('./test/static')
..addMimeType('ogg', 'video/ogg')
..addMimeTypes({'opus': 'audio/opus', 'mkv': 'video/x-matroska'})
..use(new FukiyaFormParser())
..use(new FukiyaJsonParser())
..listen('127.0.0.1', 3333);
Expand Down

0 comments on commit 52af4e2

Please sign in to comment.