-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
6 changed files
with
148 additions
and
3 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
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
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/scalescape/dolores/monitor" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type baseCommand struct { | ||
*cli.Command | ||
log zerolog.Logger | ||
} | ||
|
||
type Monitor struct { | ||
baseCommand | ||
} | ||
|
||
func (m Monitor) Daemon(cctx *cli.Context) error { | ||
m.log.Info().Msgf("I'm here") | ||
cfg := monitor.Config{Port: cctx.Int("port"), Host: cctx.String("host")} | ||
pr := monitor.NewProxy(cfg) | ||
pr.Start() | ||
return nil | ||
} | ||
|
||
func monitorCommand(action cli.ActionFunc) *cli.Command { | ||
return &cli.Command{ | ||
Name: "monitor", | ||
Usage: "daemon to monitor HTTP API", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "host", | ||
Value: "localhost", | ||
}, | ||
&cli.IntFlag{ | ||
Name: "port", | ||
Aliases: []string{"p"}, | ||
Value: 9980, | ||
}, | ||
&cli.IntFlag{ | ||
Name: "watch", | ||
Aliases: []string{"w"}, | ||
Required: true, | ||
}, | ||
}, | ||
Action: action, | ||
} | ||
} | ||
|
||
func NewMonitor() *cli.Command { | ||
mon := Monitor{ | ||
baseCommand: baseCommand{ | ||
log: log.With().Str("cmd", "config").Logger(), | ||
}, | ||
} | ||
return monitorCommand(mon.Daemon) | ||
} |
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
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
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,74 @@ | ||
package monitor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type Proxy struct { | ||
cfg Config | ||
} | ||
|
||
type Config struct { | ||
Port int | ||
Host string | ||
} | ||
|
||
func (s Config) Address() string { | ||
return fmt.Sprintf("%s:%d", s.Host, s.Port) | ||
} | ||
|
||
func router() *mux.Router { | ||
m := mux.NewRouter() | ||
//m.Use(mux.MiddlewareFunc(contentWriter)) | ||
m.Use(mux.CORSMethodMiddleware(m)) | ||
//m.Use(mux.MiddlewareFunc(accessController)) | ||
m.HandleFunc("/.*", GenericHandler) | ||
return m | ||
} | ||
|
||
func GenericHandler(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("/ping")) | ||
log.Info().Msgf("got request: %s", r.URL) | ||
} | ||
|
||
func (p Proxy) Start() { | ||
addr := p.cfg.Address() | ||
log.Info().Msgf("[Main] listening on address %s", addr) | ||
server := &http.Server{ | ||
ReadTimeout: 2 * time.Second, | ||
Addr: addr, | ||
Handler: http.HandlerFunc(GenericHandler), | ||
} | ||
go func(server *http.Server) { | ||
err := server.ListenAndServe() | ||
if err != nil { | ||
log.Fatal().Msgf("[Main] error listening for rerquests on port: %s err: %v\n", addr, err) | ||
} | ||
}(server) | ||
|
||
<-watchSignal() | ||
// stop HTTP server | ||
ctx, canc := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer canc() | ||
if err := server.Shutdown(ctx); err != nil { | ||
log.Error().Msgf("[Main] error shutting down server\n") | ||
} | ||
|
||
} | ||
|
||
func watchSignal() chan os.Signal { | ||
stop := make(chan os.Signal, 2) | ||
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) | ||
return stop | ||
} | ||
|
||
func NewProxy(cfg Config) Proxy { return Proxy{cfg} } |