You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
707 B
Go
37 lines
707 B
Go
3 years ago
|
package icshttp
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type Router struct {
|
||
|
handlers map[string]map[string]http.HandlerFunc //[method][pattern]
|
||
|
}
|
||
|
|
||
|
func NewRouter() *Router {
|
||
|
return &Router{make(map[string]map[string]http.HandlerFunc)}
|
||
|
}
|
||
|
|
||
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||
|
if m, ok := r.handlers[req.Method]; ok {
|
||
|
if h, ok := m[req.URL.Path]; ok {
|
||
|
h(w, req)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
http.NotFound(w, req)
|
||
|
}
|
||
|
|
||
|
//register http handler function
|
||
|
func (r *Router) HandleFunc(method, pattern string, h http.HandlerFunc) {
|
||
|
v, ok := r.handlers[method]
|
||
|
if !ok {
|
||
|
v = make(map[string]http.HandlerFunc)
|
||
|
r.handlers[method] = v
|
||
|
}
|
||
|
v[pattern] = h
|
||
|
}
|
||
|
|
||
|
//{"Text":"aaabbb"},{"Speaker_Name":"JJ"}
|