Skip to content

Commit

Permalink
add support for deno serve
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jun 2, 2024
1 parent 0ff0698 commit 90f12b1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project try to adheres to [Semantic Versioning](https://semver.org/).
Go to the `v1` branch to see the changelog of Lume 1.

## [2.2.1] - Unreleased
### Added
- Allow to run a server with `deno serve -A _config.ts`.

### Fixed
- Port detection in `lume cms` command.
- Show an error when trying to copy a file from outside the src folder [#610].
Expand Down
35 changes: 34 additions & 1 deletion core/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import FSWatcher from "../core/watcher.ts";
import { FSWriter } from "./writer.ts";
import { Page } from "./file.ts";
import textLoader from "./loaders/text.ts";
import type { Loader } from "./loaders/mod.ts";
import Server from "./server.ts";
import notFound from "../middlewares/not_found.ts";

import type { Loader } from "./loaders/mod.ts";
import type { Component, Components } from "./component_loader.ts";
import type { Data, RawData, StaticFile } from "./file.ts";
import type { Engine, Helper, HelperOptions } from "./renderer.ts";
Expand Down Expand Up @@ -132,6 +134,8 @@ export default class Site {
/** The static files to be copied are stored here */
readonly files: StaticFile[] = [];

fetch: Deno.ServeHandler;

constructor(options: Partial<SiteOptions> = {}) {
this.options = merge(defaults, options);

Expand Down Expand Up @@ -208,6 +212,17 @@ export default class Site {
// Ignore the dest folder by the watcher
this.options.watcher.ignore.push(normalizePath(this.options.dest));
this.fs.options.ignore = this.options.watcher.ignore;

// Create the fetch function for `deno serve`
let fetchServer: Server | undefined;

this.fetch = (request: Request, info: Deno.ServeHandlerInfo) => {
if (!fetchServer) {
fetchServer = this.server();
}

return fetchServer.handle(request, info);
};
}

get globalData(): RawData {
Expand Down Expand Up @@ -581,6 +596,24 @@ export default class Site {
});
}

/** Returns a server */
server(): Server {
const { port, page404, middlewares } = this.options.server;
const root = this.options.server.root || this.dest();
const server = new Server({ root, port });

server.use(notFound({
root,
page404,
}));

if (middlewares) {
server.use(...middlewares);
}

return server;
}

/**
* Internal function to render pages
* The common operations of build and update
Expand Down

0 comments on commit 90f12b1

Please sign in to comment.