Skip to content

Commit

Permalink
improve application struct
Browse files Browse the repository at this point in the history
  • Loading branch information
m1yon committed Jul 1, 2024
1 parent a67bd68 commit 86011f6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 36 deletions.
8 changes: 4 additions & 4 deletions cmd/server/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"os"
"time"

"github.com/a-h/templ"
"github.com/alexedwards/scs/v2"
"github.com/go-playground/form/v4"
"github.com/m1yon/jobsummoner/internal/components"
"github.com/m1yon/jobsummoner/internal/models"
"github.com/m1yon/jobsummoner/internal/sqlite3store"
)

type Renderer interface {
Render(ctx context.Context, w io.Writer) error
}

type application struct {
logger *slog.Logger
Render func(component templ.Component, ctx context.Context, w io.Writer) error
jobs models.JobModelInterface
users models.UserModelInterface
sessionManager *scs.SessionManager
Expand All @@ -35,7 +36,6 @@ func newApplication(logger *slog.Logger, jobs models.JobModelInterface, users mo

app := &application{
logger: logger,
Render: components.Render,
jobs: jobs,
users: users,
sessionManager: sessionManager,
Expand Down
19 changes: 0 additions & 19 deletions cmd/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"

"github.com/PuerkitoBio/goquery"
"github.com/a-h/templ"
"github.com/m1yon/jobsummoner/internal/models"
_ "github.com/m1yon/jobsummoner/internal/testing"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -51,21 +47,6 @@ func TestHomepage(t *testing.T) {
assert.Equal(t, 200, code)
assertHeadingExists(t, body, "m1yon/jobsummoner")
})

t.Run("handles a template rendering failure", func(t *testing.T) {
app := newTestApplication(t)
app.Render = func(component templ.Component, ctx context.Context, w io.Writer) error {
return errors.New("could not render template")
}

ts := newTestServer(t, app.routes())
defer ts.Close()

code, _, body := ts.get(t, "/")

assert.Equal(t, http.StatusOK, code)
assert.Contains(t, body, "Internal Server Error")
})
}

func assertHeadingExists(t *testing.T, body string, text string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (app *application) decodePostForm(r *http.Request, dst any) error {
func (app *application) render(w http.ResponseWriter, r *http.Request, status int, component templ.Component) {
w.WriteHeader(status)

err := app.Render(component, context.Background(), w)
err := templ.Component.Render(component, context.Background(), w)
if err != nil {
app.serverError(w, r, err)
return
Expand Down
12 changes: 0 additions & 12 deletions internal/components/components.go

This file was deleted.

0 comments on commit 86011f6

Please sign in to comment.