feat (user): adjust for auth, hide pass
This commit is contained in:
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package handlercrudhelper
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
hk "github.com/karincake/hongkue"
|
||||
)
|
||||
|
||||
func RegCrud(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].(CrudBase)
|
||||
if !ok {
|
||||
panic("non CrudBase used in the last paramter")
|
||||
}
|
||||
|
||||
hk.GroupRoutes(path, r, mwList, hk.MapHandlerFunc{
|
||||
"POST /": c.Create,
|
||||
"GET /": c.GetList,
|
||||
"GET /{id}": c.GetDetail,
|
||||
"PATCH /{id}": c.Update,
|
||||
"DELETE /{id}": c.Delete,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package handlercrudhelper
|
||||
|
||||
import "net/http"
|
||||
|
||||
type CrudBase interface {
|
||||
Create(w http.ResponseWriter, r *http.Request)
|
||||
GetList(w http.ResponseWriter, r *http.Request)
|
||||
GetDetail(w http.ResponseWriter, r *http.Request)
|
||||
Update(w http.ResponseWriter, r *http.Request)
|
||||
Delete(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
Reference in New Issue
Block a user