49 lines
950 B
Go
49 lines
950 B
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Route represents a single API route
|
|
type Route struct {
|
|
Method string
|
|
Path string
|
|
Handler http.HandlerFunc
|
|
}
|
|
|
|
// Router is a simple HTTP router for the KAT API
|
|
type Router struct {
|
|
routes []Route
|
|
}
|
|
|
|
// NewRouter creates a new router instance
|
|
func NewRouter() *Router {
|
|
return &Router{
|
|
routes: []Route{},
|
|
}
|
|
}
|
|
|
|
// HandleFunc registers a new route with the router
|
|
func (r *Router) HandleFunc(method, path string, handler http.HandlerFunc) {
|
|
r.routes = append(r.routes, Route{
|
|
Method: strings.ToUpper(method),
|
|
Path: path,
|
|
Handler: handler,
|
|
})
|
|
}
|
|
|
|
// ServeHTTP implements the http.Handler interface
|
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
// Find matching route
|
|
for _, route := range r.routes {
|
|
if route.Method == req.Method && route.Path == req.URL.Path {
|
|
route.Handler(w, req)
|
|
return
|
|
}
|
|
}
|
|
|
|
// No route matched
|
|
http.NotFound(w, req)
|
|
}
|