implement proper sever route handling

This commit is contained in:
Robin Olsen
2026-02-02 14:16:08 +01:00
parent 622e830f6e
commit c69fd84fdd
10 changed files with 290 additions and 35 deletions

View File

@@ -0,0 +1,21 @@
package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/internal/service"
)
type Route struct {
Pattern string
Handler http.Handler
}
func GetRoutes(svc *service.Services) []Route {
var routes []Route
routes = append(routes, PagesRoutes(svc)...)
routes = append(routes, IssuesRoutes(svc)...)
return routes
}

View File

@@ -1,22 +1,66 @@
package handler
import (
"github.com/LazyBachelor/LazyPM/internal/service"
"encoding/json"
"net/http"
"github.com/LazyBachelor/LazyPM/internal/models"
"github.com/LazyBachelor/LazyPM/internal/service"
)
type IssuesHandler struct {
Services *service.Services
}
func NewIssuesHandler(services *service.Services) *IssuesHandler {
return &IssuesHandler{
Services: services,
func IssuesRoutes(svc *service.Services) []Route {
return []Route{
{Pattern: "/issues", Handler: GetAllIssues(svc)},
{Pattern: "POST /create-issue", Handler: CreateIssue(svc)},
}
}
func (h *IssuesHandler) HandleGetAllIssues() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func CreateIssue(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
})
var issue models.Issue
err := json.NewDecoder(r.Body).Decode(&issue)
if err != nil {
http.Error(w, "Invalid request payload", http.StatusBadRequest)
return
}
defer r.Body.Close()
err = svc.Beads.CreateIssue(r.Context(), &issue, "")
if err != nil {
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(issue)
}
}
func GetAllIssues(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
issues, err := svc.Beads.AllIssues(r.Context())
if err != nil {
http.Error(w, "Failed to retrieve issues", http.StatusInternalServerError)
return
}
jsonData, err := json.Marshal(issues)
if err != nil {
http.Error(w, "Failed to marshal issues", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
}

46
pkg/web/handler/pages.go Normal file
View File

@@ -0,0 +1,46 @@
package handler
import (
"net/http"
"github.com/LazyBachelor/LazyPM/internal/service"
"github.com/LazyBachelor/LazyPM/pkg/web/components"
"github.com/LazyBachelor/LazyPM/pkg/web/routes"
)
func PagesRoutes(svc *service.Services) []Route {
return []Route{
{Pattern: "/", Handler: IndexHandler(svc)},
}
}
func IndexHandler(svc *service.Services) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
handleNotFound(w, r)
return
}
issues, err := svc.Beads.AllIssues(r.Context())
if err != nil {
http.Error(w, "Failed to retrieve issues",
http.StatusInternalServerError)
return
}
props := routes.IndexProps{
Issues: issues,
IssueList: components.IssueListProps{
Issues: issues,
},
}
routes.Index(props).Render(r.Context(), w)
}
}
func handleNotFound(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "Page not found", http.StatusNotFound)
}