feat: started adding the method routing.
This commit is contained in:
parent
93eb265886
commit
987da305b8
4 changed files with 108 additions and 0 deletions
15
contents/main.go
Normal file
15
contents/main.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package contents
|
||||
|
||||
type Type string
|
||||
|
||||
|
||||
|
||||
const (
|
||||
// Using the UTF-8 by default.
|
||||
Unknown Type = "application/octet-stream"
|
||||
Binary = Unknown
|
||||
Plain Type = "text/plain; charset=utf-8"
|
||||
Css Type = "text/css"
|
||||
Html Type = "text/html"
|
||||
Json Type = "application/json"
|
||||
)
|
40
method.go
Normal file
40
method.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package bond
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/di4f/bond/methods"
|
||||
)
|
||||
|
||||
// The type implements functionality for multiplexing
|
||||
// the methods.
|
||||
type MethodRouter struct {
|
||||
methodMap map[methods.Method] Handler
|
||||
}
|
||||
|
||||
func Method() *MethodRouter {
|
||||
ret := &MethodRouter{}
|
||||
ret.methodMap = make(map[methods.Method] Handler)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (mr *MethodRouter) Def(
|
||||
method methods.Method,
|
||||
handler Handler,
|
||||
) *MethodRouter {
|
||||
_, dup := mr.methodMap[method]
|
||||
if dup {
|
||||
panic("got the duplicate method")
|
||||
}
|
||||
|
||||
mr.methodMap[method] = handler
|
||||
return mr
|
||||
}
|
||||
|
||||
func (mr *MethodRouter) ServeHTTP(w ResponseWriter, r *Request) {
|
||||
handler, ok := mr.methodMap[methods.Method(r.Method)]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
handler.ServeHTTP(w, r)
|
||||
}
|
24
methods/main.go
Normal file
24
methods/main.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package methods
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Method string
|
||||
|
||||
const (
|
||||
Get Method = http.MethodGet
|
||||
Post Method = http.MethodPost
|
||||
Put Method = http.MethodPut
|
||||
Head Method = http.MethodHead
|
||||
Patch Method = http.MethodPatch
|
||||
Delete Method = http.MethodDelete
|
||||
Connect Method = http.MethodConnect
|
||||
Options Method = http.MethodOptions
|
||||
Trace Method = http.MethodTrace
|
||||
)
|
||||
|
||||
func (m Method) String() string {
|
||||
return string(m)
|
||||
}
|
||||
|
29
re.go
Normal file
29
re.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package bond
|
||||
|
||||
import (
|
||||
//"regexp"
|
||||
)
|
||||
|
||||
/*type ReChecker struct {
|
||||
re *regexp.Regexp
|
||||
handler ContextHandler
|
||||
}
|
||||
|
||||
func Re(re string, handler ContextHandler) *ReChecker {
|
||||
ret := &ReChecker{}
|
||||
ret.re = regexp.MustCompile(re)
|
||||
ret.handler = handler
|
||||
return ret
|
||||
}
|
||||
|
||||
func (rr *ReChecker) ServeHttp(c *Context) {
|
||||
match := rr.re.MatchString(c.Path())
|
||||
if !match {
|
||||
c.NotFound()
|
||||
return
|
||||
}
|
||||
c.re = rr.re
|
||||
c.reSubmatches = c.re.FindStringSubmatch(c.Path())
|
||||
rr.handler.ServeHttp(c)
|
||||
}*/
|
||||
|
Loading…
Reference in a new issue