aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorBrian Picciano <me@mediocregopher.com>2024-11-01 13:00:18 +0100
committerBrian Picciano <me@mediocregopher.com>2024-11-01 13:00:18 +0100
commitcf6af6def1cac3fc6cd044c82282208b7073eb64 (patch)
tree64d6186b5e30e1125c1eff6fa1d86a379e3d3693 /example
parent246a99c28980e985bb4cff99042459bd5729cde1 (diff)
Implement gemtext HTTP middleware
Diffstat (limited to 'example')
-rw-r--r--example/Caddyfile23
-rw-r--r--example/tpl/render_gemtext.html9
-rw-r--r--example/tpl/render_gemtext_link.html5
-rw-r--r--example/tpl/render_gemtext_with_templates.html15
4 files changed, 41 insertions, 11 deletions
diff --git a/example/Caddyfile b/example/Caddyfile
index 4a4879f..6c6609d 100644
--- a/example/Caddyfile
+++ b/example/Caddyfile
@@ -5,7 +5,22 @@
http_port 8000
}
-http://template.localhost {
+http://gemtext.localhost {
+ root example/static
+
+ # Allow for either index.html or index.gmi files when serving directories
+ try_files {path} {path}/index.html {path}/index.gmi
+
+ gemtext {
+ root example/tpl
+ template render_gemtext.html
+ link_template render_gemtext_link.html
+ }
+
+ file_server
+}
+
+http://templates.localhost {
root example/static
# If a directory has an index.gmi file, then that file will be served when
@@ -20,13 +35,13 @@ http://template.localhost {
templates {
# The templates directive is given a different root, so that other
# template snippets within the tpl directory could theoretically be
- # used within render_gemtext.html.
+ # used within render_gemtext_with_templates.html.
root example
# Include the gemtext extention to make the gemtext function
# available within the template.
extensions {
- gemtext {
+ gemtext_function {
gateway_url "https://gemini.tildeverse.org/?gemini://"
}
}
@@ -37,7 +52,7 @@ http://template.localhost {
# actually want. Setting Content-Type is required because there's no
# actual file for Caddy to determine the value from.
header Content-Type "text/html; charset=utf-8"
- respond `{{ include "tpl/render_gemtext.html" }}`
+ respond `{{ include "tpl/render_gemtext_with_templates.html" }}`
}
# All other files are handled directly by the file_server.
diff --git a/example/tpl/render_gemtext.html b/example/tpl/render_gemtext.html
index 72e35a4..82d3c02 100644
--- a/example/tpl/render_gemtext.html
+++ b/example/tpl/render_gemtext.html
@@ -1,15 +1,10 @@
-{{ $pathSplit := splitList "/" .Req.URL.Path }}
-{{ $base := last $pathSplit | default "index.gmi" }}
-{{ $filePath := append (initial $pathSplit) $base | join "/" | printf "static%s" }}
-{{ if not (fileExists $filePath) }}{{ httpError 404 }}{{ end }}
-{{ $gemtextRes := gemtext (include $filePath) }}
<!DOCTYPE html>
<html>
<head>
- <title>{{ $gemtextRes.Title | default "Example Gemtext File" }}</title>
+ <title>{{ .Title | default "Example Gemtext File" }}</title>
<link rel="stylesheet" type="text/css" href="/bamboo.css" />
</head>
<body>
- {{ $gemtextRes.Body }}
+ {{ .Body }}
</body>
</html>
diff --git a/example/tpl/render_gemtext_link.html b/example/tpl/render_gemtext_link.html
new file mode 100644
index 0000000..b0d1251
--- /dev/null
+++ b/example/tpl/render_gemtext_link.html
@@ -0,0 +1,5 @@
+{{- $url := .URL }}
+{{- if (hasPrefix "gemini://" $url) }}
+ {{- $url = printf "https://gemini.tildeverse.org/?%s" $url }}
+{{- end }}
+<p><a href="{{ $url }}">{{ .Label }}</a></p>
diff --git a/example/tpl/render_gemtext_with_templates.html b/example/tpl/render_gemtext_with_templates.html
new file mode 100644
index 0000000..72e35a4
--- /dev/null
+++ b/example/tpl/render_gemtext_with_templates.html
@@ -0,0 +1,15 @@
+{{ $pathSplit := splitList "/" .Req.URL.Path }}
+{{ $base := last $pathSplit | default "index.gmi" }}
+{{ $filePath := append (initial $pathSplit) $base | join "/" | printf "static%s" }}
+{{ if not (fileExists $filePath) }}{{ httpError 404 }}{{ end }}
+{{ $gemtextRes := gemtext (include $filePath) }}
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>{{ $gemtextRes.Title | default "Example Gemtext File" }}</title>
+ <link rel="stylesheet" type="text/css" href="/bamboo.css" />
+ </head>
+ <body>
+ {{ $gemtextRes.Body }}
+ </body>
+</html>