Skip to content

Commit

Permalink
[cb-#99] Merge branch 'master' into cb-#99
Browse files Browse the repository at this point in the history
  • Loading branch information
crookse committed Dec 27, 2019
2 parents 3cd770f + 50f8cb7 commit b132747
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
1 change: 0 additions & 1 deletion docs/README.md
Expand Up @@ -14,7 +14,6 @@ Add environment variables.

```shell
export DRASH_DIR_ROOT="/path/to/deno-drash"
export DRASH_SERVER_DIRECTORY="/path/to/deno-drash/docs/public"
```

Change to `/docs` directory and install node modules.
Expand Down
19 changes: 4 additions & 15 deletions src/http/response.ts
Expand Up @@ -137,27 +137,16 @@ export default class Response {
* Send the response of a static asset (e.g., a CSS file, JS file, PDF
* file, etc.) to the client making the request.
*
* This method is reliant on the `DRASH_SERVER_DIRECTORY` environment
* variable. The `DRASH_SERVER_DIRECTORY` environment variable MUST point
* to the parent directory of the directory (or list of directories)
* containing static assets. For example, if my project is located at
* `/path/to/my/project` and my CSS files are located at
* `/path/to/my/project/public/assets`, then `DRASH_SERVER_DIRECTORY`
* should be `/path/to/my/project/public`.
* @param string file
* The file that will be served to the client.
*
* @return any
*/
public sendStatic(): any {
const file = this.request.url_path;
// Remove trailing slash
let staticPathParent = Deno.env().DRASH_SERVER_DIRECTORY;
staticPathParent = staticPathParent.replace(/(\/$)/, "");
const fullFilepath = `${staticPathParent}${file}`;

public sendStatic(file): any {
let output = {
status: this.status_code,
headers: this.headers,
body: Deno.readFileSync(fullFilepath)
body: Deno.readFileSync(file)
};

this.request.respond(output);
Expand Down
42 changes: 29 additions & 13 deletions src/http/server.ts
Expand Up @@ -38,6 +38,15 @@ export default class Server {
*/
protected configs: any;

/**
* @description
* A property to hold the location of this server on the filesystem. This
* property is used when resolving static paths.
*
* @property string directory
*/
protected directory: string;

/**
* @description
* A property to hold the Deno server. This property is set in
Expand Down Expand Up @@ -125,6 +134,7 @@ export default class Server {
}

if (configs.static_paths) {
this.directory = configs.directory; // blow up if this doesn't exist
configs.static_paths.forEach(path => {
this.addStaticPath(path);
});
Expand Down Expand Up @@ -204,7 +214,7 @@ export default class Server {
//
let resource = this.getResourceObject(resourceClass, request);
request.resource = resource;
this.logger.debug(
this.logDebug(
"Using `" +
resource.constructor.name +
"` resource class to handle the request."
Expand All @@ -216,13 +226,13 @@ export default class Server {
this.executeMiddlewareBeforeRequest(request, resource);

// Perform the request
this.logger.debug("Calling " + request.method.toUpperCase() + "().");
this.logDebug("Calling " + request.method.toUpperCase() + "().");
response = resource[request.method.toUpperCase()]();

this.executeMiddlewareAfterRequest(request, resource);

// Send the response
this.logger.info("Sending response. " + response.status_code + ".");
this.logDebug("Sending response. " + response.status_code + ".");
return response.send();

} catch (error) {
Expand All @@ -249,14 +259,14 @@ export default class Server {
resource: Drash.Http.Resource = null,
response: Drash.Http.Response = null
): any {
this.logger.debug(
this.logDebug(
`Error occurred while handling request: ${request.method} ${request.url}`
);
this.logger.trace(error.message);
this.logger.trace("Stack trace below:");
this.logger.trace(error.stack);
this.logDebug(error.message);
this.logDebug("Stack trace below:");
this.logDebug(error.stack);

this.logger.trace("Generating generic error response object.");
this.logDebug("Generating generic error response object.");

// If a resource was found, but an error occurred, then that's most likely
// due to the HTTP method not being defined in the resource class;
Expand All @@ -278,7 +288,7 @@ export default class Server {
? error.message
: response.getStatusMessage();

this.logger.info(
this.logDebug(
`Sending response. Content-Type: ${response.headers.get(
"Content-Type"
)}. Status: ${response.getStatusMessageFull()}.`
Expand All @@ -304,8 +314,8 @@ export default class Server {
headers.set("Content-Type", "image/x-icon");
if (!this.trackers.requested_favicon) {
this.trackers.requested_favicon = true;
this.logger.debug("/favicon.ico requested.");
this.logger.debug(
this.logDebug("/favicon.ico requested.");
this.logDebug(
"All future log messages for /favicon.ico will be muted."
);
}
Expand All @@ -329,7 +339,7 @@ export default class Server {
public handleHttpRequestForStaticPathAsset(request): any {
try {
let response = new Drash.Http.Response(request);
return response.sendStatic();
return response.sendStatic(this.directory + "/" + request.url_path);
} catch (error) {
return this.handleHttpRequestError(request, this.httpErrorResponse(404));
}
Expand Down Expand Up @@ -642,7 +652,9 @@ export default class Server {
}
// If the request URL is "/public/assets/js/bundle.js", then we take out
// "/public" and use that to check against the static paths
let requestUrl = `/${request.url.split("/")[1]}`;
let staticPath = request.url.split("/")[1];
// Prefix with a leading slash, so it can be matched properly
let requestUrl = "/" + staticPath;

if (this.static_paths.indexOf(requestUrl) != -1) {
request = Drash.Services.HttpService.hydrateHttpRequest(request, {
Expand All @@ -658,4 +670,8 @@ export default class Server {

return false;
}

protected logDebug(message) {
this.logger.debug("[drash] " + message);
}
}

0 comments on commit b132747

Please sign in to comment.