| 435 | } |
| 436 | |
| 437 | func (s *Server) Handler() http.Handler { |
| 438 | r := NewRouter() |
| 439 | |
| 440 | // Health |
| 441 | r.Route("GET", "/health", s.HealthHandler) |
| 442 | |
| 443 | // Health |
| 444 | r.Route("HEAD", "/", s.HeadHandler) |
| 445 | |
| 446 | // JSON |
| 447 | r.Route("GET", "/", s.JSONHandler).Header("Accept", jsonMediaType) |
| 448 | r.Route("GET", "/json", s.JSONHandler) |
| 449 | |
| 450 | // CLI |
| 451 | r.Route("GET", "/", s.CLIHandler).MatcherFunc(cliMatcher) |
| 452 | r.Route("GET", "/", s.CLIHandler).Header("Accept", textMediaType) |
| 453 | r.Route("GET", "/ip", s.CLIHandler) |
| 454 | if !s.gr.IsEmpty() { |
| 455 | r.Route("GET", "/country", s.CLICountryHandler) |
| 456 | r.Route("GET", "/country-iso", s.CLICountryISOHandler) |
| 457 | r.Route("GET", "/city", s.CLICityHandler) |
| 458 | r.Route("GET", "/coordinates", s.CLICoordinatesHandler) |
| 459 | r.Route("GET", "/asn", s.CLIASNHandler) |
| 460 | r.Route("GET", "/asn-org", s.CLIASNOrgHandler) |
| 461 | } |
| 462 | |
| 463 | // Browser |
| 464 | if s.Template != "" { |
| 465 | r.Route("GET", "/", s.DefaultHandler) |
| 466 | } |
| 467 | |
| 468 | // Port testing |
| 469 | if s.LookupPort != nil { |
| 470 | r.RoutePrefix("GET", "/port/", s.PortHandler) |
| 471 | } |
| 472 | |
| 473 | // Profiling |
| 474 | if s.profile { |
| 475 | r.Route("POST", "/debug/cache/resize", s.cacheResizeHandler) |
| 476 | r.Route("GET", "/debug/cache/", s.cacheHandler) |
| 477 | r.Route("GET", "/debug/pprof/cmdline", wrapHandlerFunc(pprof.Cmdline)) |
| 478 | r.Route("GET", "/debug/pprof/profile", wrapHandlerFunc(pprof.Profile)) |
| 479 | r.Route("GET", "/debug/pprof/symbol", wrapHandlerFunc(pprof.Symbol)) |
| 480 | r.Route("GET", "/debug/pprof/trace", wrapHandlerFunc(pprof.Trace)) |
| 481 | r.RoutePrefix("GET", "/debug/pprof/", wrapHandlerFunc(pprof.Index)) |
| 482 | } |
| 483 | |
| 484 | return r.Handler() |
| 485 | } |
| 486 | |
| 487 | func (s *Server) ListenAndServe(addr string) error { |
| 488 | return http.ListenAndServe(addr, s.Handler()) |