diff --git a/agent/agent.go b/agent/agent.go index b4f1bf0b..316e65f0 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -14,7 +14,6 @@ import ( "time" log "github.com/Sirupsen/logrus" - "github.com/gin-gonic/gin" "github.com/Dataman-Cloud/swan/agent/ipam" "github.com/Dataman-Cloud/swan/agent/janitor" @@ -202,12 +201,7 @@ func (agent *Agent) ServeApi(l net.Listener) error { return httpd.Serve(l) } -func (agent *Agent) serveProxy(ctx *gin.Context) { - var ( - r = ctx.Request - w = ctx.Writer - ) - +func (agent *Agent) serveProxy(w http.ResponseWriter, r *http.Request) { hijacker, ok := w.(http.Hijacker) if !ok { w.WriteHeader(500) diff --git a/agent/api.go b/agent/api.go index 2b40e2bc..388ca255 100644 --- a/agent/api.go +++ b/agent/api.go @@ -1,93 +1,95 @@ package agent import ( + "encoding/json" "net/http" - "github.com/gin-gonic/gin" + "github.com/gorilla/mux" ) func (agent *Agent) NewHTTPMux() http.Handler { - mux := gin.Default() - gin.SetMode(gin.ReleaseMode) + m := mux.NewRouter() - mux.GET("/sysinfo", agent.sysinfo) - mux.GET("/configs", agent.showConfigs) + m.Path("/sysinfo").Methods("GET").HandlerFunc(agent.sysinfo) + m.Path("/configs").Methods("GET").HandlerFunc(agent.showConfigs) // /proxy/** if agent.config.Janitor.Enabled { - agent.setupProxyHandlers(mux) + agent.setupProxyHandlers(m) } // /dns/** if agent.config.DNS.Enabled { - agent.setupDNSHandlers(mux) + agent.setupDNSHandlers(m) } // /ipam/** if agent.config.IPAM.Enabled { - agent.setupIPAMHandlers(mux) + agent.setupIPAMHandlers(m) } - mux.NoRoute(agent.serveProxy) - return mux + m.NotFoundHandler = http.HandlerFunc(agent.serveProxy) + + return m } -func (agent *Agent) setupProxyHandlers(mux *gin.Engine) { +func (agent *Agent) setupProxyHandlers(mux *mux.Router) { var ( janitor = agent.janitor ) - r := mux.Group("/proxy") - r.GET("", janitor.ListUpstreams) - r.GET("/upstreams", janitor.ListUpstreams) - r.GET("/upstreams/:uid", janitor.GetUpstream) - r.PUT("/upstreams", janitor.UpsertUpstream) - r.DELETE("/upstreams", janitor.DelUpstream) - r.GET("/sessions", janitor.ListSessions) - r.GET("/configs", janitor.ShowConfigs) - r.GET("/stats", janitor.ShowStats) - r.GET("/stats/:uid", janitor.ShowUpstreamStats) - r.GET("/stats/:uid/:bid", janitor.ShowBackendStats) + r := mux.PathPrefix("/proxy").Subrouter() + r.Path("").Methods("GET").HandlerFunc(janitor.ListUpstreams) + r.Path("/upstreams").Methods("GET").HandlerFunc(janitor.ListUpstreams) + r.Path("/upstreams/{uid}").Methods("GET").HandlerFunc(janitor.GetUpstream) + r.Path("/upstreams").Methods("PUT").HandlerFunc(janitor.UpsertUpstream) + r.Path("/upstreams").Methods("DELETE").HandlerFunc(janitor.DelUpstream) + r.Path("/sessions").Methods("GET").HandlerFunc(janitor.ListSessions) + r.Path("/configs").Methods("GET").HandlerFunc(janitor.ShowConfigs) + r.Path("/stats").Methods("GET").HandlerFunc(janitor.ShowStats) + r.Path("/stats/{uid}").Methods("GET").HandlerFunc(janitor.ShowUpstreamStats) + r.Path("/stats/{uid}/{bid}").Methods("GET").HandlerFunc(janitor.ShowBackendStats) } -func (agent *Agent) setupDNSHandlers(mux *gin.Engine) { +func (agent *Agent) setupDNSHandlers(mux *mux.Router) { var ( resolver = agent.resolver ) - r := mux.Group("/dns") - r.GET("", resolver.ListRecords) - r.GET("/records", resolver.ListRecords) - r.GET("/records/:id", resolver.GetRecord) - r.PUT("/records", resolver.UpsertRecord) - r.DELETE("/records", resolver.DelRecord) - r.GET("/configs", resolver.ShowConfigs) - r.GET("/stats", resolver.ShowStats) - r.GET("/stats/:id", resolver.ShowParentStats) - + r := mux.PathPrefix("/dns").Subrouter() + r.Path("").Methods("GET").HandlerFunc(resolver.ListRecords) + r.Path("/records").Methods("GET").HandlerFunc(resolver.ListRecords) + r.Path("/records/{id}").Methods("GET").HandlerFunc(resolver.GetRecord) + r.Path("/records").Methods("PUT").HandlerFunc(resolver.UpsertRecord) + r.Path("/records").Methods("DELETE").HandlerFunc(resolver.DelRecord) + r.Path("/configs").Methods("GET").HandlerFunc(resolver.ShowConfigs) + r.Path("/stats").Methods("GET").HandlerFunc(resolver.ShowStats) + r.Path("/stats/{id}").Methods("GET").HandlerFunc(resolver.ShowParentStats) } -func (agent *Agent) setupIPAMHandlers(mux *gin.Engine) { +func (agent *Agent) setupIPAMHandlers(mux *mux.Router) { var ( ipam = agent.ipam ) - r := mux.Group("/ipam") - r.GET("", ipam.ListSubNets) - r.GET("subnets", ipam.ListSubNets) - r.PUT("subnets", ipam.SetSubNetPool) + r := mux.PathPrefix("/ipam").Subrouter() + r.Path("").Methods("GET").HandlerFunc(ipam.ListSubNets) + r.Path("/subnets").Methods("GET").HandlerFunc(ipam.ListSubNets) + r.Path("/subnets").Methods("PUT").HandlerFunc(ipam.SetSubNetPool) } -func (agent *Agent) sysinfo(ctx *gin.Context) { +func (agent *Agent) sysinfo(w http.ResponseWriter, r *http.Request) { info, err := Gather() if err != nil { - http.Error(ctx.Writer, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } - ctx.JSON(200, info) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(info) } -func (agent *Agent) showConfigs(ctx *gin.Context) { - ctx.JSON(200, agent.config) +func (agent *Agent) showConfigs(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(agent.config) } diff --git a/agent/ipam/api.go b/agent/ipam/api.go index 3b289c32..60fa6d8c 100644 --- a/agent/ipam/api.go +++ b/agent/ipam/api.go @@ -1,15 +1,14 @@ package ipam import ( + "encoding/json" "net/http" - - "github.com/gin-gonic/gin" ) -func (m *IPAM) ListSubNets(c *gin.Context) { +func (m *IPAM) ListSubNets(w http.ResponseWriter, r *http.Request) { subnets, err := m.store.ListSubNets() if err != nil { - http.Error(c.Writer, err.Error(), 500) + http.Error(w, err.Error(), 500) return } @@ -31,23 +30,24 @@ func (m *IPAM) ListSubNets(c *gin.Context) { } } - c.JSON(200, ret) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(ret) } -func (m *IPAM) SetSubNetPool(c *gin.Context) { +func (m *IPAM) SetSubNetPool(w http.ResponseWriter, r *http.Request) { var pool *IPPoolRange - if err := c.BindJSON(&pool); err != nil { - http.Error(c.Writer, err.Error(), 400) + if err := json.NewDecoder(r.Body).Decode(&pool); err != nil { + http.Error(w, err.Error(), 400) return } if err := pool.Valid(); err != nil { - http.Error(c.Writer, err.Error(), 400) + http.Error(w, err.Error(), 400) return } if err := m.SetIPPool(pool); err != nil { - http.Error(c.Writer, err.Error(), 500) + http.Error(w, err.Error(), 500) return } } diff --git a/agent/janitor/api.go b/agent/janitor/api.go index 0474e38d..21c0e760 100644 --- a/agent/janitor/api.go +++ b/agent/janitor/api.go @@ -1,21 +1,23 @@ package janitor import ( + "encoding/json" "net/http" - "github.com/gin-gonic/gin" - "github.com/Dataman-Cloud/swan/agent/janitor/stats" "github.com/Dataman-Cloud/swan/agent/janitor/upstream" + + "github.com/gorilla/mux" ) -func (s *JanitorServer) ListUpstreams(c *gin.Context) { - c.JSON(200, upstream.AllUpstreams()) +func (s *JanitorServer) ListUpstreams(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(upstream.AllUpstreams()) } -func (s *JanitorServer) GetUpstream(c *gin.Context) { +func (s *JanitorServer) GetUpstream(w http.ResponseWriter, r *http.Request) { var ( - uid = c.Param("uid") + uid = mux.Vars(r)["uid"] m = upstream.AllUpstreams() ret = new(upstream.Upstream) ) @@ -27,74 +29,88 @@ func (s *JanitorServer) GetUpstream(c *gin.Context) { } } - c.JSON(200, ret) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(ret) } -func (s *JanitorServer) UpsertUpstream(c *gin.Context) { +func (s *JanitorServer) UpsertUpstream(w http.ResponseWriter, r *http.Request) { var cmb *upstream.BackendCombined - if err := c.BindJSON(&cmb); err != nil { - http.Error(c.Writer, err.Error(), 400) + if err := json.NewDecoder(r.Body).Decode(&cmb); err != nil { + http.Error(w, err.Error(), 400) return } if err := cmb.Valid(); err != nil { - http.Error(c.Writer, err.Error(), 400) + http.Error(w, err.Error(), 400) return } if err := s.UpsertBackend(cmb); err != nil { - http.Error(c.Writer, err.Error(), 500) + http.Error(w, err.Error(), 500) return } - c.Writer.WriteHeader(201) + w.WriteHeader(http.StatusCreated) } -func (s *JanitorServer) DelUpstream(c *gin.Context) { +func (s *JanitorServer) DelUpstream(w http.ResponseWriter, r *http.Request) { var cmb *upstream.BackendCombined - if err := c.BindJSON(&cmb); err != nil { - http.Error(c.Writer, err.Error(), 400) + if err := json.NewDecoder(r.Body).Decode(&cmb); err != nil { + http.Error(w, err.Error(), 400) return } s.removeBackend(cmb) - c.Writer.WriteHeader(204) + w.WriteHeader(http.StatusNoContent) } -func (s *JanitorServer) ListSessions(c *gin.Context) { - c.JSON(200, upstream.AllSessions()) +func (s *JanitorServer) ListSessions(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(upstream.AllSessions()) } -func (s *JanitorServer) ShowConfigs(c *gin.Context) { - c.JSON(200, s.config) +func (s *JanitorServer) ShowConfigs(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(s.config) } -func (s *JanitorServer) ShowStats(c *gin.Context) { +func (s *JanitorServer) ShowStats(w http.ResponseWriter, r *http.Request) { wrapper := map[string]interface{}{ "httpd": s.config.ListenAddr, "httpdTLS": s.config.TLSListenAddr, "counter": stats.Get(), "tcpd": s.tcpd, } - c.JSON(200, wrapper) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(wrapper) } -func (s *JanitorServer) ShowUpstreamStats(c *gin.Context) { - uid := c.Param("uid") +func (s *JanitorServer) ShowUpstreamStats(w http.ResponseWriter, r *http.Request) { + uid := mux.Vars(r)["uid"] if m, ok := stats.UpstreamStats()[uid]; ok { - c.JSON(200, m) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(m) return } - c.JSON(200, make(map[string]interface{})) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(make(map[string]interface{})) } -func (s *JanitorServer) ShowBackendStats(c *gin.Context) { - uid, bid := c.Param("uid"), c.Param("bid") +func (s *JanitorServer) ShowBackendStats(w http.ResponseWriter, r *http.Request) { + var ( + vars = mux.Vars(r) + uid = vars["uid"] + bid = vars["bid"] + ) + if ups, ok := stats.UpstreamStats()[uid]; ok { if backend, ok := ups[bid]; ok { - c.JSON(200, backend) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(backend) return } } - c.JSON(200, make(map[string]interface{})) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(make(map[string]interface{})) } diff --git a/agent/resolver/api.go b/agent/resolver/api.go index ce5672ad..fcfa0c34 100644 --- a/agent/resolver/api.go +++ b/agent/resolver/api.go @@ -1,70 +1,78 @@ package resolver import ( + "encoding/json" "net/http" - "github.com/gin-gonic/gin" + "github.com/gorilla/mux" ) -func (s *Resolver) ListRecords(c *gin.Context) { - c.JSON(200, s.allRecords()) +func (s *Resolver) ListRecords(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(s.allRecords()) } -func (s *Resolver) GetRecord(c *gin.Context) { +func (s *Resolver) GetRecord(w http.ResponseWriter, r *http.Request) { var ( - id = c.Param("id") - m = s.allRecords() - ret = make([]*Record, 0) + vars = mux.Vars(r) + id = vars["id"] + m = s.allRecords() + ret = make([]*Record, 0) ) if val, ok := m[id]; ok { ret = val } - c.JSON(200, ret) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(ret) } -func (s *Resolver) UpsertRecord(c *gin.Context) { +func (s *Resolver) UpsertRecord(w http.ResponseWriter, r *http.Request) { var record *Record - if err := c.BindJSON(&record); err != nil { - http.Error(c.Writer, err.Error(), 400) + if err := json.NewDecoder(r.Body).Decode(&record); err != nil { + http.Error(w, err.Error(), 400) return } if err := s.Upsert(record); err != nil { - http.Error(c.Writer, err.Error(), 500) + http.Error(w, err.Error(), 500) return } - c.Writer.WriteHeader(201) + w.WriteHeader(http.StatusCreated) } -func (s *Resolver) DelRecord(c *gin.Context) { +func (s *Resolver) DelRecord(w http.ResponseWriter, r *http.Request) { var record *Record - if err := c.BindJSON(&record); err != nil { - http.Error(c.Writer, err.Error(), 400) + if err := json.NewDecoder(r.Body).Decode(&record); err != nil { + http.Error(w, err.Error(), 400) return } if s.remove(record) { s.stats.Del(record.Parent) } - c.Writer.WriteHeader(204) + w.WriteHeader(http.StatusNoContent) } -func (s *Resolver) ShowConfigs(c *gin.Context) { - c.JSON(200, s.config) +func (s *Resolver) ShowConfigs(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(s.config) } -func (s *Resolver) ShowStats(c *gin.Context) { - c.JSON(200, s.stats.Get()) +func (s *Resolver) ShowStats(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(s.stats.Get()) } -func (s *Resolver) ShowParentStats(c *gin.Context) { - pid := c.Param("id") +func (s *Resolver) ShowParentStats(w http.ResponseWriter, r *http.Request) { + pid := mux.Vars(r)["id"] m := s.stats.Get() if m, ok := m.Parents[pid]; ok { - c.JSON(200, m) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(m) return } - c.JSON(200, make(map[string]interface{})) + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(make(map[string]interface{})) } diff --git a/agent/resolver/resolver.go b/agent/resolver/resolver.go index 4c528850..fc8f32e1 100644 --- a/agent/resolver/resolver.go +++ b/agent/resolver/resolver.go @@ -109,7 +109,8 @@ func (r *Resolver) handleLocal(w dns.ResponseWriter, req *dns.Msg) { MsgHdr: dns.MsgHdr{ Authoritative: true, RecursionAvailable: r.config.RecurseOn, - }} + }, + } msg.SetReply(req) var (