mirror of
https://github.com/spaytac/orbiter.git
synced 2026-01-21 23:34:41 +00:00
commit
432a6c9355
22
api/events.go
Normal file
22
api/events.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Events(eventChannel chan *logrus.Entry) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
logrus.Info("New events")
|
||||||
|
formatter := &logrus.JSONFormatter{}
|
||||||
|
flusher, _ := w.(http.Flusher)
|
||||||
|
for {
|
||||||
|
e := <-eventChannel
|
||||||
|
b, _ := formatter.Format(e)
|
||||||
|
fmt.Fprintf(w, string(b))
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,13 +1,15 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/gianarb/orbiter/core"
|
"github.com/gianarb/orbiter/core"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetRouter(core core.Core) *mux.Router {
|
func GetRouter(core core.Core, eventChannel chan *logrus.Entry) *mux.Router {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/handle/{autoscaler_name}/{service_name}", Handle(core.Autoscalers)).Methods("POST")
|
r.HandleFunc("/handle/{autoscaler_name}/{service_name}", Handle(core.Autoscalers)).Methods("POST")
|
||||||
r.HandleFunc("/health", Health()).Methods("GET")
|
r.HandleFunc("/health", Health()).Methods("GET")
|
||||||
|
r.HandleFunc("/events", Events(eventChannel)).Methods("GET")
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DaemonCmd struct {
|
type DaemonCmd struct {
|
||||||
|
EventChannel chan *logrus.Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *DaemonCmd) Run(args []string) int {
|
func (c *DaemonCmd) Run(args []string) int {
|
||||||
@ -51,7 +52,7 @@ func (c *DaemonCmd) Run(args []string) int {
|
|||||||
logrus.Info("Stopping and cleaning. Bye!")
|
logrus.Info("Stopping and cleaning. Bye!")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
router := api.GetRouter(core)
|
router := api.GetRouter(core, c.EventChannel)
|
||||||
logrus.Infof("API Server run on port %s", port)
|
logrus.Infof("API Server run on port %s", port)
|
||||||
http.ListenAndServe(port, router)
|
http.ListenAndServe(port, router)
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
7
main.go
7
main.go
@ -3,17 +3,22 @@ package main
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/gianarb/orbiter/cmd"
|
"github.com/gianarb/orbiter/cmd"
|
||||||
|
"github.com/gianarb/orbiter/utils/hook"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
eventChannel := make(chan *logrus.Entry)
|
||||||
|
logrus.AddHook(hook.NewChannelHook(eventChannel))
|
||||||
|
|
||||||
c := cli.NewCLI("orbiter", "0.0.0")
|
c := cli.NewCLI("orbiter", "0.0.0")
|
||||||
c.Args = os.Args[1:]
|
c.Args = os.Args[1:]
|
||||||
|
|
||||||
c.Commands = map[string]cli.CommandFactory{
|
c.Commands = map[string]cli.CommandFactory{
|
||||||
"daemon": func() (cli.Command, error) {
|
"daemon": func() (cli.Command, error) {
|
||||||
return &cmd.DaemonCmd{}, nil
|
return &cmd.DaemonCmd{eventChannel}, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
26
utils/hook/channel.go
Normal file
26
utils/hook/channel.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package hook
|
||||||
|
|
||||||
|
import "github.com/Sirupsen/logrus"
|
||||||
|
|
||||||
|
type Channel struct {
|
||||||
|
c chan *logrus.Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChannelHook(cc chan *logrus.Entry) Channel {
|
||||||
|
return Channel{
|
||||||
|
c: cc,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channel Channel) Levels() []logrus.Level {
|
||||||
|
return logrus.AllLevels
|
||||||
|
}
|
||||||
|
|
||||||
|
func (channel Channel) Fire(entry *logrus.Entry) error {
|
||||||
|
select {
|
||||||
|
case channel.c <- entry:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user