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
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"

"github.com/joeynolan/go-http-server/internal/http/handlers"
apphttp "github.com/joeynolan/go-http-server/internal/http"
"github.com/joeynolan/go-http-server/internal/platform/config"
ilog "github.com/joeynolan/go-http-server/internal/platform/log"
)
Expand All @@ -29,7 +29,7 @@ func main() {
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)

handlers.Register(r)
apphttp.Register(r)

srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Expand Down
2 changes: 1 addition & 1 deletion internal/http/handlers/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
)

func healthHandler(w http.ResponseWriter, r *http.Request) {
func HealthHandler(w http.ResponseWriter, r *http.Request) {
resp := map[string]string{"status": "ok"}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
Expand Down
17 changes: 12 additions & 5 deletions internal/http/handlers/redirect.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/go-chi/chi/v5"
)

func redirectHandler(w http.ResponseWriter, r *http.Request) {
resp := map[string]string{"status": "ok", "message": "redirect link"}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
func RedirectHandler(w http.ResponseWriter, r *http.Request) {

code := chi.URLParam(r, "code")
if code == "" {
WriteError(w, http.StatusBadRequest, "missing code")
return
}

resp := map[string]string{"message": "redirect link"}
WriteJSON(w, http.StatusFound, resp)
}
25 changes: 25 additions & 0 deletions internal/http/handlers/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handlers

import (
"encoding/json"
"net/http"
)

// ErrorResponse is a simple JSON error body with a human-friendly message.
type ErrorResponse struct {
Message string `json:"message"`
}

// WriteJSON writes v as a JSON response with the provided status code and
// sets the Content-Type to application/json; charset=utf-8.
func WriteJSON(w http.ResponseWriter, status int, v interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}

// WriteError writes a standardized JSON error response containing only a
// status code and a message. Example body: {"message":"not found"}
func WriteError(w http.ResponseWriter, status int, message string) {
WriteJSON(w, status, ErrorResponse{Message: message})
}
9 changes: 0 additions & 9 deletions internal/http/handlers/router.go

This file was deleted.

25 changes: 21 additions & 4 deletions internal/http/handlers/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ import (
"net/http"
)

func shortenHandler(w http.ResponseWriter, r *http.Request) {
resp := map[string]string{"status": "ok", "message": "shorten endpoint"}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(resp)
func ShortenHandler(w http.ResponseWriter, r *http.Request) {

var req struct {
URL string `json:"url"`
Expiry int `json:"expiry,omitempty"`
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
WriteError(w, http.StatusBadRequest, "invalid JSON")
return
}

if req.URL == "" {
WriteError(w, http.StatusBadRequest, "url is required")
return
}

resp := map[string]string{
"short": "https://short.example/abc123", // generated value
}
WriteJSON(w, http.StatusCreated, resp)
}
12 changes: 12 additions & 0 deletions internal/http/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package http

import (
"github.com/go-chi/chi/v5"
"github.com/joeynolan/go-http-server/internal/http/handlers"
)

func Register(r chi.Router) {
r.Get("/health", handlers.HealthHandler)
r.Get("/v1/r/{code}", handlers.RedirectHandler)
r.Post("/v1/shorten", handlers.ShortenHandler)
}