mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 19:53:21 +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
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"GohCMS2/adapters/secondary"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,16 +16,28 @@ type LoginPage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getPage(page string, data interface{}) ([]byte, error) {
|
func getPage(page string, data interface{}) ([]byte, error) {
|
||||||
template := secondary.GetTemplate(page)
|
var processedHTML bytes.Buffer
|
||||||
processed, err := secondary.ProcessTemplate(template, data)
|
tmpl, err := template.ParseFiles("./web/templates/" + page + ".html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func getLoginPageHandler(errMsg string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
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{
|
bs, err := getPage("login", &LoginPage{
|
||||||
IsError: strings.Compare(errMsg, "") != 0,
|
IsError: strings.Compare(errMsg, "") != 0,
|
||||||
Error: errMsg,
|
Error: errMsg,
|
||||||
@@ -44,6 +57,7 @@ func postLoginPage(w http.ResponseWriter, r *http.Request) {
|
|||||||
Password: r.FormValue("password"),
|
Password: r.FormValue("password"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
r.Method = http.MethodGet
|
||||||
getLoginPageHandler("Missing username or password.")(w, r)
|
getLoginPageHandler("Missing username or password.")(w, r)
|
||||||
return
|
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))
|
response, err := http.Post("http://localhost:8080/v1/auth/login", "application/json", bytes.NewBuffer(credentials))
|
||||||
// TODO handle possible errors in separate file (api package)
|
// TODO handle possible errors in separate file (api package)
|
||||||
if err != nil || response.StatusCode != http.StatusOK {
|
if err != nil || response.StatusCode != http.StatusOK {
|
||||||
|
r.Method = http.MethodGet
|
||||||
getLoginPageHandler("Invalid username or password.")(w, r)
|
getLoginPageHandler("Invalid username or password.")(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Set-Cookie", response.Header.Get("Set-Cookie"))
|
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) {
|
func getHomePage(w http.ResponseWriter, _ *http.Request) {
|
||||||
bs, err := getPage("home", nil)
|
navbarTmpl, _ := getPage("componentNavbar", nil)
|
||||||
if err != nil {
|
homeTmpl, _ := getPage("home", map[string]interface{}{
|
||||||
_, _ = w.Write([]byte(err.Error()))
|
"Navbar": template.HTML(navbarTmpl),
|
||||||
}
|
})
|
||||||
_, _ = w.Write(bs)
|
_, _ = 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 {
|
func IsLoggedInMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if !IsLoggedIn(r) {
|
if !IsLoggedIn(r) {
|
||||||
http.Redirect(w, r, "/login", http.StatusFound)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
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 {
|
func NewPageRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
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.Get("/login", getLoginPageHandler(""))
|
||||||
r.Post("/login", postLoginPage)
|
r.Post("/login", getLoginPageHandler(""))
|
||||||
|
r.Get("/logout", getLogout)
|
||||||
|
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(IsLoggedInMiddleware)
|
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>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||||
<title>Login Page</title>
|
<title>GohCMS | Accueil</title>
|
||||||
|
|
||||||
<link crossorigin="anonymous"
|
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
|
<script src="/static/bootstrap.min.js"
|
||||||
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
|
type="application/javascript"
|
||||||
rel="stylesheet">
|
defer></script>
|
||||||
<script crossorigin="anonymous"
|
|
||||||
defer
|
|
||||||
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
|
|
||||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
{{.Navbar}}
|
||||||
<h1>Home</h1>
|
<h1>Home</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -5,14 +5,10 @@
|
|||||||
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
<meta content="width=device-width, initial-scale=1.0" name="viewport">
|
||||||
<title>GohCMS | Login</title>
|
<title>GohCMS | Login</title>
|
||||||
|
|
||||||
<link crossorigin="anonymous"
|
<link rel="stylesheet" href="/static/bootstrap.min.css" type="text/css">
|
||||||
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css"
|
<script src="/static/bootstrap.min.js"
|
||||||
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9"
|
type="application/javascript"
|
||||||
rel="stylesheet">
|
defer></script>
|
||||||
<script crossorigin="anonymous"
|
|
||||||
defer
|
|
||||||
integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm"
|
|
||||||
src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
|
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.form-container {
|
.form-container {
|
||||||
|
|||||||
Reference in New Issue
Block a user