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"}
