LamerAlert/lameralert.go

62 lines
1.6 KiB
Go

package lameralert
import (
"net/http"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"code.chrissexton.org/cws/lameralert/config"
"code.chrissexton.org/cws/lameralert/event"
"code.chrissexton.org/cws/lameralert/sinks"
"code.chrissexton.org/cws/lameralert/sources"
)
type Connection struct {
source sources.Source
sink sinks.Sink
options map[string]interface{}
}
func NewConnection(from sources.Source, to sinks.Sink) Connection {
for _, topic := range from.GetTopics() {
to.Subscribe(from.GetSender(), topic)
}
return Connection{
source: from,
sink: to,
}
}
var mapping = map[chan event.Event][]chan event.Event{}
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
log.Debug().Msgf("%s - %s", r.Method, r.RequestURI)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func Setup() chan bool {
r := mux.NewRouter()
r.Use(logger)
c := config.New(r.PathPrefix("/config").Subrouter())
ch := make(chan bool)
h := sources.NewGenericRest(r.PathPrefix("/rest").Subrouter())
NewConnection(h, sinks.NewConsoleID(c, "00"))
NewConnection(h, sinks.NewConsoleID(c, "01"))
rss := sources.NewRSS(c, r.PathPrefix("/rss/webshit").Subrouter(), "webshit", "http://n-gate.com/hackernews/index.rss")
NewConnection(rss, sinks.NewConsoleID(c, "webshit-sink"))
if push, err := sinks.NewPushover(c, "webshit-push"); err != nil {
log.Fatal().Msgf("error: %s", err)
} else {
NewConnection(rss, push)
}
http.ListenAndServe(":9090", r)
return ch
}