From 250ca37781d0aed37e302d1d444b4c10c0896a0e Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 21 Jul 2021 00:15:57 +0200 Subject: [PATCH] refactory to extra api --- api/client.go | 23 ++++++++++++++++ request.go => api/request.go | 5 ++-- response_lists.go => api/response_lists.go | 8 +++--- response_stats.go => api/response_stats.go | 20 +++----------- fetch.go | 32 +++++++++++++++------- main.go | 10 +++---- 6 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 api/client.go rename request.go => api/request.go (80%) rename response_lists.go => api/response_lists.go (76%) rename response_stats.go => api/response_stats.go (76%) diff --git a/api/client.go b/api/client.go new file mode 100644 index 0000000..c9d6995 --- /dev/null +++ b/api/client.go @@ -0,0 +1,23 @@ +package api + +import "encoding/base64" + +// A Client for the API +type Client struct { + Token string `toml:"token"` + Host string `toml:"host"` +} + +// New Client from host and token +func New(host, token string) *Client { + c := &Client{ + Host: host, + } + c.SetToken(token) + return c +} + +// SetToken by using base64encoding +func (c *Client) SetToken(token string) { + c.Token = base64.StdEncoding.EncodeToString([]byte(token)) +} diff --git a/request.go b/api/request.go similarity index 80% rename from request.go rename to api/request.go index 63af99c..bcbd839 100644 --- a/request.go +++ b/api/request.go @@ -1,4 +1,4 @@ -package main +package api import ( "encoding/json" @@ -7,7 +7,8 @@ import ( "time" ) -func (c *configData) Request(url string, value interface{}) error { +// Request to API and unmarshal result +func (c *Client) Request(url string, value interface{}) error { netClient := &http.Client{ Timeout: time.Second * 20, } diff --git a/response_lists.go b/api/response_lists.go similarity index 76% rename from response_lists.go rename to api/response_lists.go index 2695c75..1cce419 100644 --- a/response_lists.go +++ b/api/response_lists.go @@ -1,4 +1,4 @@ -package main +package api import ( "fmt" @@ -16,7 +16,7 @@ type ResponseList struct { Data []string `json:"response,omitempty"` } -func (c *configData) RequestListVHosts() (*ResponseList, error) { +func (c *Client) RequestListVHosts() (*ResponseList, error) { req := ResponseList{} url := fmt.Sprintf(URLRequestListVHost) if err := c.Request(url, &req); err != nil { @@ -25,7 +25,7 @@ func (c *configData) RequestListVHosts() (*ResponseList, error) { return &req, nil } -func (c *configData) RequestListApps(vhost string) (*ResponseList, error) { +func (c *Client) RequestListApps(vhost string) (*ResponseList, error) { req := ResponseList{} url := fmt.Sprintf(URLRequestListApp, vhost) if err := c.Request(url, &req); err != nil { @@ -34,7 +34,7 @@ func (c *configData) RequestListApps(vhost string) (*ResponseList, error) { return &req, nil } -func (c *configData) RequestListStreams(vhost, app string) (*ResponseList, error) { +func (c *Client) RequestListStreams(vhost, app string) (*ResponseList, error) { req := ResponseList{} url := fmt.Sprintf(URLRequestListStream, vhost, app) if err := c.Request(url, &req); err != nil { diff --git a/response_stats.go b/api/response_stats.go similarity index 76% rename from response_stats.go rename to api/response_stats.go index 552edd7..f0cdb00 100644 --- a/response_stats.go +++ b/api/response_stats.go @@ -1,9 +1,7 @@ -package main +package api import ( "fmt" - - "github.com/bdlm/log" ) const ( @@ -33,17 +31,7 @@ type ResponseStatsData struct { TotalBytesOut uint64 `json:"totalBytesOut" example:"117022184"` } -func (resp *ResponseStats) Log(log *log.Entry) { - logger := log - if resp.Data != nil { - logger.WithFields(map[string]interface{}{ - "max_clients": resp.Data.MaxTotalConnections, - "clients": resp.Data.TotalConnections, - }) - } - logger.Info(resp.Message) -} -func (c *configData) RequestStatsVHost(vhost string) (*ResponseStats, error) { +func (c *Client) RequestStatsVHost(vhost string) (*ResponseStats, error) { req := ResponseStats{} url := fmt.Sprintf(URLRequestStatsVHost, vhost) if err := c.Request(url, &req); err != nil { @@ -52,7 +40,7 @@ func (c *configData) RequestStatsVHost(vhost string) (*ResponseStats, error) { return &req, nil } -func (c *configData) RequestStatsApp(vhost, app string) (*ResponseStats, error) { +func (c *Client) RequestStatsApp(vhost, app string) (*ResponseStats, error) { req := ResponseStats{} url := fmt.Sprintf(URLRequestStatsApp, vhost, app) if err := c.Request(url, &req); err != nil { @@ -61,7 +49,7 @@ func (c *configData) RequestStatsApp(vhost, app string) (*ResponseStats, error) return &req, nil } -func (c *configData) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) { +func (c *Client) RequestStatsStream(vhost, app, stream string) (*ResponseStats, error) { req := ResponseStats{} url := fmt.Sprintf(URLRequestStatsStream, vhost, app, stream) if err := c.Request(url, &req); err != nil { diff --git a/fetch.go b/fetch.go index 99ee195..0ba699c 100644 --- a/fetch.go +++ b/fetch.go @@ -1,48 +1,60 @@ package main import ( + "dev.sum7.eu/genofire/oven-exporter/api" "github.com/bdlm/log" ) -func (config *configData) fetch() { - respList, err := config.RequestListVHosts() +func statsLog(resp *api.ResponseStats, log *log.Entry) { + logger := log + if resp.Data != nil { + logger = logger.WithFields(map[string]interface{}{ + "max_clients": resp.Data.MaxTotalConnections, + "clients": resp.Data.TotalConnections, + }) + } + logger.Info(resp.Message) +} + +func fetch(client *api.Client) { + respList, err := client.RequestListVHosts() if err != nil { log.Panicf("unable to fetch vhosts: %s", err) } for _, vhost := range respList.Data { logVhost := log.WithField("vhost", vhost) - resp, err := config.RequestStatsVHost(vhost) + resp, err := client.RequestStatsVHost(vhost) if err != nil { logVhost.Errorf("error on request: %s", err) } else { - resp.Log(logVhost) + statsLog(resp, logVhost) } - respList, err = config.RequestListApps(vhost) + respList, err = client.RequestListApps(vhost) if err != nil { logVhost.Errorf("unable to fetch apps: %s", err) continue } for _, app := range respList.Data { logApp := logVhost.WithField("app", app) - resp, err = config.RequestStatsApp(vhost, app) + resp, err = client.RequestStatsApp(vhost, app) if err != nil { logApp.Errorf("error on request: %s", err) } else { - resp.Log(logApp) + statsLog(resp, logApp) } - respList, err = config.RequestListStreams(vhost, app) + respList, err = client.RequestListStreams(vhost, app) if err != nil { logApp.Errorf("unable to fetch stream: %s", err) continue } for _, stream := range respList.Data { logStream := logApp.WithField("stream", stream) - req, err := config.RequestStatsStream(vhost, app, stream) + req, err := client.RequestStatsStream(vhost, app, stream) if err != nil { logStream.Errorf("error on request: %s", err) continue } - req.Log(logStream) + statsLog(req, logStream) } } } diff --git a/main.go b/main.go index a85c741..c593ddc 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,16 @@ package main import ( - "encoding/base64" "flag" "dev.sum7.eu/genofire/golang-lib/file" "github.com/bdlm/log" + + "dev.sum7.eu/genofire/oven-exporter/api" ) type configData struct { - Token string `toml:"token"` - Host string `toml:"host"` + API *api.Client `toml:"api"` } func main() { @@ -24,6 +24,6 @@ func main() { if err := file.ReadTOML(configPath, config); err != nil { log.Panicf("open config file: %s", err) } - config.Token = base64.StdEncoding.EncodeToString([]byte(config.Token)) - config.fetch() + config.API.SetToken(config.API.Token) + fetch(config.API) }