forked from thbkrkr/toctoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thibault Richard
committed
Dec 17, 2016
0 parents
commit ae5bf5e
Showing
315 changed files
with
144,870 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
toctoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM alpine:3.4 | ||
|
||
COPY toctoc /toctoc | ||
|
||
ENTRYPOINT ["/toctoc"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.