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
7 changes: 4 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/artmoskvin/hide/pkg/files"
"github.com/artmoskvin/hide/pkg/handlers"
"github.com/artmoskvin/hide/pkg/lsp"
"github.com/artmoskvin/hide/pkg/middleware"
"github.com/artmoskvin/hide/pkg/model"
"github.com/artmoskvin/hide/pkg/project"
"github.com/artmoskvin/hide/pkg/random"
Expand Down Expand Up @@ -114,9 +115,9 @@ var runCmd = &cobra.Command{
WithListTasksHandler(handlers.ListTasksHandler{Manager: projectManager}).
WithCreateFileHandler(handlers.CreateFileHandler{ProjectManager: projectManager}).
WithListFilesHandler(handlers.ListFilesHandler{ProjectManager: projectManager}).
WithReadFileHandler(handlers.ReadFileHandler{ProjectManager: projectManager}).
WithUpdateFileHandler(handlers.UpdateFileHandler{ProjectManager: projectManager}).
WithDeleteFileHandler(handlers.DeleteFileHandler{ProjectManager: projectManager}).
WithReadFileHandler(middleware.PathValidator(handlers.ReadFileHandler{ProjectManager: projectManager})).
WithUpdateFileHandler(middleware.PathValidator(handlers.UpdateFileHandler{ProjectManager: projectManager})).
WithDeleteFileHandler(middleware.PathValidator(handlers.DeleteFileHandler{ProjectManager: projectManager})).
WithSearchFileHandler(handlers.SearchFilesHandler{ProjectManager: projectManager}).
WithSearchSymbolsHandler(handlers.NewSearchSymbolsHandler(projectManager)).
Build()
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/delete_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (h DeleteFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

filePath, err := getFilePath(r)
filePath, err := GetFilePath(r)
if err != nil {
http.Error(w, "invalid file path", http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/delete_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/artmoskvin/hide/pkg/files"
"github.com/artmoskvin/hide/pkg/handlers"
"github.com/artmoskvin/hide/pkg/middleware"
"github.com/artmoskvin/hide/pkg/project"
"github.com/artmoskvin/hide/pkg/project/mocks"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestDeleteFileHandler_ServeHTTP(t *testing.T) {
req := httptest.NewRequest(http.MethodDelete, tt.target, nil)
rr := httptest.NewRecorder()

router := handlers.NewRouter().WithDeleteFileHandler(handler).Build()
router := handlers.NewRouter().WithDeleteFileHandler(middleware.PathValidator(handler)).Build()
router.ServeHTTP(rr, req)

assert.Equal(t, tt.wantStatusCode, rr.Code)
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/read_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (h ReadFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

filePath, err := getFilePath(r)
filePath, err := GetFilePath(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid file path: %s", err), http.StatusBadRequest)
return
Expand All @@ -42,7 +42,6 @@ func (h ReadFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

file, err := h.ProjectManager.ReadFile(r.Context(), projectID, filePath)

if err != nil {
var projectNotFoundError *project.ProjectNotFoundError
if errors.As(err, &projectNotFoundError) {
Expand Down
30 changes: 16 additions & 14 deletions pkg/handlers/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handlers

import (
"net/http"

"github.com/gorilla/mux"
)

Expand All @@ -16,57 +18,57 @@ func NewRouter() *Router {
return r
}

func (r *Router) WithCreateProjectHandler(handler CreateProjectHandler) *Router {
func (r *Router) WithCreateProjectHandler(handler http.Handler) *Router {
r.Handle("/projects", handler).Methods("POST")
return r
}

func (r *Router) WithDeleteProjectHandler(handler DeleteProjectHandler) *Router {
func (r *Router) WithDeleteProjectHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}", handler).Methods("DELETE")
return r
}

func (r *Router) WithCreateTaskHandler(handler CreateTaskHandler) *Router {
func (r *Router) WithCreateTaskHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/tasks", handler).Methods("POST")
return r
}

func (r *Router) WithListTasksHandler(handler ListTasksHandler) *Router {
func (r *Router) WithListTasksHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/tasks", handler).Methods("GET")
return r
}

func (r *Router) WithCreateFileHandler(handler CreateFileHandler) *Router {
func (r *Router) WithCreateFileHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/files", handler).Methods("POST")
return r
}

func (r *Router) WithListFilesHandler(handler ListFilesHandler) *Router {
func (r *Router) WithListFilesHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/files", handler).Methods("GET")
return r
}

func (r *Router) WithReadFileHandler(handler ReadFileHandler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", PathValidator(handler)).Methods("GET")
func (r *Router) WithReadFileHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", handler).Methods("GET")
return r
}

func (r *Router) WithUpdateFileHandler(handler UpdateFileHandler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", PathValidator(handler)).Methods("PUT")
func (r *Router) WithUpdateFileHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", handler).Methods("PUT")
return r
}

func (r *Router) WithDeleteFileHandler(handler DeleteFileHandler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", PathValidator(handler)).Methods("DELETE")
func (r *Router) WithDeleteFileHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/files/{path:.*}", handler).Methods("DELETE")
return r
}

func (r *Router) WithSearchFileHandler(handler SearchFilesHandler) *Router {
func (r *Router) WithSearchFileHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/search", handler).Queries("type", "content", "query", "").Methods("GET")
return r
}

func (r *Router) WithSearchSymbolsHandler(handler SearchSymbolsHandler) *Router {
func (r *Router) WithSearchSymbolsHandler(handler http.Handler) *Router {
r.Handle("/projects/{id}/search", handler).Queries("type", "symbol", "query", "").Methods("GET")
return r
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/update_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (h UpdateFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

filePath, err := getFilePath(r)
filePath, err := GetFilePath(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid file path: %s", err), http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/handlers/update_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/artmoskvin/hide/pkg/files"
"github.com/artmoskvin/hide/pkg/handlers"
"github.com/artmoskvin/hide/pkg/middleware"
"github.com/artmoskvin/hide/pkg/model"
"github.com/artmoskvin/hide/pkg/project"
project_mocks "github.com/artmoskvin/hide/pkg/project/mocks"
Expand Down Expand Up @@ -288,7 +289,7 @@ func TestPathStartingWithSlash(t *testing.T) {
}

handler := handlers.UpdateFileHandler{ProjectManager: mockManager}
router := handlers.NewRouter().WithUpdateFileHandler(handler).Build()
router := handlers.NewRouter().WithUpdateFileHandler(middleware.PathValidator(handler)).Build()

payload, _ := json.Marshal(handlers.UpdateFileRequest{
Type: handlers.LineDiff,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func getProjectID(r *http.Request) (string, error) {
return getPathValue(r, "id")
}

func getFilePath(r *http.Request) (string, error) {
func GetFilePath(r *http.Request) (string, error) {
return getPathValue(r, "path")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package handlers
package middleware

import (
"fmt"
"net/http"
"strings"

"github.com/artmoskvin/hide/pkg/handlers"
"github.com/rs/zerolog/log"
)

func PathValidator(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Debug().Msg("Invoking PathChecker")

filePath, err := getFilePath(r)
filePath, err := handlers.GetFilePath(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid file path: %s", err), http.StatusBadRequest)
return
Expand Down