First commit
This commit is contained in:
commit
58d5d0b2e7
6 changed files with 132 additions and 0 deletions
65
.gitignore
vendored
Normal file
65
.gitignore
vendored
Normal file
|
@ -0,0 +1,65 @@
|
|||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
|
||||
|
||||
*.iml
|
||||
|
||||
## Directory-based project format:
|
||||
.idea/
|
||||
# if you remove the above rule, at least ignore the following:
|
||||
|
||||
# User-specific stuff:
|
||||
# .idea/workspace.xml
|
||||
# .idea/tasks.xml
|
||||
# .idea/dictionaries
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
# .idea/dataSources.ids
|
||||
# .idea/dataSources.xml
|
||||
# .idea/sqlDataSources.xml
|
||||
# .idea/dynamic.xml
|
||||
# .idea/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
# .idea/gradle.xml
|
||||
# .idea/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
# .idea/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.ipr
|
||||
*.iws
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
### Dart template
|
||||
# Don’t commit the following directories created by pub.
|
||||
.buildlog
|
||||
.pub/
|
||||
build/
|
||||
packages
|
||||
.packages
|
||||
|
||||
# Or the files created by dart2js.
|
||||
*.dart.js
|
||||
*.js_
|
||||
*.js.deps
|
||||
*.js.map
|
||||
|
||||
# Include when developing application packages.
|
||||
pubspec.lock
|
||||
|
29
README.md
Normal file
29
README.md
Normal file
|
@ -0,0 +1,29 @@
|
|||
# angel_static
|
||||
Static server middleware for Angel.
|
||||
|
||||
# Installation
|
||||
In `pubspec.yaml`:
|
||||
|
||||
dependencies:
|
||||
angel_framework: ^0.0.0-dev
|
||||
angel_static: ^1.0.0-beta
|
||||
|
||||
# Usage
|
||||
As with all Angel middleware, this can be used simply via a function
|
||||
call within the route declaration, or registered under a name and invoked
|
||||
under that same name.
|
||||
|
||||
```dart
|
||||
import 'dart:io';
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_static/angel_static.dart';
|
||||
|
||||
main() async {
|
||||
Angel angel = new Angel();
|
||||
angel.registerMiddleware("static", serveStatic(new Directory("build/web")));
|
||||
angel.get("*", "static");
|
||||
|
||||
await angel.startServer(InternetAddress.LOOPBACK_IP_V4, 8080);
|
||||
}
|
||||
```
|
||||
|
26
lib/angel_static.dart
Normal file
26
lib/angel_static.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
library angel_static;
|
||||
|
||||
import 'dart:io';
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:mime/mime.dart' show lookupMimeType;
|
||||
|
||||
/// Serves files statically from a given directory.
|
||||
Middleware serveStatic(Directory sourceDirectory) {
|
||||
return (RequestContext req, ResponseContext res) async {
|
||||
String requested = req.path.replaceAll(new RegExp(r'^\/'), '');
|
||||
File file = new File.fromUri(
|
||||
sourceDirectory.absolute.uri.resolve(requested));
|
||||
if (await file.exists()) {
|
||||
res
|
||||
..willCloseItself = true
|
||||
..header(HttpHeaders.CONTENT_TYPE, lookupMimeType(file.path))
|
||||
..status(200);
|
||||
await res.streamFile(file);
|
||||
await res.underlyingResponse.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
11
pubspec.yaml
Normal file
11
pubspec.yaml
Normal file
|
@ -0,0 +1,11 @@
|
|||
name: angel_static
|
||||
description: Static server middleware for Angel.
|
||||
homepage: https://github.com/angel-dart/angel_static
|
||||
author: thosakwe <thosakwe@gmail.com>
|
||||
version: 1.0.0-beta
|
||||
dependencies:
|
||||
angel_framework: ^0.0.0-dev
|
||||
mime: ^0.9.3
|
||||
dev_dependencies:
|
||||
http: ^0.11.3
|
||||
test: ^0.12.13
|
0
test/all_tests.dart
Normal file
0
test/all_tests.dart
Normal file
1
test/sample.txt
Normal file
1
test/sample.txt
Normal file
|
@ -0,0 +1 @@
|
|||
Hello world
|
Loading…
Reference in a new issue