MCPcopy Create free account
hub / github.com/dolanor/rip / handleEntityWithPath

Function handleEntityWithPath

handler.go:76–116  ·  view source on GitHub ↗
(
	urlPath string,
	create createFunc[Ent],
	get getFunc[Ent],
	update updateFunc[Ent],
	deleteFn deleteFunc,
	list listFunc[Ent],
	options *RouteOptions,
)

Source from the content-addressed store, hash-verified

74)
75
76func handleEntityWithPath[Ent any](
77 urlPath string,
78 create createFunc[Ent],
79 get getFunc[Ent],
80 update updateFunc[Ent],
81 deleteFn deleteFunc,
82 list listFunc[Ent],
83 options *RouteOptions,
84) (path string, handler http.HandlerFunc) {
85 handler = func(w http.ResponseWriter, r *http.Request) {
86 switch r.Method {
87 case http.MethodPost:
88 handleCreate(r.Method, urlPath, create, options)(w, r)
89 case http.MethodGet:
90 _, _, _, _, accept, editMode, err := getIDAndEditMode(w, r, r.Method, urlPath, options)
91 if err != nil {
92 writeError(w, accept, err, options)
93 return
94 }
95
96 if urlPath == r.URL.Path && editMode == encoding.EditOff {
97 handleListAll(urlPath, r.Method, list, options)(w, r)
98 return
99 }
100 handleGet(urlPath, r.Method, get, options)(w, r)
101 case http.MethodPut:
102 updatePathID(urlPath, r.Method, update, get, options)(w, r)
103 case http.MethodDelete:
104 deletePathID(urlPath, r.Method, deleteFn, options)(w, r)
105 default:
106 badMethodHandler(w, r, options)
107 }
108 }
109
110 for i := len(options.middlewares) - 1; i >= 0; i-- {
111 // we wrap the handler in the middlewares
112 handler = options.middlewares[i](handler)
113 }
114
115 return urlPath, handler
116}
117
118func resID(requestPath, prefixPath string) string {
119 pathID := strings.TrimPrefix(requestPath, prefixPath)

Callers 2

HandleEntitiesFunction · 0.85
createEntityHandlerMethod · 0.85

Calls 8

handleCreateFunction · 0.85
getIDAndEditModeFunction · 0.85
writeErrorFunction · 0.85
handleListAllFunction · 0.85
handleGetFunction · 0.85
updatePathIDFunction · 0.85
deletePathIDFunction · 0.85
badMethodHandlerFunction · 0.85

Tested by

no test coverage detected