(p *Proxy)
| 27 | } |
| 28 | |
| 29 | func newApiServer(p *Proxy) http.Handler { |
| 30 | m := martini.New() |
| 31 | m.Use(martini.Recovery()) |
| 32 | m.Use(render.Renderer()) |
| 33 | m.Use(func(w http.ResponseWriter, req *http.Request, c martini.Context) { |
| 34 | path := req.URL.Path |
| 35 | if req.Method != "GET" && strings.HasPrefix(path, "/api/") { |
| 36 | var remoteAddr = req.RemoteAddr |
| 37 | var headerAddr string |
| 38 | for _, key := range []string{"X-Real-IP", "X-Forwarded-For"} { |
| 39 | if val := req.Header.Get(key); val != "" { |
| 40 | headerAddr = val |
| 41 | break |
| 42 | } |
| 43 | } |
| 44 | log.Warnf("[%p] API call %s from %s [%s]", p, path, remoteAddr, headerAddr) |
| 45 | } |
| 46 | c.Next() |
| 47 | }) |
| 48 | m.Use(gzip.All()) |
| 49 | m.Use(func(c martini.Context, w http.ResponseWriter) { |
| 50 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 51 | }) |
| 52 | |
| 53 | api := &apiServer{proxy: p} |
| 54 | |
| 55 | r := martini.NewRouter() |
| 56 | r.Get("/", func(r render.Render) { |
| 57 | r.Redirect("/proxy") |
| 58 | }) |
| 59 | r.Any("/debug/**", func(w http.ResponseWriter, req *http.Request) { |
| 60 | http.DefaultServeMux.ServeHTTP(w, req) |
| 61 | }) |
| 62 | |
| 63 | r.Group("/proxy", func(r martini.Router) { |
| 64 | r.Get("", api.Overview) |
| 65 | r.Get("/model", api.Model) |
| 66 | r.Get("/stats", api.StatsNoXAuth) |
| 67 | r.Get("/slots", api.SlotsNoXAuth) |
| 68 | }) |
| 69 | r.Group("/api/proxy", func(r martini.Router) { |
| 70 | r.Get("/model", api.Model) |
| 71 | r.Get("/xping/:xauth", api.XPing) |
| 72 | r.Get("/stats/:xauth", api.Stats) |
| 73 | r.Get("/stats/:xauth/:flags", api.Stats) |
| 74 | r.Get("/slots/:xauth", api.Slots) |
| 75 | r.Put("/start/:xauth", api.Start) |
| 76 | r.Put("/stats/reset/:xauth", api.ResetStats) |
| 77 | r.Put("/forcegc/:xauth", api.ForceGC) |
| 78 | r.Put("/shutdown/:xauth", api.Shutdown) |
| 79 | r.Put("/loglevel/:xauth/:value", api.LogLevel) |
| 80 | r.Put("/fillslots/:xauth", binding.Json([]*models.Slot{}), api.FillSlots) |
| 81 | r.Put("/sentinels/:xauth", binding.Json(models.Sentinel{}), api.SetSentinels) |
| 82 | r.Put("/sentinels/:xauth/rewatch", api.RewatchSentinels) |
| 83 | }) |
| 84 | |
| 85 | m.MapTo(r, (*martini.Routes)(nil)) |
| 86 | m.Action(r.Handle) |
no test coverage detected