...
This commit is contained in:
1
pkg/servers/ui/controllers/.gitkeep
Normal file
1
pkg/servers/ui/controllers/.gitkeep
Normal file
@@ -0,0 +1 @@
|
||||
# This file is intentionally left blank to ensure the directory is tracked by Git.
|
71
pkg/servers/ui/controllers/auth_controller.go
Normal file
71
pkg/servers/ui/controllers/auth_controller.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package controllers
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
// AuthController handles authentication-related requests.
|
||||
type AuthController struct {
|
||||
// Add dependencies like a user service or session manager here
|
||||
}
|
||||
|
||||
// NewAuthController creates a new instance of AuthController.
|
||||
func NewAuthController() *AuthController {
|
||||
return &AuthController{}
|
||||
}
|
||||
|
||||
// ShowLoginPage renders the login page.
|
||||
// @Summary Show login page
|
||||
// @Description Displays the user login form.
|
||||
// @Tags auth
|
||||
// @Produce html
|
||||
// @Success 200 {string} html "Login page HTML"
|
||||
// @Router /login [get]
|
||||
func (ac *AuthController) ShowLoginPage(c *fiber.Ctx) error {
|
||||
return c.Render("pages/login", fiber.Map{
|
||||
"Title": "Login",
|
||||
})
|
||||
}
|
||||
|
||||
// HandleLogin processes the login form submission.
|
||||
// @Summary Process user login
|
||||
// @Description Authenticates the user based on submitted credentials.
|
||||
// @Tags auth
|
||||
// @Accept x-www-form-urlencoded
|
||||
// @Produce json
|
||||
// @Param username formData string true "Username"
|
||||
// @Param password formData string true "Password"
|
||||
// @Success 302 "Redirects to dashboard on successful login"
|
||||
// @Failure 400 {object} fiber.Map "Error for invalid input"
|
||||
// @Failure 401 {object} fiber.Map "Error for authentication failure"
|
||||
// @Router /login [post]
|
||||
func (ac *AuthController) HandleLogin(c *fiber.Ctx) error {
|
||||
// username := c.FormValue("username")
|
||||
// password := c.FormValue("password")
|
||||
|
||||
// TODO: Implement actual authentication logic here.
|
||||
// For now, we'll just simulate a successful login and redirect.
|
||||
// In a real app, you would:
|
||||
// 1. Validate username and password.
|
||||
// 2. Check credentials against a user store (e.g., database).
|
||||
// 3. Create a session or token.
|
||||
|
||||
// Simulate successful login
|
||||
// c.Cookie(&fiber.Cookie{Name: "session_token", Value: "dummy_token", HttpOnly: true, SameSite: "Lax"})
|
||||
return c.Redirect("/") // Redirect to dashboard
|
||||
}
|
||||
|
||||
// HandleLogout processes the logout request.
|
||||
// @Summary Process user logout
|
||||
// @Description Logs the user out by clearing their session.
|
||||
// @Tags auth
|
||||
// @Success 302 "Redirects to login page"
|
||||
// @Router /logout [get]
|
||||
func (ac *AuthController) HandleLogout(c *fiber.Ctx) error {
|
||||
// TODO: Implement actual logout logic here.
|
||||
// For now, we'll just simulate a logout and redirect.
|
||||
// In a real app, you would:
|
||||
// 1. Invalidate the session or token.
|
||||
// 2. Clear any session-related cookies.
|
||||
|
||||
// c.ClearCookie("session_token")
|
||||
return c.Redirect("/login")
|
||||
}
|
28
pkg/servers/ui/controllers/dashboard_controller.go
Normal file
28
pkg/servers/ui/controllers/dashboard_controller.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package controllers
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
// DashboardController handles requests related to the dashboard.
|
||||
type DashboardController struct {
|
||||
// Add any dependencies here, e.g., a service to fetch dashboard data
|
||||
}
|
||||
|
||||
// NewDashboardController creates a new instance of DashboardController.
|
||||
func NewDashboardController() *DashboardController {
|
||||
return &DashboardController{}
|
||||
}
|
||||
|
||||
// ShowDashboard renders the main dashboard page.
|
||||
// @Summary Show the main dashboard
|
||||
// @Description Displays the main dashboard page with an overview.
|
||||
// @Tags dashboard
|
||||
// @Produce html
|
||||
// @Success 200 {string} html "Dashboard page HTML"
|
||||
// @Router / [get]
|
||||
func (dc *DashboardController) ShowDashboard(c *fiber.Ctx) error {
|
||||
// For now, just render the dashboard template.
|
||||
// Later, you might pass data to the template.
|
||||
return c.Render("pages/dashboard", fiber.Map{
|
||||
"Title": "Dashboard", // This can be used in base.jet {{ .Title }}
|
||||
})
|
||||
}
|
76
pkg/servers/ui/controllers/process_controller.go
Normal file
76
pkg/servers/ui/controllers/process_controller.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.ourworld.tf/herocode/heroagent/pkg/servers/ui/models"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// ProcessController handles requests related to process management.
|
||||
type ProcessController struct {
|
||||
ProcessService models.ProcessManagerService
|
||||
}
|
||||
|
||||
// NewProcessController creates a new instance of ProcessController.
|
||||
func NewProcessController(ps models.ProcessManagerService) *ProcessController {
|
||||
return &ProcessController{
|
||||
ProcessService: ps,
|
||||
}
|
||||
}
|
||||
|
||||
// ShowProcessManager renders the process manager page.
|
||||
// @Summary Show process manager
|
||||
// @Description Displays a list of running processes.
|
||||
// @Tags process
|
||||
// @Produce html
|
||||
// @Success 200 {string} html "Process manager page HTML"
|
||||
// @Failure 500 {object} fiber.Map "Error message if processes cannot be fetched"
|
||||
// @Router /processes [get]
|
||||
func (pc *ProcessController) ShowProcessManager(c *fiber.Ctx) error {
|
||||
processes, err := pc.ProcessService.GetProcesses()
|
||||
if err != nil {
|
||||
// Log the error appropriately in a real application
|
||||
fmt.Println("Error fetching processes:", err)
|
||||
return c.Status(fiber.StatusInternalServerError).Render("pages/process_manager", fiber.Map{
|
||||
"Title": "Process Manager",
|
||||
"Processes": []models.Process{}, // Empty list on error
|
||||
"Error": "Failed to retrieve process list.",
|
||||
})
|
||||
}
|
||||
|
||||
return c.Render("pages/process_manager", fiber.Map{
|
||||
"Title": "Process Manager",
|
||||
"Processes": processes,
|
||||
})
|
||||
}
|
||||
|
||||
// HandleKillProcess handles the request to kill a specific process.
|
||||
// @Summary Kill a process
|
||||
// @Description Terminates a process by its PID.
|
||||
// @Tags process
|
||||
// @Produce html
|
||||
// @Param pid path int true "Process ID"
|
||||
// @Success 302 "Redirects to process manager page"
|
||||
// @Failure 400 {object} fiber.Map "Error message if PID is invalid"
|
||||
// @Failure 500 {object} fiber.Map "Error message if process cannot be killed"
|
||||
// @Router /processes/kill/{pid} [post]
|
||||
func (pc *ProcessController) HandleKillProcess(c *fiber.Ctx) error {
|
||||
pidStr := c.Params("pid")
|
||||
pid, err := strconv.Atoi(pidStr)
|
||||
if err != nil {
|
||||
// Log error
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid PID format"})
|
||||
}
|
||||
|
||||
err = pc.ProcessService.KillProcess(pid)
|
||||
if err != nil {
|
||||
// Log error
|
||||
// In a real app, you might want to return a more user-friendly error page or message
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to kill process"})
|
||||
}
|
||||
|
||||
// Redirect back to the process manager page
|
||||
return c.Redirect("/processes")
|
||||
}
|
Reference in New Issue
Block a user