mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
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:
@@ -16,7 +16,7 @@
|
||||
<a class="nav-link" href="/home">Accueil</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/posts">Posts</a>
|
||||
<a class="nav-link" href="/post">Posts</a>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="navbar-nav">
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Accueil</title>
|
||||
|
||||
<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">
|
||||
{{.Navbar}}
|
||||
<div class="container mt-3 text-black-50">
|
||||
<h1>Accueil</h1>
|
||||
<p>Bienvenue sur GohCMS !</p>
|
||||
<a href="/posts">
|
||||
<a href="/post">
|
||||
<button class="btn btn-outline-primary">Posts »</button>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Connexion</title>
|
||||
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
type="application/javascript"
|
||||
defer></script>
|
||||
{{.Head}}
|
||||
|
||||
<style>
|
||||
.form-container {
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Post edition</title>
|
||||
|
||||
<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">
|
||||
{{.Navbar}}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Posts</title>
|
||||
|
||||
<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">
|
||||
{{.Navbar}}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Installation</title>
|
||||
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
type="application/javascript"
|
||||
defer></script>
|
||||
{{.Head}}
|
||||
|
||||
<style>
|
||||
.form-container {
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Installation</title>
|
||||
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
type="application/javascript"
|
||||
defer></script>
|
||||
{{.Head}}
|
||||
|
||||
<style>
|
||||
.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
@@ -3,6 +3,7 @@ package api
|
||||
import (
|
||||
"embed"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
//go:embed static
|
||||
var staticFolder embed.FS
|
||||
|
||||
var headTmpl template.HTML
|
||||
|
||||
var contentTypes = map[string]string{
|
||||
".css": "text/css",
|
||||
".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 {
|
||||
r := chi.NewRouter()
|
||||
|
||||
contentStatic := fs.FS(staticFolder)
|
||||
|
||||
InitHeadTmpl()
|
||||
|
||||
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(LoginRoute, GetLoginPageHandler(EmptyLoginPage))
|
||||
@@ -89,7 +93,8 @@ func NewPageRouter() http.Handler {
|
||||
r.Use(IsLoggedInMiddleware)
|
||||
r.Get("/register-confirm", GetRegisterConfirmPage)
|
||||
r.Get("/home", GetHomePage)
|
||||
r.Get("/posts", GetPostsPage)
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/post/edit", GetPostEditPage)
|
||||
})
|
||||
|
||||
return r
|
||||
|
||||
@@ -9,6 +9,7 @@ func GetHomePage(w http.ResponseWriter, _ *http.Request) {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
homeTmpl, _ := Container.GetPageUseCase.GetPage("home", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
})
|
||||
_, _ = w.Write(homeTmpl)
|
||||
}
|
||||
|
||||
+5
-1
@@ -33,7 +33,11 @@ func GetLoginPageHandler(loginPage *LoginPage) http.HandlerFunc {
|
||||
http.Redirect(w, r, "/register", http.StatusSeeOther)
|
||||
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 {
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ func GetPostsPage(w http.ResponseWriter, _ *http.Request) {
|
||||
navbarTmpl, _ := Container.GetPageUseCase.GetPage("componentNavbar", nil)
|
||||
postsTmpl, _ := Container.GetPageUseCase.GetPage("posts", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
"Head": headTmpl,
|
||||
})
|
||||
_, _ = w.Write(postsTmpl)
|
||||
}
|
||||
|
||||
+6
-1
@@ -29,7 +29,12 @@ func GetRegisterPageHandler(registerPage *RegisterPage) http.HandlerFunc {
|
||||
PostRegisterPage(w, r)
|
||||
return
|
||||
}
|
||||
bs, err := Container.GetPageUseCase.GetPage("setup1", ®isterPage)
|
||||
bs, err := Container.GetPageUseCase.GetPage("setup1", map[string]interface{}{
|
||||
"PageError": registerPage.PageError,
|
||||
"Username": registerPage.Username,
|
||||
"Email": registerPage.Email,
|
||||
"Head": headTmpl,
|
||||
})
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -42,6 +42,6 @@ func InitServer() *chi.Mux {
|
||||
|
||||
func StartServer(router *chi.Mux) error {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user