2013-07-07 23:48:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-07-08 00:07:19 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2013-07-07 23:48:39 +00:00
|
|
|
"log"
|
2013-07-08 00:07:19 +00:00
|
|
|
"net"
|
2013-07-07 23:48:39 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
server := newCrispyServer()
|
|
|
|
http.Handle("/", server)
|
2014-09-20 14:39:22 +00:00
|
|
|
log.Printf("Serving on :9799 with %+v\n", server)
|
|
|
|
log.Fatal(http.ListenAndServe(":9799", nil))
|
2013-07-07 23:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type crispyServer struct {
|
|
|
|
logstasher *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCrispyServer() *crispyServer {
|
|
|
|
return &crispyServer{
|
|
|
|
logstasher: newLogstashLogger(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *crispyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2014-09-20 14:39:22 +00:00
|
|
|
if r.URL.Path == "/DIE" && r.Method == "POST" {
|
|
|
|
me.logstasher.Fatal("OH GOSH")
|
|
|
|
}
|
|
|
|
|
2013-07-08 00:07:19 +00:00
|
|
|
j, err := json.Marshal(r)
|
|
|
|
if err == nil {
|
|
|
|
me.logstasher.Println(string(j))
|
|
|
|
}
|
2013-07-07 23:48:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLogstashLogger() *log.Logger {
|
2013-07-08 00:07:19 +00:00
|
|
|
return log.New(&logstashWriter{}, "", 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
type logstashWriter struct{}
|
|
|
|
|
|
|
|
func (me *logstashWriter) Write(b []byte) (int, error) {
|
|
|
|
conn, err := net.Dial("tcp", ":55784")
|
|
|
|
if err == nil {
|
|
|
|
defer conn.Close()
|
|
|
|
fmt.Fprintf(conn, string(b))
|
|
|
|
} else {
|
|
|
|
fmt.Printf(string(b))
|
|
|
|
}
|
|
|
|
return 0, nil
|
2013-07-07 23:48:39 +00:00
|
|
|
}
|