Skip to content

Commit fee0d10

Browse files
authored
Merge pull request #52 from factorysh/admin_http
HTTP Admin
2 parents 45eca92 + cd28739 commit fee0d10

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ build:
55
-ldflags "-X github.com/factorysh/microdensity/version.version=$(GIT_VERSION)" \
66
.
77

8+
build-linux:
9+
make build GOOS=linux
10+
upx microdensity
11+
812
TESTS= github.com/factorysh/microdensity/task \
913
github.com/factorysh/microdensity/middlewares/jwt \
1014
github.com/factorysh/microdensity/middlewares/project\

application/application.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ type Application struct {
3939
Domain string
4040
GitlabURL string
4141
Router http.Handler
42+
AdminRouter http.Handler
4243
storage storage.Storage
4344
volumes *volumes.Volumes
4445
logger *zap.Logger
4546
queue *queue.Queue
4647
Sink events.Sink
4748
Server *http.Server
49+
AdminServer *http.Server
4850
Stopper chan (os.Signal)
4951
}
5052

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

82+
ar := chi.NewRouter()
83+
ar.Use(middleware.Logger)
84+
ar.Use(middleware.Recoverer)
85+
ar.Get("/", AdminHomeHandler)
86+
ar.Get("/metrics", promhttp.Handler().ServeHTTP)
87+
ar.Get("/robots.txt", RobotsHandler)
88+
ar.Get("/favicon.png", FaviconHandler)
89+
8090
sessions := sessions.New()
8191
err = sessions.Start(15)
8292
if err != nil {
@@ -117,6 +127,7 @@ func New(cfg *conf.Conf) (*Application, error) {
117127
serviceFolder: cfg.Services,
118128
storage: s,
119129
Router: MagicPathHandler(r),
130+
AdminRouter: ar,
120131
volumes: v,
121132
logger: logger,
122133
queue: &q,
@@ -137,7 +148,6 @@ func New(cfg *conf.Conf) (*Application, error) {
137148
r.Get("/", a.HomeHandler())
138149
r.Get("/robots.txt", RobotsHandler)
139150
r.Get("/favicon.png", FaviconHandler)
140-
r.Get("/metrics", promhttp.Handler().ServeHTTP)
141151
r.Get("/oauth/callback", oauth.CallbackHandler(&cfg.OAuth, &sessions))
142152

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

279+
func (a *Application) AdminRun(listen string) error {
280+
a.AdminServer = &http.Server{
281+
Addr: listen,
282+
Handler: a.AdminRouter,
283+
}
284+
285+
// start and serve
286+
go func() {
287+
if err := a.AdminServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
288+
a.logger.Fatal("fatal error when running server", zap.Error(err))
289+
}
290+
}()
291+
return nil
292+
}
293+
269294
// Shutdown the server and put running tasks into interrupted state
270295
func (a *Application) Shutdown() error {
271296
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)

application/home.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func acceptsHTML(r *http.Request) bool {
3333
return false
3434
}
3535

36+
const logo = `
37+
_ _ _
38+
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
39+
/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \
40+
| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) |
41+
\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/
42+
|___/
43+
`
44+
3645
// HomeHandler display the home page
3746
func (a *Application) HomeHandler() http.HandlerFunc {
3847
return func(w http.ResponseWriter, r *http.Request) {
@@ -44,14 +53,7 @@ func (a *Application) HomeHandler() http.HandlerFunc {
4453
// nice geeky ascii art
4554
if !acceptsHTML(r) {
4655
w.Header().Set("content-type", "text/plain")
47-
w.Write([]byte(`
48-
_ _ _
49-
___| |__ ___ ___| | __ _ __ ___ _ _ __ _____| |__
50-
/ __| '_ \ / _ \/ __| |/ / | '_ ' _ \| | | | \ \ /\ / / _ \ '_ \
51-
| (__| | | | __/ (__| < | | | | | | |_| | \ V V / __/ |_) |
52-
\ ___|_| |_|\___|\___|_|\_\ |_| |_| |_|\__, | \_/\_/ \___|_.__/
53-
|___/
54-
`))
56+
w.Write([]byte(logo))
5557
fmt.Fprintf(w, "Version: %s", version.Version())
5658
return
5759
}
@@ -72,3 +74,12 @@ func (a *Application) HomeHandler() http.HandlerFunc {
7274
}
7375
}
7476
}
77+
78+
func AdminHomeHandler(w http.ResponseWriter, r *http.Request) {
79+
w.Header().Set("content-type", "text/plain")
80+
w.Write([]byte(logo))
81+
fmt.Fprintf(w, "Version: %s", version.Version())
82+
w.Write([]byte(`
83+
/metrics Prometheus export
84+
`))
85+
}

conf/conf.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ type Conf struct {
1212
Services string `yaml:"services"` // Service folder
1313
JWKProvider string `yaml:"jwk_provider"`
1414
Listen string `yaml:"listen"` // http listen address
15+
AdminListen string `yaml:"admin_listen"`
1516
DataPath string `yaml:"data_path"`
16-
Hosts []string `yaml:"hosts"`
17+
Hosts []string `yaml:"hosts"` // private hostnames for exposing private services, like browserless
1718
}
1819

1920
func (c *Conf) Defaults() {

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func main() {
5757
l.Error("Run", zap.Error(err))
5858
os.Exit(1)
5959
}
60+
if cfg.AdminListen == "" {
61+
cfg.AdminListen = "127.0.0.1:3615"
62+
}
63+
err = a.AdminRun(cfg.AdminListen)
64+
if err != nil {
65+
l.Error("AdminRun", zap.Error(err))
66+
os.Exit(1)
67+
}
6068

6169
<-a.Stopper
6270

run/run.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import (
88
"github.com/factorysh/microdensity/task"
99
"github.com/factorysh/microdensity/volumes"
1010
"github.com/google/uuid"
11+
"github.com/prometheus/client_golang/prometheus"
12+
"github.com/prometheus/client_golang/prometheus/promauto"
13+
)
14+
15+
var (
16+
serviceRun = promauto.NewCounterVec(prometheus.CounterOpts{
17+
Name: "run_total",
18+
Help: "Total tasks run",
19+
}, []string{"service", "project"})
1120
)
1221

1322
// Context is a run context, with a STDOUT and a STDERR
@@ -86,6 +95,8 @@ func (r *Runner) Run(t *task.Task) (int, error) {
8695
if !found {
8796
return 0, fmt.Errorf("task with id `%s` not found in runner", t.Id)
8897
}
89-
98+
defer serviceRun.With(prometheus.Labels{
99+
"service": t.Service,
100+
"project": t.Project}).Inc()
90101
return ctx.run.Run(ctx.Stdout, ctx.Stderr)
91102
}

0 commit comments

Comments
 (0)