Skip to content

Commit

Permalink
use gorilla mux replace gin as RUL Router
Browse files Browse the repository at this point in the history
  • Loading branch information
pwzgorilla committed Sep 25, 2017
1 parent 118e9d4 commit 8ebe264
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 117 deletions.
8 changes: 1 addition & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
88 changes: 45 additions & 43 deletions agent/api.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 10 additions & 10 deletions agent/ipam/api.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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
}
}
78 changes: 47 additions & 31 deletions agent/janitor/api.go
Original file line number Diff line number Diff line change
@@ -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)
)
Expand All @@ -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{}))
}
Loading

0 comments on commit 8ebe264

Please sign in to comment.