diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 17:36:27 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-10-02 17:36:27 +0200 |
| commit | dfca0f5bbebfe11d9c5d8bf6584bd1bf80d62274 (patch) | |
| tree | e033bb57affecf03fc03b678b3a876377441de9f | |
| parent | a471baf6ee1b1a05acec23f2de1b74f56f4596a5 (diff) | |
feat(backend): og and twitter seo
| -rw-r--r-- | backend/data.go | 36 | ||||
| -rw-r--r-- | backend/home.go | 15 | ||||
| -rw-r--r-- | backend/templates/base.html | 16 |
3 files changed, 58 insertions, 9 deletions
diff --git a/backend/data.go b/backend/data.go index adf5db3..e1a8478 100644 --- a/backend/data.go +++ b/backend/data.go @@ -1,11 +1,41 @@ package backend +import ( + "fmt" + "html/template" + "net/http" +) + type data struct { - title string - Article bool + title string + Article bool + Domain string + URL string + Image string + Description string +} + +func (d *data) handleGeneric(w http.ResponseWriter, name string) { + t, err := template.New("").Funcs(template.FuncMap{ + "static": func(path string) string { + return fmt.Sprintf("/static/%s", path) + }, + "assets": func(path string) string { + return fmt.Sprintf("/assets/%s", path) + }, + }).ParseFS(templates, fmt.Sprintf("templates/%s.html", name), "templates/base.html") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + err = t.ExecuteTemplate(w, "base.html", d) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } } -func (d data) Title() string { +func (d *data) Title() string { title := "anhgelus" if d.Article { title += " - log entry" diff --git a/backend/home.go b/backend/home.go index 8577fec..a7f2ae1 100644 --- a/backend/home.go +++ b/backend/home.go @@ -1,18 +1,21 @@ package backend import ( - "html/template" "net/http" "github.com/go-chi/chi/v5" ) func HandleHome(r *chi.Mux) { - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - t := template.Must(template.ParseFS(templates, "templates/home.html", "templates/base.html")) - err := t.ExecuteTemplate(w, "base.html", &data{}) - if err != nil { - panic(err) + r.Get("/", func(w http.ResponseWriter, _ *http.Request) { + d := &data{ + title: "", + Article: false, + Domain: "anhgelus.world", + URL: "/", + Image: "", + Description: "", } + d.handleGeneric(w, "home") }) } diff --git a/backend/templates/base.html b/backend/templates/base.html index c994cac..922273e 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -5,6 +5,22 @@ <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ .Title }}</title> <link rel="stylesheet" href="/assets/styles.css"> + <meta property="description" content="{{ .Description }}" /> + <!-- Open Graph --> + <meta property="og:title" content="{{ .Title }}" /> + <meta property="og:type" content="website" /> + <meta property="og:url" content="https://{{ .Domain }}{{ .URL }}" /> + <meta property="og:image" content="https://{{ .Domain }}{{ static .Image }}" /> + <meta property="og:description" content="{{ .Description }}" /> + <meta property="og:local" content="fr_FR" /> + <meta property="og:site_name" content="{{ .Title }}'s Now page" /> + <!-- Twitter --> + <meta name="twitter:card" content="summary_large_image" /> + <meta property="twitter:domain" content="{{ .Domain }}" /> + <meta property="twitter:url" content="https://{{ .Domain }}{{ .URL }}/" /> + <meta name="twitter:title" content="{{ .Title }}" /> + <meta name="twitter:description" content="{{ .Description }}" /> + <meta name="twitter:image" content="https://{{ .Domain }}{{ static .Image }}" /> </head> <body> <header> |
