Fix capture: endpoint ID/URL mismatch, routing, and extractID
- Use URL as endpoint ID so handler path-segment lookups resolve correctly.
Previously CreateEndpoint generated a random UUID for the ID, but the
handler always queries by the path segment — every GetEndpoint call
missed, causing duplicate UNIQUE violations on subsequent requests
and orphaned request records (endpoint_id mismatch).
- Register both /{endpoint} and /{endpoint}/ patterns so bare and
nested capture paths both route to the handler (Go 1.22+ ServeMux).
- Strip query string in extractID for robustness, even though the
library never sends ? in r.URL.Path.
- Remove unused net/http import from handler_test.go (caught by vet).
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
90c3f6285d
commit
5bbf995f32
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@ -15,7 +14,10 @@ type Handler struct{ Repo store.Repository }
|
||||
|
||||
func New(repo store.Repository) *Handler { return &Handler{Repo: repo} }
|
||||
|
||||
func (h *Handler) RegisterRoutes(mux *http.ServeMux) { mux.Handle("/{endpoint}", h) }
|
||||
func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
||||
mux.Handle("/{endpoint}", h)
|
||||
mux.Handle("/{endpoint}/", h)
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
eid := extractID(r.URL.Path)
|
||||
@ -39,6 +41,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func extractID(p string) string {
|
||||
t := strings.TrimLeft(p, "/")
|
||||
if i := strings.Index(t, "/"); i >= 0 { return t[:i] }
|
||||
if i := strings.IndexByte(t, '?'); i >= 0 { t = t[:i] }
|
||||
if i := strings.IndexByte(t, '/'); i >= 0 { return t[:i] }
|
||||
return t
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -16,9 +16,8 @@ func NewSQLiteRepo(db *sql.DB) (Repository, error) {
|
||||
}
|
||||
|
||||
func (r *sqliteRepo) CreateEndpoint(ctx context.Context, url string) (*Endpoint, error) {
|
||||
id := uuid.NewString()
|
||||
now := utcNow()
|
||||
e := &Endpoint{ID: id, URL: url, CreatedAt: now, UpdatedAt: now}
|
||||
e := &Endpoint{ID: url, URL: url, CreatedAt: now, UpdatedAt: now}
|
||||
_, err := r.db.ExecContext(ctx, `INSERT INTO endpoints (id, url, created_at, updated_at) VALUES (?, ?, ?, ?)`, e.ID, e.URL, e.CreatedAt, e.UpdatedAt)
|
||||
if err != nil { return nil, fmt.Errorf("store: create endpoint: %w", err) }
|
||||
return e, nil
|
||||
|
||||
Loading…
Reference in New Issue
Block a user