Skip to content

Commit

Permalink
Merge pull request #35 from zazuko/enable-logs
Browse files Browse the repository at this point in the history
Enable logs
  • Loading branch information
ludovicm67 authored May 21, 2024
2 parents 046c1eb + d247546 commit 50095c6
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/sour-humans-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"varnish-post": minor
---

It is now possible to enable logs, by setting `ENABLE_LOGS` to `true`, which is now the default value.
To disable them, just put any other value, like `false` for example.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ENV VARNISH_SIZE="100M"
ENV DISABLE_ERROR_CACHING="true"
ENV DISABLE_ERROR_CACHING_TTL="30s"
ENV CONFIG_FILE="default.vcl"
ENV ENABLE_LOGS="true"

# Install some dependencies
RUN apt-get update \
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ You can use following environment variables for configuration:
- `DISABLE_ERROR_CACHING`: disable the caching of errors (default: `true`)
- `DISABLE_ERROR_CACHING_TTL`: time where requests should be directly sent to the backend after an error occured (default: `30s`)
- `CONFIG_FILE`: the name of the configuration file to use (default: `default.vcl`)
- `ENABLE_LOGS`: enable logs (default: `true`)
4 changes: 4 additions & 0 deletions config/default.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ sub vcl_backend_fetch {
if (bereq.http.X-Body-Len) {
set bereq.method = "POST";
}

return (fetch);
}

# Indicate whether the response was served from cache or not
Expand All @@ -88,4 +90,6 @@ sub vcl_deliver {
} else {
set resp.http.X-Cache = "MISS";
}

return (deliver);
}
11 changes: 9 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/sh

ENABLE_LOGS="${ENABLE_LOGS}"

set -eu

# environment variables substitution
# Environment variables substitution
for SRC_LOCATION in $(find /templates -type f); do
DST_LOCATION=$(echo "${SRC_LOCATION}" | sed 's/^\/templates/\/etc\/varnish/')
envsubst \
Expand All @@ -11,9 +13,14 @@ for SRC_LOCATION in $(find /templates -type f); do
echo "INFO: generated '${DST_LOCATION}' from '${SRC_LOCATION}' (environment variables substitution)"
done

# Display logs if configured
if [ "${ENABLE_LOGS}" = "true" ]; then
varnishncsa&
fi

set -x

# run varnish
# Run Varnish
varnishd \
-F \
-f "/etc/varnish/${CONFIG_FILE}" \
Expand Down
41 changes: 35 additions & 6 deletions test/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@fastify/formbody": "^7.4.0",
"fastify": "^4.27.0"
}
}
17 changes: 10 additions & 7 deletions test/app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import fastify from 'fastify';
import fastify from "fastify";
import fastifyFormbody from "@fastify/formbody";

// fetch values from environment variables
const port = process.env.SERVER_PORT || 8080;
const host = process.env.SERVER_HOST || '::';
const host = process.env.SERVER_HOST || "::";

// init fastify
const server = fastify({
logger: true,
});

server.register(fastifyFormbody);

// default route
server.all('/', async () => ({
hello: 'world',
server.all("/", async () => ({
hello: "world",
time: Date.now(),
}));

Expand All @@ -20,9 +23,9 @@ server.all<{
Params: {
code: number;
};
}>('/error/:code', async (request, reply) => {
}>("/error/:code", async (request, reply) => {
reply.code(request.params.code).send({
hello: 'error',
hello: "error",
time: Date.now(),
code: request.params.code,
});
Expand All @@ -33,7 +36,7 @@ server.all<{
Params: {
name: string;
};
}>('/:name', async (request) => ({
}>("/:name", async (request) => ({
hello: request.params.name,
time: Date.now(),
}));
Expand Down
30 changes: 30 additions & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,44 @@ info "Check if we can purge a cache entry…"
sleep 3
# do a request after TTL to invalidate the cache
res_tmp=$(fetch_time "${CACHED_ENDPOINT}")
res_tmp=$(fetch_time "${CACHED_ENDPOINT}/cached")
res_tmp=$(fetch_time "${CACHED_ENDPOINT}/purged")
res1=$(fetch_time "${CACHED_ENDPOINT}")
res1_1=$(fetch_time "${CACHED_ENDPOINT}/cached")
res1_2=$(fetch_time "${CACHED_ENDPOINT}/purged")
sleep 1
curl -sL -X PURGE "${CACHED_ENDPOINT}" >/dev/null
curl -sL -X PURGE "${CACHED_ENDPOINT}/purged" >/dev/null
res2=$(fetch_time "${CACHED_ENDPOINT}")
res2_1=$(fetch_time "${CACHED_ENDPOINT}/cached")
res2_2=$(fetch_time "${CACHED_ENDPOINT}/purged")
if [ "${res1}" -eq "${res2}" ]; then
error "cache was not purged"
fi
if [ "${res1_1}" -ne "${res2_1}" ]; then
error "cache was purged"
fi
if [ "${res1_2}" -eq "${res2_2}" ]; then
error "cache was not purged"
fi


info "Check if we can purge a cache entry (POST scenario)…"
# We cache a POST request
req1=$(curl -sL -X POST --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge | jq .time)
# We cache another POST request
req2=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time)
# We request the first POST request to be purged
curl -sL -X PURGE --data '{"foo": "bar"}' http://localhost:8081/test-cache-purge >/dev/null
# We check the requests
req3=$(curl -sL -X POST --data '{"foo": "foo"}' http://localhost:8081/test-cache-purge | jq .time)
req4=$(curl -sL -X POST --data '{"foo": "foobar"}' http://localhost:8081/test-cache-purge | jq .time)
if [ "${req1}" -eq "${req3}" ]; then
error "cache was not purged"
fi
if [ "${req2}" -ne "${req4}" ]; then
error "cache was purged"
fi

# If we are at this point, no test failed
info "All tests passed :)"
Expand Down

0 comments on commit 50095c6

Please sign in to comment.