mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: added navbar system with some refactors
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
package secondary
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"os"
|
||||
)
|
||||
|
||||
func ProcessTemplate(html []byte, data interface{}) ([]byte, error) {
|
||||
tmpl, err := template.New("template").Parse(string(html))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var processedHTML bytes.Buffer
|
||||
err = tmpl.Execute(&processedHTML, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return processedHTML.Bytes(), nil
|
||||
}
|
||||
|
||||
func GetTemplate(name string) []byte {
|
||||
file, err := os.Open("./web/templates/" + name + ".html")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}(file)
|
||||
|
||||
stat, err := file.Stat()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bs := make([]byte, stat.Size())
|
||||
_, err = file.Read(bs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return bs
|
||||
}
|
||||
+67
-12
@@ -1,11 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"GohCMS2/adapters/secondary"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -15,16 +16,28 @@ type LoginPage struct {
|
||||
}
|
||||
|
||||
func getPage(page string, data interface{}) ([]byte, error) {
|
||||
template := secondary.GetTemplate(page)
|
||||
processed, err := secondary.ProcessTemplate(template, data)
|
||||
var processedHTML bytes.Buffer
|
||||
tmpl, err := template.ParseFiles("./web/templates/" + page + ".html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return processed, nil
|
||||
err = tmpl.Execute(&processedHTML, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return processedHTML.Bytes(), nil
|
||||
}
|
||||
|
||||
func getLoginPageHandler(errMsg string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if IsLoggedIn(r) {
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost {
|
||||
postLoginPage(w, r)
|
||||
return
|
||||
}
|
||||
bs, err := getPage("login", &LoginPage{
|
||||
IsError: strings.Compare(errMsg, "") != 0,
|
||||
Error: errMsg,
|
||||
@@ -44,6 +57,7 @@ func postLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
Password: r.FormValue("password"),
|
||||
})
|
||||
if err != nil {
|
||||
r.Method = http.MethodGet
|
||||
getLoginPageHandler("Missing username or password.")(w, r)
|
||||
return
|
||||
}
|
||||
@@ -52,37 +66,78 @@ func postLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
response, err := http.Post("http://localhost:8080/v1/auth/login", "application/json", bytes.NewBuffer(credentials))
|
||||
// TODO handle possible errors in separate file (api package)
|
||||
if err != nil || response.StatusCode != http.StatusOK {
|
||||
r.Method = http.MethodGet
|
||||
getLoginPageHandler("Invalid username or password.")(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Set-Cookie", response.Header.Get("Set-Cookie"))
|
||||
|
||||
http.Redirect(w, r, "/home", http.StatusFound)
|
||||
http.Redirect(w, r, "/home", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func getHomePage(w http.ResponseWriter, _ *http.Request) {
|
||||
bs, err := getPage("home", nil)
|
||||
if err != nil {
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
}
|
||||
_, _ = w.Write(bs)
|
||||
navbarTmpl, _ := getPage("componentNavbar", nil)
|
||||
homeTmpl, _ := getPage("home", map[string]interface{}{
|
||||
"Navbar": template.HTML(navbarTmpl),
|
||||
})
|
||||
_, _ = w.Write(homeTmpl)
|
||||
}
|
||||
|
||||
func getLogin(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/login", http.StatusPermanentRedirect)
|
||||
}
|
||||
|
||||
func getLogout(w http.ResponseWriter, r *http.Request) {
|
||||
removeJwtCookie(w)
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !IsLoggedIn(r) {
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func staticFileServerWithContentType(dir http.Dir) http.Handler {
|
||||
contentTypes := map[string]string{
|
||||
".css": "text/css",
|
||||
".js": "application/javascript",
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".webp": "image/webp",
|
||||
".svg": "image/svg+xml",
|
||||
".ico": "image/x-icon",
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestedFilePath := r.URL.Path
|
||||
fileExtension := filepath.Ext(requestedFilePath)
|
||||
|
||||
if contentType, ok := contentTypes[fileExtension]; ok {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
|
||||
http.FileServer(dir).ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func NewPageRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Handle("/static/*", http.StripPrefix("/static/", staticFileServerWithContentType("./web/static")))
|
||||
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./web/static/favicon.ico")
|
||||
})
|
||||
|
||||
r.Get("/", getLogin)
|
||||
r.Get("/login", getLoginPageHandler(""))
|
||||
r.Post("/login", postLoginPage)
|
||||
r.Post("/login", getLoginPageHandler(""))
|
||||
r.Get("/logout", getLogout)
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
r.Use(IsLoggedInMiddleware)
|
||||
|
||||
Vendored
+6
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Vendored
+7
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
@@ -0,0 +1,27 @@
|
||||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
|
||||
<div class="container">
|
||||
<a class="navbar-brand" href="#">GohCMS</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
|
||||
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav me-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/home">Accueil</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#">Link 2</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#">Link 3</a>
|
||||
</li>
|
||||
</ul>
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link disconnect-link" href="/logout">Se déconnecter</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
@@ -3,21 +3,18 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>Login Page</title>
|
||||
<title>GohCMS | Accueil</title>
|
||||
|
||||
<link crossorigin="anonymous"
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
|
||||
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
|
||||
rel="stylesheet">
|
||||
<script crossorigin="anonymous"
|
||||
defer
|
||||
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
type="application/javascript"
|
||||
defer></script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
{{.Navbar}}
|
||||
<h1>Home</h1>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -5,14 +5,10 @@
|
||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||
<title>GohCMS | Login</title>
|
||||
|
||||
<link crossorigin="anonymous"
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
|
||||
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
|
||||
rel="stylesheet">
|
||||
<script crossorigin="anonymous"
|
||||
defer
|
||||
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
|
||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||
<script src="/static/bootstrap.min.js"
|
||||
type="application/javascript"
|
||||
defer></script>
|
||||
|
||||
<style>
|
||||
.form-container {
|
||||
|
||||
Reference in New Issue
Block a user