Skip to content

Commit

Permalink
Switch to using new server code
Browse files Browse the repository at this point in the history
  • Loading branch information
Maelkum committed Apr 24, 2024
1 parent f8aa8b0 commit 5eb80e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions api/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/blocklessnetwork/b7s/node/aggregate"
)

// Execute implements the REST API endpoint for function execution.
func (a *API) Execute(ctx echo.Context) error {
// ExecuteFunction implements the REST API endpoint for function execution.
func (a *API) ExecuteFunction(ctx echo.Context) error {

// Unpack the API request.
var req ExecutionRequest
Expand Down
7 changes: 3 additions & 4 deletions api/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAPI_Execute(t *testing.T) {
rec, ctx, err := setupRecorder(executeEndpoint, req)
require.NoError(t, err)

err = srv.Execute(ctx)
err = srv.ExecuteFunction(ctx)
require.NoError(t, err)

var res api.ExecutionResponse
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestAPI_Execute_HandlesErrors(t *testing.T) {
rec, ctx, err := setupRecorder(executeEndpoint, req)
require.NoError(t, err)

err = srv.Execute(ctx)
err = srv.ExecuteFunction(ctx)
require.NoError(t, err)

var res api.ExecutionResponse
Expand All @@ -121,7 +121,6 @@ func TestAPI_Execute_HandlesErrors(t *testing.T) {
func TestAPI_Execute_HandlesMalformedRequests(t *testing.T) {

api := setupAPI(t)
_ = api

const (
wrongFieldType = `
Expand Down Expand Up @@ -180,7 +179,7 @@ func TestAPI_Execute_HandlesMalformedRequests(t *testing.T) {
_, ctx, err := setupRecorder(executeEndpoint, test.payload, prepare)
require.NoError(t, err)

err = api.Execute(ctx)
err = api.ExecuteFunction(ctx)
require.Error(t, err)

echoErr, ok := err.(*echo.HTTPError)
Expand Down
2 changes: 1 addition & 1 deletion api/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
functionInstallTimeout = 10 * time.Second
)

func (a *API) Install(ctx echo.Context) error {
func (a *API) InstallFunction(ctx echo.Context) error {

// Unpack the API request.
var req FunctionInstallRequest
Expand Down
10 changes: 5 additions & 5 deletions api/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestAPI_FunctionInstall(t *testing.T) {
rec, ctx, err := setupRecorder(installEndpoint, req)
require.NoError(t, err)

err = srv.Install(ctx)
err = srv.InstallFunction(ctx)
require.NoError(t, err)

require.Equal(t, http.StatusOK, rec.Result().StatusCode)
Expand All @@ -50,7 +50,7 @@ func TestAPI_FunctionInstall_HandlesErrors(t *testing.T) {
_, ctx, err := setupRecorder(installEndpoint, req)
require.NoError(t, err)

err = srv.Install(ctx)
err = srv.InstallFunction(ctx)
require.Error(t, err)

echoErr, ok := err.(*echo.HTTPError)
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAPI_FunctionInstall_HandlesErrors(t *testing.T) {
rec, ctx, err := setupRecorder(installEndpoint, req)
require.NoError(t, err)

err = srv.Install(ctx)
err = srv.InstallFunction(ctx)
require.NoError(t, err)

require.Equal(t, http.StatusOK, rec.Result().StatusCode)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestAPI_FunctionInstall_HandlesErrors(t *testing.T) {
_, ctx, err := setupRecorder(installEndpoint, req)
require.NoError(t, err)

err = srv.Install(ctx)
err = srv.InstallFunction(ctx)
require.Error(t, err)

echoErr, ok := err.(*echo.HTTPError)
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestAPI_InstallFunction_HandlesMalformedRequests(t *testing.T) {
_, ctx, err := setupRecorder(installEndpoint, test.payload, prepare)
require.NoError(t, err)

err = srv.Install(ctx)
err = srv.InstallFunction(ctx)
require.Error(t, err)

echoErr, ok := err.(*echo.HTTPError)
Expand Down
29 changes: 15 additions & 14 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,11 @@ func run() int {
}

// Create echo server and iniialize logging.
server := echo.New()
server.HideBanner = true
server.HidePort = true

elog := lecho.From(log)
server.Logger = elog
server.Use(lecho.Middleware(lecho.Config{Logger: elog}))
server := createEchoServer(log)

// Create an API handler.
api := api.New(log, node)

// Set endpoint handlers.
server.GET("/api/v1/health", api.Health)
server.POST("/api/v1/functions/execute", api.Execute)
server.POST("/api/v1/functions/install", api.Install)
server.POST("/api/v1/functions/requests/result", api.ExecutionResult)
apiHandler := api.New(log, node)
api.RegisterHandlers(server, apiHandler)

// Start API in a separate goroutine.
go func() {
Expand Down Expand Up @@ -314,6 +303,18 @@ func run() int {
return success
}

func createEchoServer(log zerolog.Logger) *echo.Echo {
server := echo.New()
server.HideBanner = true
server.HidePort = true

elog := lecho.From(log)
server.Logger = elog
server.Use(lecho.Middleware(lecho.Config{Logger: elog}))

return server
}

func needLimiter(cfg *config.Config) bool {
return cfg.Worker.CPUPercentageLimit != 1.0 || cfg.Worker.MemoryLimitKB > 0
}
Expand Down

0 comments on commit 5eb80e3

Please sign in to comment.