package probe import ( "context" "errors" "fmt" "net/http" "git.loafle.net/commons_go/logging" "git.loafle.net/overflow/overflow_probes/central/api/module" "git.loafle.net/overflow/overflow_probes/central/client" "git.loafle.net/overflow/overflow_probes/commons" "git.loafle.net/overflow/overflow_probes/config" opuu "git.loafle.net/overflow/overflow_probes/util/url" ) const ( probeEntryPoint = "/probe" metricsEntryPoint = "/metrics" fileEntryPoint = "/file" ) type Probe interface { commons.Handler } type probe struct { probeEntryURL string metricsEntryURL string probeClient client.Client metricsClient client.Client shutdownChan chan bool } func New() (Probe, error) { p := &probe{ shutdownChan: make(chan bool), } var err error if p.probeEntryURL, err = opuu.Join(*config.CFG.Central.URL, probeEntryPoint); nil != err { return nil, err } if p.metricsEntryURL, err = opuu.Join(*config.CFG.Central.URL, metricsEntryPoint); nil != err { return nil, err } return p, nil } func (p *probe) Serve() error { var err error if err = p.connectToCentral(); nil != err { return err } // ListenLoop: for { select { case <-p.shutdownChan: return errors.New("Shutting down") } } return nil } func (p *probe) Shutdown(ctx context.Context) error { p.shutdownChan <- true return nil } func (p *probe) connectToCentral() error { header := http.Header{} header[module.ProbeHeader_ProbeKey] = []string{*config.CFG.Probe.Key} var res *http.Response var err error p.probeClient = client.New() if res, err = p.probeClient.Dial(p.probeEntryURL, header, 4096, 4096); nil != err { return err } logging.Logger.Debug(fmt.Sprintf("Probe: Connect Probe HTTP Status[%s]", res.Status)) p.probeClient.OnNotify(p.onNotify) p.metricsClient = client.New() if res, err = p.metricsClient.Dial(p.metricsEntryURL, header, 4096, 4096); nil != err { return err } logging.Logger.Debug(fmt.Sprintf("Probe: Connect Metrics HTTP Status[%s]", res.Status)) return nil }