Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ build:
-ldflags "-X github.com/factorysh/microdensity/version.version=$(GIT_VERSION)" \
.

build-linux:
make build GOOS=linux
upx microdensity

TESTS= github.com/factorysh/microdensity/task \
github.com/factorysh/microdensity/middlewares/jwt \
github.com/factorysh/microdensity/middlewares/project\
Expand Down
27 changes: 26 additions & 1 deletion application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ type Application struct {
Domain string
GitlabURL string
Router http.Handler
AdminRouter http.Handler
storage storage.Storage
volumes *volumes.Volumes
logger *zap.Logger
queue *queue.Queue
Sink events.Sink
Server *http.Server
AdminServer *http.Server
Stopper chan (os.Signal)
}

Expand Down Expand Up @@ -77,6 +79,14 @@ func New(cfg *conf.Conf) (*Application, error) {
logger.Info("There is no Sentry set")
}

ar := chi.NewRouter()
ar.Use(middleware.Logger)
ar.Use(middleware.Recoverer)
ar.Get("/", AdminHomeHandler)
ar.Get("/metrics", promhttp.Handler().ServeHTTP)
ar.Get("/robots.txt", RobotsHandler)
ar.Get("/favicon.png", FaviconHandler)

sessions := sessions.New()
err = sessions.Start(15)
if err != nil {
Expand Down Expand Up @@ -117,6 +127,7 @@ func New(cfg *conf.Conf) (*Application, error) {
serviceFolder: cfg.Services,
storage: s,
Router: MagicPathHandler(r),
AdminRouter: ar,
volumes: v,
logger: logger,
queue: &q,
Expand All @@ -137,7 +148,6 @@ func New(cfg *conf.Conf) (*Application, error) {
r.Get("/", a.HomeHandler())
r.Get("/robots.txt", RobotsHandler)
r.Get("/favicon.png", FaviconHandler)
r.Get("/metrics", promhttp.Handler().ServeHTTP)
r.Get("/oauth/callback", oauth.CallbackHandler(&cfg.OAuth, &sessions))

r.Get("/services", a.ServicesHandler)
Expand Down Expand Up @@ -266,6 +276,21 @@ func (a *Application) Run(listen string) error {
return nil
}

func (a *Application) AdminRun(listen string) error {
a.AdminServer = &http.Server{
Addr: listen,
Handler: a.AdminRouter,
}

// start and serve
go func() {
if err := a.AdminServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
a.logger.Fatal("fatal error when running server", zap.Error(err))
}
}()
return nil
}

// Shutdown the server and put running tasks into interrupted state
func (a *Application) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
Expand Down
27 changes: 19 additions & 8 deletions application/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func acceptsHTML(r *http.Request) bool {
return false
}

const logo = `
_ _ _
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \
| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) |
\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/
|___/
`

// HomeHandler display the home page
func (a *Application) HomeHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -44,14 +53,7 @@ func (a *Application) HomeHandler() http.HandlerFunc {
// nice geeky ascii art
if !acceptsHTML(r) {
w.Header().Set("content-type", "text/plain")
w.Write([]byte(`
_ _ _
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \
| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) |
\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/
|___/
`))
w.Write([]byte(logo))
fmt.Fprintf(w, "Version: %s", version.Version())
return
}
Expand All @@ -72,3 +74,12 @@ func (a *Application) HomeHandler() http.HandlerFunc {
}
}
}

func AdminHomeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/plain")
w.Write([]byte(logo))
fmt.Fprintf(w, "Version: %s", version.Version())
w.Write([]byte(`
/metrics Prometheus export
`))
}
3 changes: 2 additions & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type Conf struct {
Services string `yaml:"services"` // Service folder
JWKProvider string `yaml:"jwk_provider"`
Listen string `yaml:"listen"` // http listen address
AdminListen string `yaml:"admin_listen"`
DataPath string `yaml:"data_path"`
Hosts []string `yaml:"hosts"`
Hosts []string `yaml:"hosts"` // private hostnames for exposing private services, like browserless
}

func (c *Conf) Defaults() {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func main() {
l.Error("Run", zap.Error(err))
os.Exit(1)
}
if cfg.AdminListen == "" {
cfg.AdminListen = "127.0.0.1:3615"
}
err = a.AdminRun(cfg.AdminListen)
if err != nil {
l.Error("AdminRun", zap.Error(err))
os.Exit(1)
}

<-a.Stopper

Expand Down
13 changes: 12 additions & 1 deletion run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"github.com/factorysh/microdensity/task"
"github.com/factorysh/microdensity/volumes"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
serviceRun = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "run_total",
Help: "Total tasks run",
}, []string{"service", "project"})
)

// Context is a run context, with a STDOUT and a STDERR
Expand Down Expand Up @@ -86,6 +95,8 @@ func (r *Runner) Run(t *task.Task) (int, error) {
if !found {
return 0, fmt.Errorf("task with id `%s` not found in runner", t.Id)
}

defer serviceRun.With(prometheus.Labels{
"service": t.Service,
"project": t.Project}).Inc()
return ctx.run.Run(ctx.Stdout, ctx.Stderr)
}