examples: Add example that demonstrates working with files

This commit is contained in:
Sasha Koshka 2025-03-27 09:28:49 -04:00
parent b70fe4d4ba
commit c21f8a8712
12 changed files with 39 additions and 0 deletions

27
examples/files/index.step Normal file
View File

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
{{$directory := "letters"}}
<p>Files sorted alphabetically:</p>
<ul>
{{range listFiles $directory}}
<li>{{.}}</li>
{{end}}
</ul>
<p>Files sorted temporally:</p>
<ul>
{{range listFilesDate $directory}}
<li>{{.}}</li>
{{end}}
</ul>
<hr>
<p>Make a new file and watch the information above change:</p>
<form action="make.step" method="POST">
<input type="text" name="name" placeholder="File name"><br>
<input type="text" name="content" placeholder="File content"><br>
<input type="submit" value="Create file">
</form>
</body>
</html>

0
examples/files/letters/e Normal file
View File

0
examples/files/letters/i Normal file
View File

0
examples/files/letters/o Normal file
View File

0
examples/files/letters/p Normal file
View File

0
examples/files/letters/q Normal file
View File

0
examples/files/letters/r Normal file
View File

0
examples/files/letters/t Normal file
View File

0
examples/files/letters/u Normal file
View File

0
examples/files/letters/w Normal file
View File

0
examples/files/letters/y Normal file
View File

12
examples/files/make.step Normal file
View File

@ -0,0 +1,12 @@
{{- if ne .Data.Req.Method "POST"}}
{{- error 405 "only POST is supported"}}
{{- end}}
{{- $form := parseForm .Data.Req}}
{{- $name := $form.Get "name" | trim}}
{{- $content := $form.Get "content"}}
{{- if strInRange 1 32 $name | not}}{{error 400 "invalid name length"}}{{end}}
{{/* this is dangerous. you should not write to the same directory that the site
is in as you could end up accidentally allowing the user to overwrite site
contents */}}
{{writeFile (osJoinPaths "letters" (stripNonLetters $name)) $content}}
{{- redirect 303 "."}}