aboutsummaryrefslogtreecommitdiff
path: root/service.go
diff options
context:
space:
mode:
Diffstat (limited to 'service.go')
-rw-r--r--service.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/service.go b/service.go
index e8c1f8d..fd9fc59 100644
--- a/service.go
+++ b/service.go
@@ -17,16 +17,16 @@ type Service struct {
httpConn *http.Client
dialer *net.Dialer
ticker *time.Ticker
- opts ServiceOptions
+ cfg ServiceConfig
isReady bool
}
// NewService create and initialize the connection to service.
-func NewService(opts ServiceOptions) (svc *Service, err error) {
+func NewService(cfg ServiceConfig) (svc *Service, err error) {
svc = &Service{
- opts: opts,
+ cfg: cfg,
}
- err = svc.opts.init()
+ err = svc.cfg.init()
if err != nil {
return nil, fmt.Errorf(`NewService: %w`, err)
}
@@ -37,7 +37,7 @@ func NewService(opts ServiceOptions) (svc *Service, err error) {
func (svc *Service) Scan() (report ScanReport) {
var err error
- report.Name = svc.opts.Name
+ report.Name = svc.cfg.Name
report.At = internal.Now()
if !svc.isReady {
err = svc.connect()
@@ -47,11 +47,11 @@ func (svc *Service) Scan() (report ScanReport) {
}
}
- switch svc.opts.scanURL.Scheme {
+ switch svc.cfg.scanURL.Scheme {
case serviceKindHTTP, serviceKindHTTPS:
var req = &http.Request{
- Method: svc.opts.HTTPMethod,
- URL: svc.opts.scanURL,
+ Method: svc.cfg.HTTPMethod,
+ URL: svc.cfg.scanURL,
}
var httpResp *http.Response
httpResp, err = svc.httpConn.Do(req)
@@ -66,7 +66,7 @@ func (svc *Service) Scan() (report ScanReport) {
case serviceKindTCP, serviceKindUDP:
var conn net.Conn
- conn, err = svc.dialer.Dial(svc.opts.scanURL.Scheme, svc.opts.scanURL.Host)
+ conn, err = svc.dialer.Dial(svc.cfg.scanURL.Scheme, svc.cfg.scanURL.Host)
if err != nil {
report.Error = err.Error()
return report
@@ -82,14 +82,14 @@ func (svc *Service) Scan() (report ScanReport) {
// Start scanning periodically and send the report to reportq.
func (svc *Service) Start(reportq chan<- ScanReport) {
- svc.ticker = time.NewTicker(svc.opts.interval)
+ svc.ticker = time.NewTicker(svc.cfg.interval)
reportq <- svc.Scan()
for {
_, ok := <-svc.ticker.C
if !ok {
- log.Printf("Service: %s not ok", svc.opts.Name)
+ log.Printf("Service: %s not ok", svc.cfg.Name)
return
}
reportq <- svc.Scan()
@@ -102,15 +102,15 @@ func (svc *Service) Stop() {
}
func (svc *Service) connect() (err error) {
- switch svc.opts.scanURL.Scheme {
+ switch svc.cfg.scanURL.Scheme {
case serviceKindHTTP, serviceKindHTTPS:
svc.httpConn = &http.Client{
- Timeout: svc.opts.timeout,
+ Timeout: svc.cfg.timeout,
}
case serviceKindTCP, serviceKindUDP:
svc.dialer = &net.Dialer{
- Timeout: svc.opts.timeout,
+ Timeout: svc.cfg.timeout,
}
}
svc.isReady = true