(t *Topom)
| 29 | } |
| 30 | |
| 31 | func newApiServer(t *Topom) http.Handler { |
| 32 | m := martini.New() |
| 33 | m.Use(martini.Recovery()) |
| 34 | m.Use(render.Renderer()) |
| 35 | m.Use(func(w http.ResponseWriter, req *http.Request, c martini.Context) { |
| 36 | path := req.URL.Path |
| 37 | if req.Method != "GET" && strings.HasPrefix(path, "/api/") { |
| 38 | var remoteAddr = req.RemoteAddr |
| 39 | var headerAddr string |
| 40 | for _, key := range []string{"X-Real-IP", "X-Forwarded-For"} { |
| 41 | if val := req.Header.Get(key); val != "" { |
| 42 | headerAddr = val |
| 43 | break |
| 44 | } |
| 45 | } |
| 46 | log.Warnf("[%p] API call %s from %s [%s]", t, path, remoteAddr, headerAddr) |
| 47 | } |
| 48 | c.Next() |
| 49 | }) |
| 50 | m.Use(gzip.All()) |
| 51 | m.Use(func(c martini.Context, w http.ResponseWriter) { |
| 52 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 53 | }) |
| 54 | |
| 55 | api := &apiServer{topom: t} |
| 56 | |
| 57 | r := martini.NewRouter() |
| 58 | |
| 59 | r.Get("/", func(r render.Render) { |
| 60 | r.Redirect("/topom") |
| 61 | }) |
| 62 | r.Any("/debug/**", func(w http.ResponseWriter, req *http.Request) { |
| 63 | http.DefaultServeMux.ServeHTTP(w, req) |
| 64 | }) |
| 65 | |
| 66 | r.Group("/topom", func(r martini.Router) { |
| 67 | r.Get("", api.Overview) |
| 68 | r.Get("/model", api.Model) |
| 69 | r.Get("/stats", api.StatsNoXAuth) |
| 70 | r.Get("/slots", api.SlotsNoXAuth) |
| 71 | }) |
| 72 | r.Group("/api/topom", func(r martini.Router) { |
| 73 | r.Get("/model", api.Model) |
| 74 | r.Get("/xping/:xauth", api.XPing) |
| 75 | r.Get("/stats/:xauth", api.Stats) |
| 76 | r.Get("/slots/:xauth", api.Slots) |
| 77 | r.Put("/reload/:xauth", api.Reload) |
| 78 | r.Put("/shutdown/:xauth", api.Shutdown) |
| 79 | r.Put("/loglevel/:xauth/:value", api.LogLevel) |
| 80 | r.Group("/proxy", func(r martini.Router) { |
| 81 | r.Put("/create/:xauth/:addr", api.CreateProxy) |
| 82 | r.Put("/online/:xauth/:addr", api.OnlineProxy) |
| 83 | r.Put("/reinit/:xauth/:token", api.ReinitProxy) |
| 84 | r.Put("/remove/:xauth/:token/:force", api.RemoveProxy) |
| 85 | }) |
| 86 | r.Group("/group", func(r martini.Router) { |
| 87 | r.Put("/create/:xauth/:gid", api.CreateGroup) |
| 88 | r.Put("/remove/:xauth/:gid", api.RemoveGroup) |
no test coverage detected