-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from PyreStudios/brad/42
Start on static file access
- Loading branch information
Showing
9 changed files
with
179 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
sidebar_position: 5 | ||
--- | ||
|
||
# Static Assets | ||
|
||
Steward also offers static asset handlers for serving files that do not change based on server requests. Commonly, image files, html pages, and more are served as static assets. If you are using view templating, those templates are _not_ static assets and should not be served this way. | ||
|
||
```dart | ||
final router = Router(); | ||
router.staticFiles('/static'); | ||
``` | ||
|
||
Additionally, you can use middleware with static file handling. | ||
|
||
```dart | ||
final router = Router(); | ||
router.staticFiles('/private-assets', middleware: [RequireAuthMiddleware]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
|
||
<title>A random asset</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Here's an asset</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
app: | ||
name: My Steward App | ||
port: 4040 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:steward/router/router.dart'; | ||
|
||
class StaticBinding extends RouteBinding { | ||
StaticBinding({required super.path, super.middleware}) | ||
: super(verb: HttpVerb.Get); | ||
|
||
static final imageExtensions = ['png', 'jpeg', 'jpg', 'webp', 'gif']; | ||
static final textExtensions = ['css', 'csv', 'html', 'xml']; | ||
|
||
@override | ||
Future<Response> process(Request request) async { | ||
var assetPath = '$path${request.request.uri.path.split(path).last}'; | ||
var assetType = 'text/html'; | ||
|
||
try { | ||
final extension = assetPath.split('.').last; | ||
if (imageExtensions.contains(extension)) { | ||
assetType = 'image/$extension'; | ||
} | ||
if (textExtensions.contains(extension)) { | ||
assetType = 'text/$extension'; | ||
} else { | ||
// TODO: this is probably to vague | ||
assetType = 'application/$extension'; | ||
} | ||
} on StateError { | ||
// if there is no extension, assume text/html. | ||
assetType = 'text/html'; | ||
} | ||
|
||
if (assetType == 'text/html' && !assetPath.contains('.')) { | ||
assetPath = '$assetPath.html'; | ||
} | ||
|
||
try { | ||
final asset = File('./$assetPath'); | ||
final contents = await asset.readAsString(); | ||
final response = Response(200, body: contents); | ||
final splitAssetType = assetType.split('/'); | ||
final primaryType = splitAssetType.first; | ||
final subType = splitAssetType.last; | ||
response.headers.contentType = ContentType(primaryType, subType); | ||
|
||
return response; | ||
} catch (e) { | ||
return Response.NotFound(); | ||
} | ||
} | ||
|
||
@override | ||
bool get isPrefixBinding => true; | ||
} |
Oops, something went wrong.