Skip to content

Commit

Permalink
added graylog capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenios91 committed Feb 18, 2022
1 parent 6e43808 commit a2aad27
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
16 changes: 16 additions & 0 deletions config/log_server.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module EchoBot

go 1.17

require gopkg.in/Graylog2/go-gelf.v2 v2.0.0-20191017102106-1550ee647df0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
42 changes: 28 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion static/template/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="card">
<h5 class="card-header">Create An Echo Request</h5>
<div class="card-body">
<p class="card-text">Create an http request to replay
<p class="card-text">Create an http request to replay</br></br>
</p>
<a href="/createEchoRequest" class="btn btn-primary">Let's Go</a>
</div>
Expand Down

0 comments on commit a2aad27

Please sign in to comment.