Added basic auth and improved error handling

This commit is contained in:
dagger committed 2024-02-12 11:33:30 -04:00
1 parent 782e25f0f1
commit 1d357b0385
3 files changed
+33 -6

No files matched your search

+24 -6
View File
@@ -1,19 +1,21 @@
package main
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"time"
"github.com/icholy/digest"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"gopkg.in/yaml.v2"
)
type config struct {
Horn string `yaml:"horn"`
Times []string `yaml:"times"`
Horn map[string]string `yaml:"horn"`
Times []string `yaml:"times"`
}
func (config *config) getConf() *config {
@@ -29,7 +31,7 @@ func (config *config) getConf() *config {
logger.Error("Unmarshal", logger.Args(":", err))
}
logger.Info("Config loaded", logger.Args("Times", config.Times, "API URL", config.Horn))
logger.Info("Config loaded", logger.Args("Times", config.Times, "API URL", config.Horn["url"]))
return config
}
@@ -42,9 +44,10 @@ func main() {
putils.LettersFromStringWithStyle("Bell ", pterm.FgRed.ToStyle()),
putils.LettersFromStringWithStyle("System", pterm.FgBlue.ToStyle())).
Srender()
pterm.DefaultCenter.Println(s)
pterm.DefaultCenter.Println("By Cody (" + pterm.LightBlue("https://oki.cx/tree") + ")")
pterm.DefaultCenter.Println("v1.0")
pterm.DefaultCenter.Println("v1.1")
pterm.Print("\n")
@@ -60,11 +63,26 @@ func main() {
for _, time := range config.Times {
if time == ctime {
resp, err := http.Get(config.Horn)
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{
Transport: &digest.Transport{
Username: config.Horn["username"],
Password: config.Horn["password"],
},
}
req, err := http.NewRequest("GET", config.Horn["url"], nil)
if err != nil {
logger.Error(fmt.Sprintf("%s", err))
}
logger.Info(fmt.Sprintf("Sending HTTP request to %s", config.Horn), logger.Args("Reason", "Config File", "Time", ctime, "Status Code", resp.Status))
resp, err := client.Do(req)
if err != nil {
logger.Error(fmt.Sprintf("%s", err))
}
if resp.StatusCode != 200 {
logger.Error(fmt.Sprintf("HTTP request failed: %s", resp.Status))
} else {
logger.Info(fmt.Sprintf("Sending HTTP request to %s", config.Horn["url"]), logger.Args("Reason", "Config File", "Time", ctime, "Status Code", resp.Status))
}
}
}