Skip to content

Commit

Permalink
add /health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Spyros Psarras committed Aug 16, 2023
1 parent ad86757 commit 39e2d3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Usage of ./build/cspr-collector:
enable http output
-output-http-host string
http host to send the csp violations to (default "http://localhost:80/")
-output-http-headers string
additional headers for HTTP output
-output-http-headers string
header header key with value to send with the http request. Example "Authorization: ApiKey <APIKEY>"
-output-stdout
Expand Down Expand Up @@ -102,6 +100,12 @@ curl -X POST \
go fmt ./...
```

## Health check endpoint
endpoint with health response "OK"
```bash
curl http://localhost:8080/health
```

## License

The MIT License (MIT). Please see the [license file](LICENSE.md) for more information.
11 changes: 10 additions & 1 deletion collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Collector struct {
}

func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/health" {
c.handleHealth(w)
return
}

if r.RequestURI != "/" {
message := fmt.Sprintf("Path \"%s\" not found.", r.RequestURI)
c.response(w, http.StatusNotFound, message)
Expand Down Expand Up @@ -58,7 +63,6 @@ func (c *Collector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.WorkQueue <- data

c.response(w, http.StatusCreated, "")
return
}

func (c *Collector) response(w http.ResponseWriter, status int, message string) {
Expand All @@ -82,3 +86,8 @@ func (c *Collector) response(w http.ResponseWriter, status int, message string)
log.Fatal(err)
}
}

func (c *Collector) handleHealth(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
25 changes: 15 additions & 10 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ func (d *Dispatcher) Run() {
}

func (d *Dispatcher) start() {
for {
select {
case work := <-d.WorkQueue:
log.Print("Received work request.")
go func() {
worker := <-d.WorkerQueue
log.Print("Dispatching work request.")
worker <- work
}()
}
for work := range d.WorkQueue {
log.Print("Received work request.")
go func(w interface{}) {
worker := <-d.WorkerQueue
log.Print("Dispatching work request.")

// Perform a type assertion
typedWork, ok := w.(CSPRequest)
if !ok {
log.Print("Invalid work request type")
return
}

worker <- typedWork
}(work)
}
}

0 comments on commit 39e2d3a

Please sign in to comment.