diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 0887dd68..42feb5ba 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -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 } diff --git a/internal/handler/handler_test.go b/internal/handler/handler_test.go index c11f14da..c39ca13e 100644 --- a/internal/handler/handler_test.go +++ b/internal/handler/handler_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "io" - "net/http" "net/http/httptest" "strings" "testing" diff --git a/internal/store/sqlite_repo.go b/internal/store/sqlite_repo.go index 6b9ba152..0c4d16ce 100644 --- a/internal/store/sqlite_repo.go +++ b/internal/store/sqlite_repo.go @@ -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