Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for page redirections (v0.02a)

  • Loading branch information...
commit a6c15d240dfc946ce85888d303498467a8df8dc2 1 parent 2929e50
@nathanpc authored
View
3  .gitignore
@@ -1 +1,2 @@
-.DS_Store
+.DS_Store*
+.children
View
6 README.md
@@ -15,6 +15,10 @@ the features of the server. Anyhow, here is how you create a simple server:
void main() {
var Target = new Target();
+
+ Target.redirections = {
+ "none": ""
+ };
File script = new File(new Options().script);
script.directory((Directory d) {
@@ -29,4 +33,4 @@ void main() {
* First working code
* Just a static server
- * Redirects "{path}/" to "{path}/index.html"
+ * Redirects `{path}/` to `{path}/index.html`
View
33 Target.dart
@@ -8,6 +8,8 @@
/** Target server. */
class Target {
+ Map redirections;
+
/**
* Initiate the server instance.
*/
@@ -16,15 +18,38 @@ class Target {
server.listen(location, port);
server.onRequest = (HttpRequest request, HttpResponse response) {
+
print(request.path);
- print("\n==================\n");
+ //print("\n==================\n");
var file;
- if (request.path.endsWith("/")) {
- file = new File("${basePath}/${request.path}index.html");
+ if (redirections.containsKey("none")) {
+ // Is called when the developer hasn't set any page redirections.
+ if (request.path.endsWith("/")) {
+ file = new File("${basePath}/${request.path}index.html");
+ } else {
+ file = new File("${basePath}/${request.path}");
+ }
+ } else if (redirections.containsKey(request.path)) {
+ // Is called if the developer set any page redirections.
+ if (redirections.containsKey(request.path)) {
+ file = new File("${basePath}/${redirections[request.path]}");
+ } else {
+ if (request.path.endsWith("/")) {
+ file = new File("${basePath}/${request.path}index.html");
+ } else {
+ file = new File("${basePath}/${request.path}");
+ }
+ }
} else {
- file = new File("${basePath}/${request.path}");
+ // Is called if the developer set any page redirections and the user
+ // requests a page that isn't included in the redirection.
+ if (request.path.endsWith("/")) {
+ file = new File("${basePath}/${request.path}index.html");
+ } else {
+ file = new File("${basePath}/${request.path}");
+ }
}
file.exists((found) {
View
10 tests/public/test.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+
+<html>
+ <head>
+ <title>test</title>
+ </head>
+ <body>
+ <h1>test</h1>
+ </body>
+</html>
View
25 tests/simple_server.dart
@@ -0,0 +1,25 @@
+// simple_server.dart
+// Main test file, this is where the magic happens
+
+#import("dart:io");
+#import("../Target.dart");
+
+
+void main() {
+ var Target = new Target();
+
+ Target.redirections = {
+ "/test": "test.html",
+ "/hello": "index.html",
+ "/dd": "test/index.html"
+ };
+
+ /*Target.redirections = {
+ "none": ""
+ };*/
+
+ File script = new File(new Options().script);
+ script.directory((Directory d) {
+ Target.createServer(d.path + "/public", 8800);
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.