This commit is contained in:
2026-01-24 15:59:05 +02:00
parent cbb3fb3421
commit b59d27d13d
12 changed files with 165 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
)
type LoginHandler struct{}
func NewLoginHandler() *LoginHandler {
return &LoginHandler{}
}
func (lh *LoginHandler) Login(c *gin.Context) {
email := c.PostForm("email")
password := c.PostForm("password")
// Simple check, replace with real auth
if email == "test@example.com" && password == "password" {
c.Header("Content-Type", "text/html")
c.String(http.StatusOK, "<p>Login successful! <a href='/dashboard'>Go to Dashboard</a></p>")
} else {
c.Header("Content-Type", "text/html")
c.String(http.StatusUnauthorized, "<p>Invalid credentials. Try again.</p>")
}
}

View File

@@ -3,12 +3,15 @@ package routes
import (
"fmt"
"net/http"
"strings"
handlers "go-server/internal/handlers"
"go-server/internal/middleware"
"go-server/internal/repository"
"go-server/internal/service"
"go-server/internal/templates"
"github.com/a-h/templ"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -36,9 +39,28 @@ func SetupRouter(db *gorm.DB) *gin.Engine {
r.Use(middleware.Logger())
r.Use(gin.Recovery())
// Serve simple text at "/"
// Cache headers for static files
r.Use(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/static/") || c.Request.URL.Path == "/favicon.ico" {
c.Header("Cache-Control", "public, max-age=31536000")
}
c.Next()
})
// Serve static files
r.Static("/static", "./static")
r.StaticFile("/favicon.ico", "./static/favicon.ico")
// Serve home page
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello")
ctx := templ.WithChildren(c.Request.Context(), templates.Home())
templates.Layout("Home").Render(ctx, c.Writer)
})
// Serve login page
r.GET("/login", func(c *gin.Context) {
ctx := templ.WithChildren(c.Request.Context(), templates.Login())
templates.Layout("Login").Render(ctx, c.Writer)
})
// Custom 404 handler
@@ -69,6 +91,7 @@ func SetupRouter(db *gorm.DB) *gin.Engine {
dailyOverviewController := handlers.NewDailyOverviewController(dailyOverviewService)
categoryController := handlers.NewCategoryController(categoryService)
todoController := handlers.NewTodoController(todoService)
loginHandler := handlers.NewLoginHandler()
skipAuth := true
@@ -119,6 +142,11 @@ func SetupRouter(db *gorm.DB) *gin.Engine {
{"GET", "/weekly", todoController.GetWeeklySummary, !skipAuth}, // Get weekly summary
},
},
"user": {
Routes: []Route{
{"POST", "/login", loginHandler.Login, false},
},
},
}
// Register all routes dynamically

View File

@@ -0,0 +1,10 @@
package templates
templ Home() {
<div>
<h1>Welcome to Futur Web App</h1>
<p>This is server-rendered with Templ and HTMX.</p>
<a href="/login">Login</a>
<a href="/dashboard">Dashboard</a>
</div>
}

View File

@@ -0,0 +1,19 @@
package templates
templ Layout(title string) {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<script src="/static/htmx.2.0.8.min.js"></script>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }
</style>
</head>
<body>
{ children... }
</body>
</html>
}

View File

@@ -0,0 +1,20 @@
package templates
templ Login() {
<div>
<h2>Login</h2>
<form hx-post="/api/user/login" hx-target="#result" hx-swap="innerHTML">
<div>
<label>Email:</label>
<input type="email" name="email" placeholder="Email" required>
</div>
<div>
<label>Password:</label>
<input type="password" name="password" placeholder="Password" required>
</div>
<button type="submit">Login</button>
</form>
<div id="result"></div>
<a href="/">Home</a>
</div>
}