Skip to content

Commit

Permalink
Add logging env variable to enable console logs (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriziocarella authored Nov 29, 2023
1 parent 595361a commit 9bf2806
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Env {
DIRECTORY_LISTING?: boolean
HIDE_HIDDEN_FILES?: boolean
DIRECTORY_CACHE_CONTROL?: string
LOGGING?: boolean
}

const units = ['B', 'KB', 'MB', 'GB', 'TB'];
Expand All @@ -33,7 +34,7 @@ function getRangeHeader(range: ParsedRange, fileSize: number): string {
(range.offset + range.length - 1)}/${fileSize}`;
}

// some ideas for this were taken from / inspired by
// some ideas for this were taken from / inspired by
// https://github.com/cloudflare/workerd/blob/main/samples/static-files-from-disk/static.js
async function makeListingResponse(path: string, env: Env, request: Request): Promise<Response | null> {
if (path === "/")
Expand Down Expand Up @@ -162,7 +163,9 @@ export default {
let range: ParsedRange | undefined;

if (!response || !(response.ok || response.status == 304)) {
console.warn("Cache miss");
if (env.LOGGING) {
console.warn("Cache MISS for", request.url);
}
const url = new URL(request.url);
let path = (env.PATH_PREFIX || "") + decodeURIComponent(url.pathname);

Expand Down Expand Up @@ -318,6 +321,10 @@ export default {

if (request.method === "GET" && !range && isCachingEnabled && !notFound)
ctx.waitUntil(cache.put(request, response.clone()));
} else {
if (env.LOGGING) {
console.warn("Cache HIT for", request.url);
}
}

return response;
Expand Down
5 changes: 4 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NOTFOUND_FILE = ""
#NOT_FOUND_FILE = "404.html"

# Enable to show a directory listing fallback on paths ending in /
# If INDEX_FILE is also provided, it will be used instead if the file exists.
# If INDEX_FILE is also provided, it will be used instead if the file exists.
DIRECTORY_LISTING = false

# Enable to hide files or directories beginning with . from directory listings.
Expand All @@ -40,6 +40,9 @@ HIDE_HIDDEN_FILES = false
# Set a cache header here, e.g. "max-age=86400", if you want to cache directory listings.
DIRECTORY_CACHE_CONTROL = "no-store"

# Set debugging log enabled
LOGGING = true

[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "kot" # Set this to your R2 bucket name. Required
Expand Down

0 comments on commit 9bf2806

Please sign in to comment.