Specify a default sort order for file-server

1. The problem I’m having:

I have a caddy server serving files in a directory:

caddy file-server --browse --root /app/web

Currently the files are sorted by Filename in ASC order. I would like to specify a different sort order by default. I have taken a look at the browse.html template file, but I didn’t find any way to specify a default sort order.

2. Error messages and/or full log output:

none

3. Caddy version:

Docker Image caddy:2.7
v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

Custom docker image with Caddy inside:

FROM caddy:2.7

# Install python3 and pip3
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools graphviz

# Install docker
RUN apk add --update --no-cache docker-cli

# Install app
RUN mkdir -p /app/web
WORKDIR /app
COPY ./listDockerNetworks.py /app
COPY ./graphviz.html /app

# Setting up crontab
COPY ./crontab /tmp/crontab
RUN cat /tmp/crontab >> /etc/crontabs/root

ARG VCS_REF=0
ENV VCS_REF=$VCS_REF
RUN echo "VCS_REF=$VCS_REF"

ARG BUILD_DATE=0
ENV BUILD_DATE=$BUILD_DATE
RUN echo "BUILD_DATE=$BUILD_DATE"

ARG VERSION=""
ENV VERSION=$VERSION
RUN echo "VERSION=$VERSION"

 RUN sed -i "s/%%GITHASH%%/$VCS_REF/g" /app/graphviz.html

LABEL org.opencontainers.image.version=$VERSION
LABEL org.opencontainers.image.revision=$VCS_REF
LABEL org.opencontainers.image.vendor="Company Name"
LABEL org.opencontainers.image.title="docker-network-visualizer"
LABEL org.opencontainers.image.created=$BUILD_DATE

CMD ["sh", "-c", "/usr/sbin/crond -L /var/log/cron.log && caddy file-server --browse --root /app/web"]

a. System environment:

  • Debian GNU/Linux 12 (bookworm), amd64
  • Docker version 25.0.3, build 4debf41

The default sort order can’t be changed, but you can use a query string parameter to change the sort. Would that work for you? (Just click the column headers to get the qs values).

I think you could take the default browse template and modify it to your needs to change the default sort order.

Or you can use matchers and rewrite to set the query to the sort you want, as Matt suggests.

Hi Matt and Francis,

I know that every user can sort by clicking on the header, but it is quite inconvenient to have to do this every time you access the page.

I have also taken already a look at the browse.html template but couldn’t figure out a way to set the default sort orders.

I went through the docs and have not been able to find this.

thanks
Philipp

You could add something like this to your config:

	@directories `path('/foo/', '/bar/') && {query} == ''`
	redir @directories ?sort=size&order=desc

This uses an expression matcher, matching 2 specific directories (you could adjust this to whatever paths you want, or simply */ for all directory requests) and checks that the query is empty. If so, immediately redirects to add a query.

2 Likes

Thanks @francislavoie thats exactly what I was looking for.

If anyone else is looking for this, I will share my Caddyfile here:

http:// {
    @directories `path('*/') && {query} == ''`
    redir @directories ?sort=name&order=desc

    file_server * {
        root /app/web
        browse
    }
}
2 Likes

I’d suggest changing it to this:

root * /app/web
file_server browse

This way if you later decide to use the file matcher or try_files, it’ll work without changing anything else (because of using the root directive).

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.