diff --git a/api/api.go b/api/api.go index 3b5d6bf..a9019a3 100644 --- a/api/api.go +++ b/api/api.go @@ -3,7 +3,7 @@ package api import ( "github.com/gorilla/mux" "gitlab.hpi.de/codeocean/codemoon/poseidon/api/auth" - "gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool" + "gitlab.hpi.de/codeocean/codemoon/poseidon/environment" "gitlab.hpi.de/codeocean/codemoon/poseidon/logging" "net/http" ) @@ -21,7 +21,7 @@ const ( // always returns a router for the newest version of our API. We // use gorilla/mux because it is more convenient than net/http, e.g. // when extracting path parameters. -func NewRouter(runnerPool pool.RunnerPool) *mux.Router { +func NewRouter(runnerPool environment.RunnerPool) *mux.Router { router := mux.NewRouter() // this can later be restricted to a specific host with // `router.Host(...)` and to HTTPS with `router.Schemes("https")` @@ -32,7 +32,7 @@ func NewRouter(runnerPool pool.RunnerPool) *mux.Router { // newRouterV1 returns a sub-router containing the routes of version // 1 of our API. -func newRouterV1(router *mux.Router, runnerPool pool.RunnerPool) *mux.Router { +func newRouterV1(router *mux.Router, runnerPool environment.RunnerPool) *mux.Router { v1 := router.PathPrefix(RouteBase).Subrouter() v1.HandleFunc(RouteHealth, Health).Methods(http.MethodGet) diff --git a/api/api_test.go b/api/api_test.go index a094ee6..83a580c 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -4,7 +4,7 @@ import ( "github.com/gorilla/mux" "github.com/stretchr/testify/assert" "gitlab.hpi.de/codeocean/codemoon/poseidon/config" - "gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool" + "gitlab.hpi.de/codeocean/codemoon/poseidon/environment" "net/http" "net/http/httptest" "testing" @@ -17,7 +17,7 @@ func mockHTTPHandler(writer http.ResponseWriter, _ *http.Request) { func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) { config.Config.Server.Token = "" router := mux.NewRouter() - v1 := newRouterV1(router, pool.NewLocalRunnerPool()) + v1 := newRouterV1(router, environment.NewLocalRunnerPool()) t.Run("health route is accessible", func(t *testing.T) { request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil) @@ -44,7 +44,7 @@ func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) { func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) { config.Config.Server.Token = "TestToken" router := mux.NewRouter() - v1 := newRouterV1(router, pool.NewLocalRunnerPool()) + v1 := newRouterV1(router, environment.NewLocalRunnerPool()) t.Run("health route is accessible", func(t *testing.T) { request, err := http.NewRequest(http.MethodGet, "/api/v1/health", nil) diff --git a/api/runners.go b/api/runners.go index c50ac5c..c3b9f4c 100644 --- a/api/runners.go +++ b/api/runners.go @@ -7,7 +7,6 @@ import ( "gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto" "gitlab.hpi.de/codeocean/codemoon/poseidon/config" "gitlab.hpi.de/codeocean/codemoon/poseidon/environment" - "gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool" "gitlab.hpi.de/codeocean/codemoon/poseidon/runner" "net/http" "net/url" @@ -95,24 +94,24 @@ func connectToRunner(writer http.ResponseWriter, request *http.Request) { // The findRunnerMiddleware looks up the runnerId for routes containing it // and adds the runner to the context of the request. -func findRunnerMiddleware(runnerPool pool.RunnerPool) func(handler http.Handler) http.Handler { +func findRunnerMiddleware(runnerPool environment.RunnerPool) func(handler http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { // Find runner runnerId := mux.Vars(request)[RunnerIdKey] - r, ok := runnerPool.GetRunner(runnerId) + r, ok := runnerPool.Get(runnerId) if !ok { writer.WriteHeader(http.StatusNotFound) return } - ctx := runner.NewContext(request.Context(), r) + ctx := runner.NewContext(request.Context(), r.(runner.Runner)) requestWithRunner := request.WithContext(ctx) next.ServeHTTP(writer, requestWithRunner) }) } } -func registerRunnerRoutes(router *mux.Router, runnerPool pool.RunnerPool) { +func registerRunnerRoutes(router *mux.Router, runnerPool environment.RunnerPool) { router.HandleFunc("", provideRunner).Methods(http.MethodPost) runnerRouter := router.PathPrefix(fmt.Sprintf("/{%s}", RunnerIdKey)).Subrouter() runnerRouter.Use(findRunnerMiddleware(runnerPool)) diff --git a/api/runners_test.go b/api/runners_test.go index b18e30d..ea023ba 100644 --- a/api/runners_test.go +++ b/api/runners_test.go @@ -7,7 +7,7 @@ import ( "github.com/gorilla/mux" "github.com/stretchr/testify/assert" "gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto" - "gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool" + "gitlab.hpi.de/codeocean/codemoon/poseidon/environment" "gitlab.hpi.de/codeocean/codemoon/poseidon/runner" "net/http" "net/http/httptest" @@ -17,10 +17,10 @@ import ( ) func TestFindRunnerMiddleware(t *testing.T) { - runnerPool := pool.NewLocalRunnerPool() + runnerPool := environment.NewLocalRunnerPool() var capturedRunner runner.Runner testRunner := runner.NewExerciseRunner("testRunner") - runnerPool.AddRunner(testRunner) + runnerPool.Add(testRunner) testRunnerIdRoute := func(writer http.ResponseWriter, request *http.Request) { var ok bool @@ -66,10 +66,10 @@ func TestFindRunnerMiddleware(t *testing.T) { } func TestExecuteRoute(t *testing.T) { - runnerPool := pool.NewLocalRunnerPool() + runnerPool := environment.NewLocalRunnerPool() router := NewRouter(runnerPool) testRunner := runner.NewExerciseRunner("testRunner") - runnerPool.AddRunner(testRunner) + runnerPool.Add(testRunner) path, err := router.Get(ExecutePath).URL(RunnerIdKey, testRunner.Id()) if err != nil { diff --git a/main.go b/main.go index 8e37dce..7af381f 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func runServer(server *http.Server) { } } -func initServer(runnerPool pool.RunnerPool) *http.Server { +func initServer(runnerPool environment.RunnerPool) *http.Server { return &http.Server{ Addr: config.Config.PoseidonAPIURL().Host, WriteTimeout: time.Second * 15, @@ -75,7 +75,7 @@ func main() { } // ToDo: Move to create execution environment - runnerPool := pool.NewLocalRunnerPool() + runnerPool := environment.NewLocalRunnerPool() environment.DebugInit(runnerPool, nomadAPIClient) server := initServer(runnerPool)