diff options
| -rw-r--r-- | backend/data.go | 28 | ||||
| -rw-r--r-- | backend/logs.go | 17 | ||||
| -rw-r--r-- | backend/router.go | 2 | ||||
| -rw-r--r-- | backend/templates/base.html | 1 | ||||
| -rw-r--r-- | backend/templates/rss.xml | 20 |
5 files changed, 65 insertions, 3 deletions
diff --git a/backend/data.go b/backend/data.go index 2d9f97d..6ca6020 100644 --- a/backend/data.go +++ b/backend/data.go @@ -14,6 +14,7 @@ import ( "net/url" "regexp" "strings" + txt "text/template" ) var ( @@ -41,8 +42,7 @@ func (d *data) SetData(data *data) { *d = *data } -func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) { - cfg := r.Context().Value(configKey).(*Config) +func (d *data) merge(cfg *Config, r *http.Request) { if d.Domain == "" { d.Domain = cfg.Domain } @@ -74,6 +74,11 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string } d.URL = r.URL.Path } +} + +func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) { + cfg := r.Context().Value(configKey).(*Config) + d.merge(cfg, r) t, err := template.New("").Funcs(template.FuncMap{ "static": getStatic, "fullStatic": func(path string) string { @@ -108,6 +113,25 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string } } +func (d *data) handleRSS(w http.ResponseWriter, r *http.Request, custom dataUsable) { + cfg := r.Context().Value(configKey).(*Config) + d.merge(cfg, r) + t, err := txt.ParseFS(templates, "templates/rss.xml") + if err != nil { + panic(err) + } + r.Header.Set("Content-Type", "application/rss+xml") + if custom == nil { + err = t.ExecuteTemplate(w, "rss.xml", d) + } else { + custom.SetData(d) + err = t.ExecuteTemplate(w, "rss.xml", custom) + } + if err != nil { + panic(err) + } +} + func (d *data) Title() string { title := d.Name if d.Article { diff --git a/backend/logs.go b/backend/logs.go index 86e0221..03d4f1f 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -36,6 +36,10 @@ func (d *logData) PubDate() string { return d.PubLocalDate.String() } +func (d *logData) PubDateRSS() string { + return d.PubLocalDate.AsTime(time.Local).Format(time.RFC1123Z) // because RFC822 in go isn't RFC822??? +} + func (d *logData) Title() string { return d.data.Title() } @@ -116,6 +120,10 @@ func HandleLogs(r *chi.Mux) { r.Get("/logs", handleLogList) r.Route("/logs", func(r chi.Router) { r.Get("/", handleLogList) + + r.Get("/rss", handleLogRSS) + r.Get("/rss/", handleLogRSS) + r.Get("/{slug:[a-zA-Z0-9-]+}", handleLog) r.Get("/{slug:[a-zA-Z0-9-]+}/", handleLog) }) @@ -130,6 +138,15 @@ func handleLogList(w http.ResponseWriter, r *http.Request) { d.handleGeneric(w, r, "home_log", d) } +func handleLogRSS(w http.ResponseWriter, r *http.Request) { + d := handleGenericLogsDisplay(w, r, 5) + if d == nil { + return + } + d.title = "logs" + d.handleRSS(w, r, d) +} + func handleLog(w http.ResponseWriter, r *http.Request) { cfg := r.Context().Value(configKey).(*Config) slug := chi.URLParam(r, "slug") diff --git a/backend/router.go b/backend/router.go index fb43845..59a02e3 100644 --- a/backend/router.go +++ b/backend/router.go @@ -17,7 +17,7 @@ import ( ) const ( - Version = "0.2.0" + Version = "0.3.0" configKey = "config" isUpdateKey = "is_update" assetsFSKey = "assets_fs" diff --git a/backend/templates/base.html b/backend/templates/base.html index 86e2f58..0a54096 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -7,6 +7,7 @@ {{ $styles := asset "styles.css" }} <link rel="stylesheet" href="{{ $styles.Src }}" integrity="{{ $styles.Checksum }}" /> <link rel="shortcut icon" href="{{ static .Logo.Favicon }}" /> + <link rel="alternate" href="/logs/rss" type="application/rss+xml" title="RSS" /> <meta property="description" content="{{ .PageDescription }}" /> <!-- Open Graph --> <meta property="og:title" content="{{ .Title }}" /> diff --git a/backend/templates/rss.xml b/backend/templates/rss.xml new file mode 100644 index 0000000..c8390da --- /dev/null +++ b/backend/templates/rss.xml @@ -0,0 +1,20 @@ +<?xml version="1.0"?> +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> + <channel> + <atom:link href="https://{{ .Domain }}{{ .URL }}" rel="self" type="application/rss+xml" /> + <title>{{ .Title }}</title> + <link>https://{{ .Domain }}/logs/</link> + <description>{{ .PageDescription }}</description> + <language>fr-fr</language> + {{ $domain := .Domain }} + {{ range .Logs }} + <item> + <title>{{ .LogTitle }}</title> + <link>https://{{ $domain }}/logs/{{ .Slug }}</link> + <guid>https://{{ $domain }}/logs/{{ .Slug }}</guid> + <description>{{ .Description }}</description> + <pubDate>{{ .PubDateRSS }}</pubDate> + </item> + {{ end }} + </channel> +</rss>
\ No newline at end of file |
