-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from ethereum-optimism/10-24-feat_admin_add_s…
…imple_admin_server_with__ready feat(admin): add simple admin server with /ready
- Loading branch information
Showing
5 changed files
with
220 additions
and
0 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
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", | ||
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 | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.