clean: refactor templates to use a common head section

This commit introduces a refactoring in various page templates to use a central, common 'head' section, stored under utilsHead.html. This head section contains common metadata, styles, and scripts. This change enhances code reusability and standardizes the head section across different pages for consistent user experience and ease of future potential changes. Also, some minimal adjustments were made in the navigation and link routing for more user-friendly URL paths. Lastly, the server's listening interface was updated to only listen on the localhost.
This commit is contained in:
Florian Sylvain
2023-10-11 20:28:00 +02:00
parent c2b64bf0fa
commit 7d071ebc2e
16 changed files with 59 additions and 56 deletions
@@ -16,7 +16,7 @@
<a class="nav-link" href="/home">Accueil</a> <a class="nav-link" href="/home">Accueil</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/posts">Posts</a> <a class="nav-link" href="/post">Posts</a>
</li> </li>
</ul> </ul>
<ul class="navbar-nav"> <ul class="navbar-nav">
@@ -1,22 +1,15 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Accueil</title> <title>GohCMS | Accueil</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
</head> </head>
<body class="text-dark"> <body class="text-dark">
{{.Navbar}} {{.Navbar}}
<div class="container mt-3 text-black-50"> <div class="container mt-3 text-black-50">
<h1>Accueil</h1> <h1>Accueil</h1>
<p>Bienvenue sur GohCMS !</p> <p>Bienvenue sur GohCMS !</p>
<a href="/posts"> <a href="/post">
<button class="btn btn-outline-primary">Posts &raquo;</button> <button class="btn btn-outline-primary">Posts &raquo;</button>
</a> </a>
</div> </div>
@@ -1,14 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Connexion</title> <title>GohCMS | Connexion</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
<style> <style>
.form-container { .form-container {
@@ -1,14 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Post edition</title> <title>GohCMS | Post edition</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
</head> </head>
<body class="text-dark"> <body class="text-dark">
{{.Navbar}} {{.Navbar}}
@@ -1,14 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Posts</title> <title>GohCMS | Posts</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
</head> </head>
<body class="text-dark"> <body class="text-dark">
{{.Navbar}} {{.Navbar}}
@@ -1,14 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Installation</title> <title>GohCMS | Installation</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
<style> <style>
.form-container { .form-container {
@@ -1,14 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>GohCMS | Installation</title> <title>GohCMS | Installation</title>
{{.Head}}
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
<style> <style>
.form-container { .form-container {
@@ -0,0 +1,7 @@
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
<script src="/static/bootstrap.min.js"
type="application/javascript"
defer></script>
+12 -7
View File
@@ -3,6 +3,7 @@ package api
import ( import (
"embed" "embed"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"html/template"
"io/fs" "io/fs"
"net/http" "net/http"
"path/filepath" "path/filepath"
@@ -12,6 +13,8 @@ import (
//go:embed static //go:embed static
var staticFolder embed.FS var staticFolder embed.FS
var headTmpl template.HTML
var contentTypes = map[string]string{ var contentTypes = map[string]string{
".css": "text/css", ".css": "text/css",
".js": "application/javascript", ".js": "application/javascript",
@@ -65,18 +68,19 @@ func StaticFileServerWithContentType(fsys http.FileSystem) http.Handler {
}) })
} }
func InitHeadTmpl() {
headTmplHtml, _ := Container.GetPageUseCase.GetPage("utilsHead", nil)
headTmpl = template.HTML(headTmplHtml)
}
func NewPageRouter() http.Handler { func NewPageRouter() http.Handler {
r := chi.NewRouter() r := chi.NewRouter()
contentStatic := fs.FS(staticFolder) contentStatic := fs.FS(staticFolder)
InitHeadTmpl()
r.Handle("/static/*", StaticFileServerWithContentType(http.FS(contentStatic))) r.Handle("/static/*", StaticFileServerWithContentType(http.FS(contentStatic)))
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
_, err := http.FS(staticFolder).Open("favicon.ico")
if err != nil {
return
}
})
r.Get("/", GetLogin) r.Get("/", GetLogin)
r.Get(LoginRoute, GetLoginPageHandler(EmptyLoginPage)) r.Get(LoginRoute, GetLoginPageHandler(EmptyLoginPage))
@@ -89,7 +93,8 @@ func NewPageRouter() http.Handler {
r.Use(IsLoggedInMiddleware) r.Use(IsLoggedInMiddleware)
r.Get("/register-confirm", GetRegisterConfirmPage) r.Get("/register-confirm", GetRegisterConfirmPage)
r.Get("/home", GetHomePage) r.Get("/home", GetHomePage)
r.Get("/posts", GetPostsPage) r.Get("/post", GetPostsPage)
r.Get("/post/edit", GetPostEditPage)
}) })
return r return r
+1
View File
@@ -9,6 +9,7 @@ func GetHomePage(w http.ResponseWriter, _ *http.Request) {
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil) navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
homeTmpl, _ := Container.GetPageUseCase.GetPage("home", map[string]interface{}{ homeTmpl, _ := Container.GetPageUseCase.GetPage("home", map[string]interface{}{
"Navbar": template.HTML(navbarTmpl), "Navbar": template.HTML(navbarTmpl),
"Head": headTmpl,
}) })
_, _ = w.Write(homeTmpl) _, _ = w.Write(homeTmpl)
} }
+5 -1
View File
@@ -33,7 +33,11 @@ func GetLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
http.Redirect(w, r, "/register", http.StatusSeeOther) http.Redirect(w, r, "/register", http.StatusSeeOther)
return return
} }
bs, err := Container.GetPageUseCase.GetPage("login", &loginPage) bs, err := Container.GetPageUseCase.GetPage("login", map[string]interface{}{
"PageError": loginPage.PageError,
"Username": loginPage.Username,
"Head": headTmpl,
})
if err != nil { if err != nil {
_, _ = w.Write([]byte(err.Error())) _, _ = w.Write([]byte(err.Error()))
} }
+15
View File
@@ -0,0 +1,15 @@
package api
import (
"html/template"
"net/http"
)
func GetPostEditPage(w http.ResponseWriter, _ *http.Request) {
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
postsTmpl, _ := Container.GetPageUseCase.GetPage("postEdit", map[string]interface{}{
"Navbar": template.HTML(navbarTmpl),
"Head": headTmpl,
})
_, _ = w.Write(postsTmpl)
}
+1
View File
@@ -9,6 +9,7 @@ func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil) navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
postsTmpl, _ := Container.GetPageUseCase.GetPage("posts", map[string]interface{}{ postsTmpl, _ := Container.GetPageUseCase.GetPage("posts", map[string]interface{}{
"Navbar": template.HTML(navbarTmpl), "Navbar": template.HTML(navbarTmpl),
"Head": headTmpl,
}) })
_, _ = w.Write(postsTmpl) _, _ = w.Write(postsTmpl)
} }
+6 -1
View File
@@ -29,7 +29,12 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
PostRegisterPage(w, r) PostRegisterPage(w, r)
return return
} }
bs, err := Container.GetPageUseCase.GetPage("setup1", &registerPage) bs, err := Container.GetPageUseCase.GetPage("setup1", map[string]interface{}{
"PageError": registerPage.PageError,
"Username": registerPage.Username,
"Email": registerPage.Email,
"Head": headTmpl,
})
if err != nil { if err != nil {
_, _ = w.Write([]byte(err.Error())) _, _ = w.Write([]byte(err.Error()))
} }
+3 -1
View File
@@ -5,6 +5,8 @@ import (
) )
func GetRegisterConfirmPage(w http.ResponseWriter, _ *http.Request) { func GetRegisterConfirmPage(w http.ResponseWriter, _ *http.Request) {
registerConfirmTmpl, _ := Container.GetPageUseCase.GetPage("setup2", nil) registerConfirmTmpl, _ := Container.GetPageUseCase.GetPage("setup2", map[string]interface{}{
"Head": headTmpl,
})
_, _ = w.Write(registerConfirmTmpl) _, _ = w.Write(registerConfirmTmpl)
} }
+1 -1
View File
@@ -42,6 +42,6 @@ func InitServer() *chi.Mux {
func StartServer(router *chi.Mux) error { func StartServer(router *chi.Mux) error {
fmt.Println("Server starting on http://localhost:" + os.Getenv("PORT")) fmt.Println("Server starting on http://localhost:" + os.Getenv("PORT"))
err := http.ListenAndServe(":"+os.Getenv("PORT"), router) err := http.ListenAndServe("localhost:"+os.Getenv("PORT"), router)
return err return err
} }