Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(admin): add simple admin server with /ready #229

Merged
merged 1 commit into from
Oct 24, 2024
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
70 changes: 70 additions & 0 deletions admin/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package admin

import (
"context"
"net/http"
"sync"

"github.com/ethereum/go-ethereum/log"

"github.com/gin-gonic/gin"
)

type AdminServer struct {
srv *http.Server
cancel context.CancelFunc
wg sync.WaitGroup

log log.Logger
}

func NewAdminServer(log log.Logger) *AdminServer {
return &AdminServer{log: log}
}

func (s *AdminServer) Start(ctx context.Context) error {
router := setupRouter()

s.srv = &http.Server{
Addr: ":8420",
jakim929 marked this conversation as resolved.
Show resolved Hide resolved
Handler: router,
}

ctx, s.cancel = context.WithCancel(ctx)

s.wg.Add(1)
go func() {
defer s.wg.Done()
if err := s.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
s.log.Error("ListenAndServe error", "error", err)
}
}()

go func() {
<-ctx.Done()
if err := s.srv.Shutdown(context.Background()); err != nil {
s.log.Error("Server shutdown error", "error", err)
}
}()

return nil
}

func (s *AdminServer) Stop(ctx context.Context) error {
if s.cancel != nil {
s.cancel()
}
s.wg.Wait()
return nil
}

func setupRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())

router.GET("/ready", func(c *gin.Context) {
c.String(http.StatusOK, "OK")
})
return router
}
63 changes: 63 additions & 0 deletions admin/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package admin

import (
"context"
"io"
"net/http"
"testing"
"time"

"github.com/ethereum-optimism/optimism/op-service/testlog"
"github.com/ethereum/go-ethereum/log"
"github.com/stretchr/testify/require"
)

func TestAdminServerBasicFunctionality(t *testing.T) {
testlog := testlog.Logger(t, log.LevelInfo)

ctx, cancel := context.WithCancel(context.Background())
adminServer := NewAdminServer(testlog)
t.Cleanup(func() { cancel() })

require.NoError(t, adminServer.Start(ctx))

require.Equal(t, ":8420", adminServer.srv.Addr)

resp, err := http.Get("http://localhost:8420/ready")
require.NoError(t, err)
defer resp.Body.Close()

require.Equal(t, http.StatusOK, resp.StatusCode)

body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, "OK", string(body))

require.NoError(t, adminServer.Stop(context.Background()))

resp, err = http.Get("http://localhost:8420/ready")
if err == nil {
resp.Body.Close()
}
require.Error(t, err)
}

func TestAdminServerContextCancellation(t *testing.T) {
testlog := testlog.Logger(t, log.LevelInfo)

ctx, cancel := context.WithCancel(context.Background())
adminServer := NewAdminServer(testlog)

require.NoError(t, adminServer.Start(ctx))

// Cancel the context
cancel()

time.Sleep(100 * time.Millisecond)

resp, err := http.Get("http://localhost:8420/ready")
if err == nil {
resp.Body.Close()
}
require.Error(t, err)
}
21 changes: 21 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ require (
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.5 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand All @@ -43,9 +47,16 @@ require (
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand All @@ -61,17 +72,23 @@ require (
github.com/holiman/uint256 v1.3.1 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/pointerstructure v1.2.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
Expand All @@ -89,12 +106,16 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
Expand Down
Loading
Loading