Skip to content

Commit

Permalink
refactor: combine multiple packages into one main package
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Zhao committed Dec 21, 2019
1 parent fc2b3c2 commit 8aa1ba8
Show file tree
Hide file tree
Showing 23 changed files with 421 additions and 406 deletions.
9 changes: 9 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "log"

func debug(format string, v ...interface{}) {
if debugFlag {
log.Printf(format, v...)
}
}
7 changes: 7 additions & 0 deletions debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func TestDebug(t *testing.T) {
debug("%s %d", "test", 123)
}
83 changes: 83 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"flag"
"log"
"strconv"
"time"
)

var (
debugFlag bool
urlFlag string
cycleFlag time.Duration
timeoutFlag time.Duration
headerTimeoutFlag time.Duration

mailFromNameFlag string
mailFromAddrFlag string
mailFromPwdFlag string
mailToAddrFlag string
mailAuthHostFlag string
mailServerAddrFlag string
)

func init() {
var cycle, timeout, headerTimeout int

flag.BoolVar(&debugFlag, "debug", false, "turn on debug mode")

flag.StringVar(&urlFlag, "url", "", "HTTP URL to monitor")
flag.IntVar(&cycle, "cycle", 20000, "detection cycle in milliseconds")
flag.IntVar(&timeout, "timeout", 5000, "timeout for waiting for HTTP response, in milliseconds")
flag.IntVar(&headerTimeout, "header-timeout", 2000, "timeout for waiting for HTTP response headers after establishing a connection, in milliseconds")

flag.StringVar(&mailFromNameFlag, "mail-fname", "Site Monitor", "mail sender's name")
flag.StringVar(&mailFromAddrFlag, "mail-faddr", "", "mail sender's address")
flag.StringVar(&mailFromPwdFlag, "mail-fpwd", "", "mail sender's SMTP password")
flag.StringVar(&mailToAddrFlag, "mail-taddr", "", "mail recipient address")
flag.StringVar(&mailAuthHostFlag, "mail-auth", "", "SMTP authentication host address")
flag.StringVar(&mailServerAddrFlag, "mail-server", "", "SMTP server address")

//testing.Init() // uncomment when executing unit tests
flag.Parse()

var miss string

if urlFlag == "" {
miss += "-url, "
}
if mailFromNameFlag == "" {
miss += "-mail-fname, "
}
if mailFromAddrFlag == "" {
miss += "-mail-faddr, "
}
if mailFromPwdFlag == "" {
miss += "-mail-fpwd, "
}
if mailToAddrFlag == "" {
miss += "-mail-taddr, "
}
if mailAuthHostFlag == "" {
miss += "-mail-auth, "
}
if mailServerAddrFlag == "" {
miss += "-mail-server, "
}
if miss != "" {
log.Fatalf("missing the necessary flag(s): %srun \"site-monitor -h\" for more information about each flag.", miss)
}

convertDuration(cycle, &cycleFlag)
convertDuration(timeout, &timeoutFlag)
convertDuration(headerTimeout, &headerTimeoutFlag)
}

func convertDuration(n int, du *time.Duration) {
d, err := time.ParseDuration(strconv.Itoa(n) + "ms")
if err != nil {
log.Fatal("the input time format is not available")
}
*du = d
}
13 changes: 13 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"testing"
"time"
)

func TestConvertDuration(t *testing.T) {
var du time.Duration
if convertDuration(10, &du); du.String() != "10ms" {
t.Fail()
}
}
19 changes: 19 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"net/http"
"time"
)

func newHTTPClient(timeout, responseHeaderTimeout time.Duration) http.Client {
tr := http.Transport{
DisableKeepAlives: true,
ResponseHeaderTimeout: responseHeaderTimeout,
}
client := http.Client{
Transport: &tr,
Timeout: timeout,
}

return client
}
19 changes: 0 additions & 19 deletions http/client.go

This file was deleted.

14 changes: 0 additions & 14 deletions http/client_test.go

This file was deleted.

28 changes: 28 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"net/http"
"testing"
"time"
)

func TestNewHTTPClient(t *testing.T) {
c := newHTTPClient(time.Second*5, time.Second*2)
if c.Timeout.String() != "5s" {
t.Fail()
return
}
if tr := c.Transport.(*http.Transport); tr.ResponseHeaderTimeout.String() != "2s" {
t.Fail()
return
}
resp, err := c.Get("https://jsonplaceholder.typicode.com/users/1")
if err != nil {
t.Log(err)
t.Fail()
return
}
defer resp.Body.Close()
fmt.Println(resp.StatusCode)
}
66 changes: 66 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"sync"
"time"
)

var (
loggerInstance logger
)

type logRecord struct {
time time.Time
isNormal bool
statusCode int
isTimeout bool
err error
}

type logger struct {
sync.Mutex
records []logRecord
}

func (l *logger) string() string {
var msg string
for _, record := range l.records {
msg += fmt.Sprintf("* %s\tnormal: %v\tstatus code: %d\ttimeout: %v\terror: %v\n",
record.time.Format("2006-01-02 15:04:05"), record.isNormal, record.statusCode, record.isTimeout, record.err)
}
return msg
}

func logPush(record logRecord) {
loggerInstance.Lock()
defer loggerInstance.Unlock()

if len(loggerInstance.records) >= 10 {
loggerInstance.records = loggerInstance.records[1:]
}
loggerInstance.records = append(loggerInstance.records, record)

logCheck()
}

func logCheck() {
var unNormal int
for _, r := range loggerInstance.records {
if !r.isNormal {
unNormal++
}
}
if unNormal >= 5 {
debug("* Site status: unhealthy")
go sendMail(loggerInstance.string())
} else if float64(unNormal)/float64(len(loggerInstance.records)) > 0.5 {
debug("* Site status: unhealthy")
} else {
debug("* Site status: healthy")
}
}

func init() {
loggerInstance.records = make([]logRecord, 0, 10)
}
68 changes: 0 additions & 68 deletions logger/logger.go

This file was deleted.

56 changes: 0 additions & 56 deletions logger/logger_test.go

This file was deleted.

Loading

0 comments on commit 8aa1ba8

Please sign in to comment.