From b421639f5ea447ae3df88b6a59132d42efeec5ef Mon Sep 17 00:00:00 2001 From: Thanatat Tamtan Date: Fri, 7 Mar 2025 14:57:13 +0700 Subject: [PATCH] add mounter helper --- arpc.go | 12 ++++++++++++ mounter.go | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 mounter.go diff --git a/arpc.go b/arpc.go index a4e6c32..450a41b 100644 --- a/arpc.go +++ b/arpc.go @@ -45,6 +45,10 @@ type Validatable interface { Valid() error } +type Mux interface { + Handle(pattern string, handler http.Handler) +} + type Manager struct { Decoder Decoder Encoder Encoder @@ -357,6 +361,14 @@ func (m *Manager) Handler(f any) http.Handler { }) } +func (m *Manager) Mount(mux Mux, pattern string, f any) { + mux.Handle(pattern, m.Handler(f)) +} + +func (m *Manager) Mounter(mux Mux) *Mounter { + return &Mounter{Manager: m, Mux: mux} +} + type MiddlewareContext struct { r *http.Request w http.ResponseWriter diff --git a/mounter.go b/mounter.go new file mode 100644 index 0000000..0734559 --- /dev/null +++ b/mounter.go @@ -0,0 +1,11 @@ +package arpc + +type Mounter struct { + Manager *Manager + Mux Mux +} + +// Mount mounts the handler to the mux +func (m *Mounter) Mount(pattern string, f any) { + m.Manager.Mount(m.Mux, pattern, f) +}