several bugfixes, add internal.IntLen, round up http-linux example with new router API

This commit is contained in:
Patricio Whittingslow
2026-07-25 22:09:49 -03:00
parent d4666630f7
commit a7f598f975
8 changed files with 233 additions and 136 deletions
+51
View File
@@ -0,0 +1,51 @@
package httphi
import (
"strings"
"github.com/soypat/lneto/internal"
)
type Mux interface {
LookupHandler(get Method, uri []byte) HandlerFunc
}
type MuxSlice struct {
// TODO: binary search worth it?
_handlers []struct {
method Method
uri string
handler HandlerFunc
}
}
func (sm *MuxSlice) Reset(capacity int) {
internal.SliceReuse(&sm._handlers, capacity)
}
func (sm *MuxSlice) LookupHandler(method Method, uri []byte) HandlerFunc {
for _, endpoint := range sm._handlers {
if endpoint.method != MethUndefined && endpoint.method != method {
continue
}
// Method matches.
if b2s(uri) == endpoint.uri {
return endpoint.handler
}
}
return nil
}
func (sm *MuxSlice) Handle(reg string, handler HandlerFunc) {
v := internal.SliceReclaim(&sm._handlers)
method := MethUndefined
methodOrURL, url, methodFound := strings.Cut(reg, " ")
if methodFound {
method = MethodFromBytes([]byte(methodOrURL))
} else {
url = methodOrURL
}
v.method = method
v.uri = url
v.handler = handler
}