Skip to content

Commit

Permalink
bim!
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibault Richard committed Dec 17, 2016
0 parents commit ae5bf5e
Show file tree
Hide file tree
Showing 315 changed files with 144,870 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toctoc
5 changes: 5 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.4

COPY toctoc /toctoc

ENTRYPOINT ["/toctoc"]
103 changes: 103 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"errors"
"flag"
"sync"
"time"

log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/thbkrkr/go-utilz/http"
)

var (
name = "toctoc"
buildDate = "dev"
gitCommit = "dev"

port int
tick int
timeout int
mutex sync.RWMutex
events = map[string]Event{}
)

func init() {
flag.IntVar(&port, "port", 4242, "Port")
flag.IntVar(&tick, "tick", 10, "Tick seconds")
flag.IntVar(&timeout, "timeout", 10, "Timeout in seconds")

flag.Parse()
}

func main() {
go watch()

http.API(name, buildDate, gitCommit, port, router)
}

func router(r *gin.Engine) {
r.POST("/event", sendEvent)
r.GET("/health", getStatus)
}

func watch() {
tick := time.NewTicker(time.Second * time.Duration(tick))
for range tick.C {
for _, event := range events {
if time.Since(event.Timestamp) > time.Second*time.Duration(timeout) {
log.WithField("ID", event.ID).Errorf("No event since %d seconds", timeout)
}
}
}
}

type Event struct {
ID string
Timestamp time.Time
Value interface{}
}

func sendEvent(c *gin.Context) {

var obj map[string]interface{}
err := c.BindJSON(&obj)
if err != nil {
c.JSON(400, gin.H{"message": "Fail to bind JSON"})
return
}

ID, err := extractID(obj)
if err != nil {
c.JSON(400, gin.H{"message": "Fail to extract ID"})
return
}

ID = c.Request.RemoteAddr + "/" + ID

mutex.Lock()
defer mutex.Unlock()

events[ID] = Event{
ID: ID,
Timestamp: time.Now(),
Value: obj,
}
}

func extractID(obj map[string]interface{}) (string, error) {
host := obj["Host"]
if host != nil {
return host.(string), nil
}

return "", errors.New("No field found to extract ID")
}

func getStatus(c *gin.Context) {
mutex.RLock()
defer mutex.RUnlock()

c.JSON(200, events)
}
66 changes: 66 additions & 0 deletions vendor/github.com/Sirupsen/logrus/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/Sirupsen/logrus/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ae5bf5e

Please sign in to comment.