Compare commits
4 Commits
299e586d69
...
f382ccc008
Author | SHA1 | Date | |
---|---|---|---|
f382ccc008
|
|||
512d76a380
|
|||
bc9b2d44b2
|
|||
b7dbe1ef0d
|
@ -1,9 +1,9 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"gitea.local/admin/hspguard/internal/web"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
@ -15,9 +15,22 @@ func NewUserHandler() *UserHandler {
|
||||
|
||||
func (h *UserHandler) RegisterRoutes(router chi.Router) {
|
||||
router.Get("/login", h.handleLogin)
|
||||
router.Get("/register", h.handleRegister)
|
||||
}
|
||||
|
||||
func (h *UserHandler) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = fmt.Fprintf(w, "/GET Hello, user from %s", r.RemoteAddr)
|
||||
data := map[string]any{
|
||||
"Title": "Login",
|
||||
}
|
||||
|
||||
web.RenderTemplate(w, "login", data)
|
||||
}
|
||||
|
||||
func (h *UserHandler) handleRegister(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]any{
|
||||
"Title": "Register",
|
||||
}
|
||||
|
||||
web.RenderTemplate(w, "register", data)
|
||||
}
|
||||
|
||||
|
27
internal/web/templates.go
Normal file
27
internal/web/templates.go
Normal file
@ -0,0 +1,27 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func RenderTemplate(w http.ResponseWriter, page string, data map[string]any) {
|
||||
base, err := template.ParseGlob(filepath.Join("templates", "layout", "*.html"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := base.ParseFiles(filepath.Join("templates", "pages", fmt.Sprintf("%s.html", page)))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "base", data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
19
templates/layout/base.html
Normal file
19
templates/layout/base.html
Normal file
@ -0,0 +1,19 @@
|
||||
{{ define "base" }}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>{{ .Title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>{{ .Title }}</h1>
|
||||
</header>
|
||||
<main>
|
||||
{{ block "content" . }}{{ end }}
|
||||
</main>
|
||||
{{ template "footer" . }}
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
|
4
templates/layout/footer.html
Normal file
4
templates/layout/footer.html
Normal file
@ -0,0 +1,4 @@
|
||||
{{ define "footer" }}
|
||||
<footer><p>© 2025 HSP Guard</p></footer>
|
||||
{{ end }}
|
||||
|
17
templates/pages/login.html
Normal file
17
templates/pages/login.html
Normal file
@ -0,0 +1,17 @@
|
||||
{{ define "content" }}
|
||||
<form method="POST" action="/api/v1/login">
|
||||
<label for="email">
|
||||
Email:
|
||||
<input type="email" name="email" required>
|
||||
</label>
|
||||
<br>
|
||||
<label for="email">
|
||||
Password:
|
||||
<input type="password" name="password" required>
|
||||
</label>
|
||||
<br>
|
||||
<button type="submit">
|
||||
Login
|
||||
</button>
|
||||
</form>
|
||||
{{ end }}
|
21
templates/pages/register.html
Normal file
21
templates/pages/register.html
Normal file
@ -0,0 +1,21 @@
|
||||
{{ define "content" }}
|
||||
<form method="POST" action="/api/v1/register">
|
||||
<label for="email">
|
||||
Full Name:
|
||||
<input type="text" name="full_name" required>
|
||||
</label>
|
||||
<label for="email">
|
||||
Email:
|
||||
<input type="email" name="email" required>
|
||||
</label>
|
||||
<br>
|
||||
<label for="email">
|
||||
Password:
|
||||
<input type="password" name="password" required>
|
||||
</label>
|
||||
<br>
|
||||
<button type="submit">
|
||||
Register
|
||||
</button>
|
||||
</form>
|
||||
{{ end }}
|
Reference in New Issue
Block a user