From a2aad272e5dbb5fdcd6e83bb830c6c9ef9997080 Mon Sep 17 00:00:00 2001 From: corey Date: Fri, 18 Feb 2022 16:53:26 +0000 Subject: [PATCH] added graylog capabilities --- config/log_server.go | 16 +++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ main.go | 42 ++++++++++++++++++++++++++------------- static/template/home.html | 2 +- 5 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 config/log_server.go create mode 100644 go.sum diff --git a/config/log_server.go b/config/log_server.go new file mode 100644 index 0000000..0bba439 --- /dev/null +++ b/config/log_server.go @@ -0,0 +1,16 @@ +package Config + +import ( + "log" + "regexp" +) + +func CheckLogServer(serverURL *string) bool { + var regex *regexp.Regexp + var err error + + if regex, err = regexp.Compile("(\b(https?)://)?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]| ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); err != nil { + log.Fatal(err) + } + return regex.MatchString(*serverURL) +} diff --git a/go.mod b/go.mod index 0ba1b4c..4a91ea5 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module EchoBot go 1.17 + +require gopkg.in/Graylog2/go-gelf.v2 v2.0.0-20191017102106-1550ee647df0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dcacbfa --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +gopkg.in/Graylog2/go-gelf.v2 v2.0.0-20191017102106-1550ee647df0 h1:Xg23ydYYJLmb9AK3XdcEpplHZd1MpN3X2ZeeMoBClmY= +gopkg.in/Graylog2/go-gelf.v2 v2.0.0-20191017102106-1550ee647df0/go.mod h1:CeDeqW4tj9FrgZXF/dQCWZrBdcZWWBenhJtxLH4On2g= diff --git a/main.go b/main.go index 16de330..1da021a 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,14 @@ package main import ( handlers "EchoBot/handlers" service "EchoBot/service" + "flag" "fmt" + "io" "log" "net/http" "os" - "strconv" + + "gopkg.in/Graylog2/go-gelf.v2/gelf" ) func main() { @@ -23,21 +26,32 @@ func startServices() { service.RunTimedTask() } +func grayLogConfig(graylogAddr *string) { + if len(*graylogAddr) > 0 { + gelfWriter, err := gelf.NewTCPWriter(*graylogAddr) + if err != nil { + log.Fatalf("gelf.NewWriter: %s", err) + } + // log to both stderr and graylog2 + log.SetOutput(io.MultiWriter(os.Stderr, gelfWriter)) + log.Printf("logging to stderr & graylog2@'%s'\n", *graylogAddr) + } +} + func startServer() { loadHandlers() - startServices() - var port string - if len(os.Args) > 1 { - if _, err := strconv.Atoi(os.Args[1]); err != nil { - log.Println(fmt.Sprintf("Port cannot be [%s], assigning default port 8080", os.Args[1])) - } else { - port = os.Args[1] - } - } else { - port = "8080" - } + var graylogServer string + flag.StringVar(&graylogServer, "graylog", "", "The graylog server and port to utilize [Example: -graylog=http:www.graylog.com:8080") + + var port int + flag.IntVar(&port, "port", 8080, "Port number for EchoBot to run on.") + + flag.Parse() + + grayLogConfig(&graylogServer) + startServices() - log.Printf("EchoBot server started on port %s!\n", port) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) + log.Printf("EchoBot server started on port %d!\n", port) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } diff --git a/static/template/home.html b/static/template/home.html index bdae8ed..4f281d5 100644 --- a/static/template/home.html +++ b/static/template/home.html @@ -7,7 +7,7 @@
Create An Echo Request
-

Create an http request to replay +

Create an http request to replay

Let's Go