on going division

This commit is contained in:
vanilia
2025-11-17 12:06:09 +07:00
parent b3a6820a13
commit 34b79e6472
14 changed files with 886 additions and 21 deletions
@@ -74,3 +74,36 @@ func RegCrudByCode(r *http.ServeMux, path string, mwAndRouter ...any) {
"DELETE /{code}": c.Delete,
})
}
func SyncCrud(r *http.ServeMux, path string, mwAndRouter ...any) {
sLength := len(mwAndRouter)
mwCandidates := mwAndRouter[:sLength-1]
mwList := []hk.HandlerMw{}
for i := range mwCandidates {
// have to do it manually, since casting directly results unexpected result
myType := reflect.TypeOf(mwCandidates[i])
if myType.String() != "func(http.Handler) http.Handler" {
panic("non middleware included as middleware")
}
mwList = append(mwList, mwCandidates[i].(func(http.Handler) http.Handler))
// if g, okHandler := mwCandidates[i].(func(http.Handler) http.Handler); !okHandler {
// panic("non middleware included")
// } else {
// mwList = append(mwList, g)
// }
}
c, ok := mwAndRouter[sLength-1].(SyncCrudBase)
if !ok {
panic("non CrudBase used in the last paramter")
}
hk.GroupRoutes(path, r, mwList, hk.MapHandlerFunc{
"POST /": c.Create,
"POST /log": c.CreateLog,
"PATCH /{id}": c.Update,
"DELETE /{id}": c.Delete,
})
}
+7
View File
@@ -9,3 +9,10 @@ type CrudBase interface {
Update(w http.ResponseWriter, r *http.Request)
Delete(w http.ResponseWriter, r *http.Request)
}
type SyncCrudBase interface {
Create(w http.ResponseWriter, r *http.Request)
CreateLog(w http.ResponseWriter, r *http.Request)
Update(w http.ResponseWriter, r *http.Request)
Delete(w http.ResponseWriter, r *http.Request)
}