-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-response-checker.go
160 lines (125 loc) · 2.78 KB
/
http-response-checker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// Author string : Author's name
const Author = "J. de Freitas"
// Version int : App's Version
const Version = 1.02
// Delay int : One second delay
const Delay = 1
// HTTPOK int : Http response when the resquest resturns properly
const HTTPOK = 200
func main() {
introduction()
for {
showIntroMenu()
switch getUserInput() {
case 1:
checkStatus()
case 2:
retreiveLogs()
case 3:
fmt.Println("Exiting...")
os.Exit(0)
default:
fmt.Println("Unknown command")
os.Exit(-1)
}
}
}
func introduction() {
fmt.Println("-----------------------")
fmt.Println("Http Response Checker")
fmt.Println("Author: ", Author)
fmt.Println("Version: ", Version)
fmt.Println("-----------------------")
}
func getUserInput() int {
var userInput int
fmt.Scan(&userInput)
fmt.Println("Your input is", userInput)
return userInput
}
func showIntroMenu() {
fmt.Println("1 - Check status")
fmt.Println("2 - Show logs")
fmt.Println("3 - Exit")
}
func retreiveLogs() {
fmt.Println("Retrieving logs...")
file, err := ioutil.ReadFile("logs.txt")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(file))
}
}
func checkStatus() {
fmt.Println("Checking status...")
sites := showSites()
for _, site := range sites {
testSite(site)
time.Sleep(Delay * time.Second)
}
}
func showSites() []string {
var sites []string
filename := "sites.txt"
_, err := os.Stat(filename)
if os.IsNotExist(err) {
fmt.Println(filename, "does not exist.")
} else {
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error to open the file: ", err)
}
reader := bufio.NewReader(file)
for {
row, fileErr := reader.ReadString('\n')
row = strings.TrimSpace(row)
sites = append(sites, row)
if fileErr == io.EOF {
break
}
}
file.Close()
}
return sites
}
func testSite(site string) {
httpResp, err := http.Get(site)
fmt.Println("Testing ", site)
if err != nil {
fmt.Println(err)
} else {
if &httpResp.StatusCode != nil {
if httpResp.StatusCode == HTTPOK {
fmt.Println("Status: ", httpResp.StatusCode, " - Successfully loaded")
} else {
fmt.Println("Status: ", httpResp.StatusCode, " We are unable to load this website")
}
registerLog(site, httpResp.StatusCode, httpResp.Status)
} else {
fmt.Println("Something went wrong")
}
}
}
func registerLog(site string, statusCode int, status string) {
file, err := os.OpenFile("logs.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println(err)
} else {
file.WriteString(time.Now().Format("02/01/2006 15:04:05") + " - ")
file.WriteString(site + " - status - " + strconv.FormatInt(int64(statusCode), 10) + " - " + status + "\n")
}
file.Close()
}