tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

64 lignes · 1386 octetsBrut · Historique
1/// Static assets, served from disk under `/static/` (std/file, ADR 0049).
2
3import std/file.{type fs}
4import std/http.{type Response}
5import std/string
6
7/// The file at `.` + `path` (`/static/x.txt` is `./static/x.txt`), with
8/// the content type of its extension, or 404. A path containing `..` is
9/// rejected: a request's path must not leave the directory.
10pub fn static_file :: path: String -> Response / <fs>
11{
12 if string.contains path ".."
13 {
14 http.not_found ()
15 }
16 else
17 {
18 match file.read_bytes ("." <> path)
19 { Ok(content) -> http.response 200 (content_type path) content
20 , Error(_) -> http.not_found ()
21 }
22 }
23}
24
25/// The content type of a path's extension.
26fn content_type :: path: String -> String
27{
28 if string.ends_with path ".css"
29 {
30 "text/css; charset=utf-8"
31 }
32 else if string.ends_with path ".js"
33 {
34 "text/javascript; charset=utf-8"
35 }
36 else if string.ends_with path ".png"
37 {
38 "image/png"
39 }
40 else if string.ends_with path ".jpg" || string.ends_with path ".jpeg"
41 {
42 "image/jpeg"
43 }
44 else if string.ends_with path ".svg"
45 {
46 "image/svg+xml"
47 }
48 else if string.ends_with path ".woff2"
49 {
50 "font/woff2"
51 }
52 else if string.ends_with path ".html"
53 {
54 "text/html; charset=utf-8"
55 }
56 else if string.ends_with path ".txt"
57 {
58 "text/plain; charset=utf-8"
59 }
60 else
61 {
62 "application/octet-stream"
63 }
64}