refactor services to use Interface and change name to app
This commit is contained in:
@@ -29,7 +29,7 @@ func ListComments(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func CreateComment(w http.ResponseWriter, r *http.Request) {
|
||||
issue := r.Context().Value(issueKey).(*models.Issue)
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
form, err := ParseForm[CommentForm](r)
|
||||
@@ -47,7 +47,7 @@ func CreateComment(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
comment, err := svc.Beads.AddIssueComment(r.Context(), issue.ID, form.Author, form.Text)
|
||||
comment, err := app.Issues.AddIssueComment(r.Context(), issue.ID, form.Author, form.Text)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to create comment: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -11,14 +11,14 @@ import (
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
servicesKey contextKey = "services"
|
||||
htmxKey contextKey = "htmx"
|
||||
appKey contextKey = "app"
|
||||
htmxKey contextKey = "htmx"
|
||||
)
|
||||
|
||||
func ServicesMiddleware(svc *service.Services) func(http.Handler) http.Handler {
|
||||
func AppMiddleware(app *service.App) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.WithValue(r.Context(), servicesKey, svc)
|
||||
ctx := context.WithValue(r.Context(), appKey, app)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
@@ -33,8 +33,8 @@ func HTMXMiddleware(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func Services(r *http.Request) *service.Services {
|
||||
return r.Context().Value(servicesKey).(*service.Services)
|
||||
func App(r *http.Request) *service.App {
|
||||
return r.Context().Value(appKey).(*service.App)
|
||||
}
|
||||
|
||||
func HTMX(r *http.Request) *htmx.Handler {
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
)
|
||||
|
||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
issues, err := svc.Beads.AllIssues(r.Context())
|
||||
issues, err := app.Issues.AllIssues(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, "failed to retrieve issues", http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -30,7 +30,7 @@ type UpdateIssueForm struct {
|
||||
}
|
||||
|
||||
func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
form, err := ParseForm[IssueForm](r)
|
||||
@@ -49,7 +49,7 @@ func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
issue := form.toIssue()
|
||||
if err := svc.Beads.CreateIssue(r.Context(), &issue, ""); err != nil {
|
||||
if err := app.Issues.CreateIssue(r.Context(), &issue, ""); err != nil {
|
||||
http.Error(w, "Failed to create issue: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -64,10 +64,10 @@ func CreateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func ListIssues(w http.ResponseWriter, r *http.Request) {
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
issues, err := svc.Beads.AllIssues(r.Context())
|
||||
issues, err := app.Issues.AllIssues(r.Context())
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to retrieve issues", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -79,10 +79,10 @@ func ListIssues(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func IssueCtx(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
|
||||
id := chi.URLParam(r, "id")
|
||||
issue, err := svc.Beads.GetIssue(r.Context(), id)
|
||||
issue, err := app.Issues.GetIssue(r.Context(), id)
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting issue: "+err.Error(), http.StatusNotFound)
|
||||
return
|
||||
@@ -92,7 +92,7 @@ func IssueCtx(next http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
comments, err := svc.Beads.GetIssueComments(r.Context(), issue.ID)
|
||||
comments, err := app.Issues.GetIssueComments(r.Context(), issue.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "Error getting comments: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -132,7 +132,7 @@ func GetIssue(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func UpdateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
issue := r.Context().Value(issueKey).(*models.Issue)
|
||||
svc := Services(r)
|
||||
app := App(r)
|
||||
hx := HTMX(r)
|
||||
|
||||
form, err := ParseForm[UpdateIssueForm](r)
|
||||
@@ -152,12 +152,12 @@ func UpdateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
changes := form.toChanges()
|
||||
|
||||
if err := svc.Beads.UpdateIssue(r.Context(), issue.ID, changes, ""); err != nil {
|
||||
if err := app.Issues.UpdateIssue(r.Context(), issue.ID, changes, ""); err != nil {
|
||||
http.Error(w, "Failed to update issue", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
issue, err = svc.Beads.GetIssue(r.Context(), issue.ID)
|
||||
issue, err = app.Issues.GetIssue(r.Context(), issue.ID)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to retrieve updated issue", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -170,8 +170,8 @@ func UpdateIssue(w http.ResponseWriter, r *http.Request) {
|
||||
func DeleteIssue(w http.ResponseWriter, r *http.Request) {
|
||||
issue := r.Context().Value(issueKey).(*models.Issue)
|
||||
|
||||
svc := Services(r)
|
||||
if err := svc.Beads.DeleteIssue(r.Context(), issue.ID); err != nil {
|
||||
app := App(r)
|
||||
if err := app.Issues.DeleteIssue(r.Context(), issue.ID); err != nil {
|
||||
http.Error(w, "Failed to delete issue", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func (s *Server) RegisterRoutes(assets embed.FS) http.Handler {
|
||||
r.Use(middleware.CleanPath)
|
||||
|
||||
r.Use(handler.HTMXMiddleware)
|
||||
r.Use(handler.ServicesMiddleware(s.Services))
|
||||
r.Use(handler.AppMiddleware(s.App))
|
||||
|
||||
s.handleAssets(r, assets)
|
||||
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Address string
|
||||
Assets embed.FS
|
||||
Services *service.Services
|
||||
Address string
|
||||
Assets embed.FS
|
||||
App *service.App
|
||||
}
|
||||
|
||||
// NewServer creates and configures a new HTTP server instance.
|
||||
|
||||
@@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
"github.com/LazyBachelor/LazyPM/pkg/web/server"
|
||||
)
|
||||
|
||||
type WebConfig = service.Config
|
||||
type Config = service.Config
|
||||
|
||||
type Web struct {
|
||||
feedbackChan chan task.ValidationFeedback
|
||||
@@ -27,8 +28,8 @@ func NewWeb() *Web {
|
||||
//go:embed assets/*
|
||||
var assets embed.FS
|
||||
|
||||
func (w *Web) Run(ctx context.Context, config WebConfig) error {
|
||||
svc, cleanup, err := service.NewServices(ctx, config)
|
||||
func (w *Web) Run(ctx context.Context, config Config) error {
|
||||
app, cleanup, err := service.NewServices(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -36,16 +37,16 @@ func (w *Web) Run(ctx context.Context, config WebConfig) error {
|
||||
defer cleanup()
|
||||
|
||||
httpServer := server.NewServer(server.Server{
|
||||
Address: config.WebAddress,
|
||||
Assets: assets,
|
||||
Services: svc,
|
||||
Address: config.WebAddress,
|
||||
Assets: assets,
|
||||
App: app,
|
||||
})
|
||||
|
||||
fmt.Printf("Starting web server on %s...\n", config.WebAddress)
|
||||
|
||||
serverErr := make(chan error, 1)
|
||||
go func() {
|
||||
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
serverErr <- err
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user