Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions handler/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package handler
import (
"encoding/json"
"log/slog"
baseHttp "net/http"
"net/http"

"github.com/oullin/database"
"github.com/oullin/database/repository"
"github.com/oullin/database/repository/pagination"
"github.com/oullin/handler/paginate"
"github.com/oullin/handler/payload"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
)

type CategoriesHandler struct {
Expand All @@ -23,14 +23,14 @@ func MakeCategoriesHandler(categories *repository.Categories) CategoriesHandler
}
}

func (h *CategoriesHandler) Index(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h *CategoriesHandler) Index(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
result, err := h.Categories.GetAll(
paginate.MakeFrom(r.URL, 5),
)

if err != nil {
slog.Error("Error getting categories", "err", err)
return http.InternalError("Error getting categories")
return endpoint.InternalError("Error getting categories")
}

items := pagination.HydratePagination(
Expand All @@ -48,7 +48,7 @@ func (h *CategoriesHandler) Index(w baseHttp.ResponseWriter, r *baseHttp.Request
if err := json.NewEncoder(w).Encode(items); err != nil {
slog.Error("failed to encode response", "err", err)

return http.InternalError("There was an issue processing the response. Please, try later.")
return endpoint.InternalError("There was an issue processing the response. Please, try later.")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions handler/education.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package handler

import (
"github.com/oullin/handler/payload"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"

"log/slog"
baseHttp "net/http"
"net/http"
)

type EducationHandler struct {
Expand All @@ -19,16 +19,16 @@ func MakeEducationHandler(filePath string) EducationHandler {
}
}

func (h EducationHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h EducationHandler) Handle(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
data, err := portal.ParseJsonFile[payload.EducationResponse](h.filePath)

if err != nil {
slog.Error("Error reading education file", "error", err)

return http.InternalError("could not read education data")
return endpoint.InternalError("could not read education data")
}

resp := http.MakeResponseFrom(data.Version, w, r)
resp := endpoint.MakeResponseFrom(data.Version, w, r)

if resp.HasCache() {
resp.RespondWithNotModified()
Expand Down
10 changes: 5 additions & 5 deletions handler/experience.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package handler

import (
"github.com/oullin/handler/payload"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"

"log/slog"
baseHttp "net/http"
"net/http"
)

type ExperienceHandler struct {
Expand All @@ -19,16 +19,16 @@ func MakeExperienceHandler(filePath string) ExperienceHandler {
}
}

func (h ExperienceHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h ExperienceHandler) Handle(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
data, err := portal.ParseJsonFile[payload.ExperienceResponse](h.filePath)

if err != nil {
slog.Error("Error reading experience file", "error", err)

return http.InternalError("could not read experience data")
return endpoint.InternalError("could not read experience data")
}

resp := http.MakeResponseFrom(data.Version, w, r)
resp := endpoint.MakeResponseFrom(data.Version, w, r)

if resp.HasCache() {
resp.RespondWithNotModified()
Expand Down
10 changes: 5 additions & 5 deletions handler/file_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package handler

import (
"encoding/json"
baseHttp "net/http"
"net/http"
"net/http/httptest"
"os"
"testing"

handlertests "github.com/oullin/handler/tests"
pkgHttp "github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
)

type fileHandler interface {
Handle(baseHttp.ResponseWriter, *baseHttp.Request) *pkgHttp.ApiError
Handle(http.ResponseWriter, *http.Request) *endpoint.ApiError
}

type fileHandlerTestCase struct {
Expand Down Expand Up @@ -44,7 +44,7 @@ func runFileHandlerTest(t *testing.T, tc fileHandlerTestCase) {
t.Fatalf("err: %v", err)
}

if rec.Code != baseHttp.StatusOK {
if rec.Code != http.StatusOK {
t.Fatalf("status %d", rec.Code)
}

Expand All @@ -68,7 +68,7 @@ func runFileHandlerTest(t *testing.T, tc fileHandlerTestCase) {
t.Fatalf("err: %v", err)
}

if rec2.Code != baseHttp.StatusNotModified {
if rec2.Code != http.StatusNotModified {
t.Fatalf("status %d", rec2.Code)
}

Expand Down
12 changes: 6 additions & 6 deletions handler/keep_alive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package handler

import (
"fmt"
baseHttp "net/http"
"net/http"
"time"

"github.com/oullin/handler/payload"
"github.com/oullin/metal/env"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"
)

Expand All @@ -19,17 +19,17 @@ func MakeKeepAliveHandler(e *env.PingEnvironment) KeepAliveHandler {
return KeepAliveHandler{env: e}
}

func (h KeepAliveHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h KeepAliveHandler) Handle(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
user, pass, ok := r.BasicAuth()

if !ok || h.env.HasInvalidCreds(user, pass) {
return http.LogUnauthorisedError(
return endpoint.LogUnauthorisedError(
"invalid credentials",
fmt.Errorf("invalid credentials"),
)
}

resp := http.MakeNoCacheResponse(w, r)
resp := endpoint.MakeNoCacheResponse(w, r)
now := time.Now().UTC()

data := payload.KeepAliveResponse{
Expand All @@ -38,7 +38,7 @@ func (h KeepAliveHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request)
}

if err := resp.RespondOk(data); err != nil {
return http.LogInternalError("could not encode keep-alive response", err)
return endpoint.LogInternalError("could not encode keep-alive response", err)
}

return nil
Expand Down
14 changes: 7 additions & 7 deletions handler/keep_alive_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package handler

import (
"fmt"
baseHttp "net/http"
"net/http"
"time"

"github.com/oullin/database"
"github.com/oullin/handler/payload"
"github.com/oullin/metal/env"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"
)

Expand All @@ -21,21 +21,21 @@ func MakeKeepAliveDBHandler(e *env.PingEnvironment, db *database.Connection) Kee
return KeepAliveDBHandler{env: e, db: db}
}

func (h KeepAliveDBHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h KeepAliveDBHandler) Handle(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
user, pass, ok := r.BasicAuth()

if !ok || h.env.HasInvalidCreds(user, pass) {
return http.LogUnauthorisedError(
return endpoint.LogUnauthorisedError(
"invalid credentials",
fmt.Errorf("invalid credentials"),
)
}

if err := h.db.Ping(); err != nil {
return http.LogInternalError("database ping failed", err)
return endpoint.LogInternalError("database ping failed", err)
}

resp := http.MakeNoCacheResponse(w, r)
resp := endpoint.MakeNoCacheResponse(w, r)
now := time.Now().UTC()

data := payload.KeepAliveResponse{
Expand All @@ -44,7 +44,7 @@ func (h KeepAliveDBHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Reques
}

if err := resp.RespondOk(data); err != nil {
return http.LogInternalError("could not encode keep-alive response", err)
return endpoint.LogInternalError("could not encode keep-alive response", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions handler/payload/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/oullin/database/repository/queries"
"github.com/oullin/pkg/portal"

baseHttp "net/http"
"net/http"
"strings"
"time"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func GetPostsFiltersFrom(request IndexRequestBody) queries.PostFilters {
}
}

func GetSlugFrom(r *baseHttp.Request) string {
func GetSlugFrom(r *http.Request) string {
str := portal.MakeStringable(r.PathValue("slug"))

return strings.TrimSpace(str.ToLower())
Expand Down
22 changes: 11 additions & 11 deletions handler/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/oullin/database/repository/pagination"
"github.com/oullin/handler/paginate"
"github.com/oullin/handler/payload"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"

"log/slog"
baseHttp "net/http"
"net/http"
)

type PostsHandler struct {
Expand All @@ -23,15 +23,15 @@ func MakePostsHandler(repo *repository.Posts) PostsHandler {
return PostsHandler{Posts: repo}
}

func (h *PostsHandler) Index(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h *PostsHandler) Index(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
defer portal.CloseWithLog(r.Body)

requestBody, err := http.ParseRequestBody[payload.IndexRequestBody](r)
requestBody, err := endpoint.ParseRequestBody[payload.IndexRequestBody](r)

if err != nil {
slog.Error("failed to parse request body", "err", err)

return http.InternalError("There was an issue reading the request. Please, try again later.")
return endpoint.InternalError("There was an issue reading the request. Please, try again later.")
}

result, err := h.Posts.GetAll(
Expand All @@ -42,7 +42,7 @@ func (h *PostsHandler) Index(w baseHttp.ResponseWriter, r *baseHttp.Request) *ht
if err != nil {
slog.Error("failed to fetch posts", "err", err)

return http.InternalError("There was an issue reading the posts. Please, try again later.")
return endpoint.InternalError("There was an issue reading the posts. Please, try again later.")
}

items := pagination.HydratePagination(
Expand All @@ -53,29 +53,29 @@ func (h *PostsHandler) Index(w baseHttp.ResponseWriter, r *baseHttp.Request) *ht
if err := json.NewEncoder(w).Encode(items); err != nil {
slog.Error("failed to encode response", "err", err)

return http.InternalError("There was an issue processing the response. Please, try later.")
return endpoint.InternalError("There was an issue processing the response. Please, try later.")
}

return nil
}

func (h *PostsHandler) Show(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h *PostsHandler) Show(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
slug := payload.GetSlugFrom(r)

if slug == "" {
return http.BadRequestError("Slugs are required to show posts content")
return endpoint.BadRequestError("Slugs are required to show posts content")
}

post := h.Posts.FindBy(slug)
if post == nil {
return http.NotFound(fmt.Sprintf("The given post '%s' was not found", slug))
return endpoint.NotFound(fmt.Sprintf("The given post '%s' was not found", slug))
}

items := payload.GetPostsResponse(*post)
if err := json.NewEncoder(w).Encode(items); err != nil {
slog.Error(err.Error())

return http.InternalError("There was an issue processing the response. Please, try later.")
return endpoint.InternalError("There was an issue processing the response. Please, try later.")
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions handler/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package handler

import (
"github.com/oullin/handler/payload"
"github.com/oullin/pkg/http"
"github.com/oullin/pkg/endpoint"
"github.com/oullin/pkg/portal"

"log/slog"
baseHttp "net/http"
"net/http"
)

type ProfileHandler struct {
Expand All @@ -19,16 +19,16 @@ func MakeProfileHandler(filePath string) ProfileHandler {
}
}

func (h ProfileHandler) Handle(w baseHttp.ResponseWriter, r *baseHttp.Request) *http.ApiError {
func (h ProfileHandler) Handle(w http.ResponseWriter, r *http.Request) *endpoint.ApiError {
data, err := portal.ParseJsonFile[payload.ProfileResponse](h.filePath)

if err != nil {
slog.Error("Error reading profile file", "error", err)

return http.InternalError("could not read profile data")
return endpoint.InternalError("could not read profile data")
}

resp := http.MakeResponseFrom(data.Version, w, r)
resp := endpoint.MakeResponseFrom(data.Version, w, r)

if resp.HasCache() {
resp.RespondWithNotModified()
Expand Down
Loading
Loading