Skip to content
Open
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
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"
"time"
"runtime"
)

func main() {
Expand All @@ -16,6 +17,9 @@ func main() {
e.Use(delayImageMiddleware)
e.Static("/", "static")

e.GET("/health", healthHandler)
e.GET("/metrics", metricsHandler)

if os.Getenv("HTTP2") != "" {
log.Printf("starting http2 server...")
//net/http.Server is a http1.1 server with optional http2 support
Expand All @@ -36,3 +40,19 @@ func delayImageMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return next(c)
}
}

func healthHandler(c echo.Context) error {
return c.String(http.StatusOK, "OK")
}

func metricsHandler(c echo.Context) error {
var m runtime.MemStats
runtime.ReadMemStats(&m)
metrics := map[string]uint64{
"Alloc": m.Alloc,
"TotalAlloc": m.TotalAlloc,
"Sys": m.Sys,
"NumGC": uint64(m.NumGC),
}
return c.JSON(http.StatusOK, metrics)
}
37 changes: 37 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)

func TestHealthHandler(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/health", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

if assert.NoError(t, healthHandler(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "OK", rec.Body.String())
}
}

func TestMetricsHandler(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

if assert.NoError(t, metricsHandler(c)) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, rec.Body.String(), "Alloc")
assert.Contains(t, rec.Body.String(), "TotalAlloc")
assert.Contains(t, rec.Body.String(), "Sys")
assert.Contains(t, rec.Body.String(), "NumGC")
}
}