Hi!
I love the automatic markdown handling, it’s almost everything I need to ditch my CMS and just serve a hierarchy of text files as my site. One thing that I couldn’t figure out though. Is it possible to tell caddy to serve the first markdown file it finds in a directory? So I could name my text files anything, and Caddy would still serve a markdown file in a directory as the index file.
Like these steps:
- User browses to www.example.com/posts/something
-
/posts/something
containsAn article.md
andsomerandomfile.md
- Caddy looks at the markdown files and picks
An article.md
because it’s the first one in natural ordering - Caddy serves that rendered markdown file for the url www.example.com/posts/something
The way I have it working now is that all directory urls get served through an index.php. The php script globs the markdown files in the dir and asks caddy for the first markdown file in the dir. It’s simple enough, but I feel like I’m missing something, that there could be a better way to do this.
Here’s my Caddyfile, nothing special
localhost:3000 {
gzip
root .
rewrite /404 {
status 404
}
rewrite / {
ext /
to /index.php
}
markdown / {
template template.html
}
fastcgi / 127.0.0.1:9000 php
}
And the php file is basically like: echo curl("domain.com/request_uri/first_markdown_file.md")
. (There’s some checks and stuff in the actual script, but I shortened the script here to the relevant bits)
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
$root = $_SERVER['DOCUMENT_ROOT'];
$dir = $root . $uri;
$files = glob($dir . '/*.md');
if ($files) {
$url = $host . $uri . '/' . rawurlencode(basename($files[0]));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
curl_close($ch);
} else {
//curl 404 page
}
Is there like a proper way to do this? Is this madness?