Implement review suggestions

Improve logging, constants for routes, RWMutex for ExerciseRunners,
use url.URL for websocket url building
This commit is contained in:
Konrad Hanff
2021-05-05 11:35:16 +02:00
parent 07cdf17eb4
commit 52b80583b5
5 changed files with 55 additions and 62 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"errors"
"fmt"
"github.com/gorilla/mux"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
@@ -9,13 +10,21 @@ import (
"gitlab.hpi.de/codeocean/codemoon/poseidon/environment/pool"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"net/http"
"net/url"
)
const (
ExecutePath = "/execute"
WebsocketPath = "/websocket"
RunnerIdKey = "runnerId"
ExecutionIdKey = "executionId"
)
// provideRunner tries to respond with the id of a runner
// This runner is then reserved for future use
func provideRunner(writer http.ResponseWriter, request *http.Request) {
runnerRequest := new(dto.RunnerRequest)
if err := parseRequestBodyJSON(writer, request, runnerRequest); err != nil {
if err := parseJSONRequestBody(writer, request, runnerRequest); err != nil {
return
}
environment, err := environment.GetExecutionEnvironment(runnerRequest.ExecutionEnvironmentId)
@@ -36,7 +45,7 @@ func provideRunner(writer http.ResponseWriter, request *http.Request) {
func executeCommand(router *mux.Router) func(w http.ResponseWriter, r *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
executionRequest := new(dto.ExecutionRequest)
if err := parseRequestBodyJSON(writer, request, executionRequest); err != nil {
if err := parseJSONRequestBody(writer, request, executionRequest); err != nil {
return
}
@@ -48,30 +57,39 @@ func executeCommand(router *mux.Router) func(w http.ResponseWriter, r *http.Requ
}
r, ok := runner.FromContext(request.Context())
if !ok {
log.Fatal("Expected runner in context! Something must be broken ...")
log.Error("Runner not set in request context.")
writeInternalServerError(writer, errors.New("findRunnerMiddleware failure"), dto.ErrorUnknown)
return
}
path, err := router.Get(WebsocketPath).URL(RunnerIdKey, r.Id())
if err != nil {
log.WithError(err).Error("Could not create runner websocket URL.")
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
id, err := r.AddExecution(*executionRequest)
if err != nil {
log.WithError(err).Error("Could not store execution.")
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
websocketUrl := url.URL{
Scheme: scheme,
Host: request.Host,
Path: path.String(),
RawQuery: fmt.Sprintf("%s=%s", ExecutionIdKey, id),
}
path, err := router.Get("runner-websocket").URL("runnerId", r.Id())
if err != nil {
log.Printf("Error creating runner websocket URL %v", err)
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
websocketUrl := fmt.Sprintf("%s://%s%s?executionId=%s", scheme, request.Host, path, id)
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl}, http.StatusOK)
sendJson(writer, &dto.WebsocketResponse{WebsocketUrl: websocketUrl.String()}, http.StatusOK)
}
}
// connectToRunner is a placeholder for now and will become the endpoint for websocket connections.
func connectToRunner(writer http.ResponseWriter, request *http.Request) {
// Todo: Execute the command, upgrade the connection to websocket and handle forwarding
executionId := request.URL.Query()["executionId"]
log.Printf("Websocket for execution %s requested", executionId)
executionId := request.URL.Query()[ExecutionIdKey]
log.WithField("executionId", executionId).Info("Websocket for execution requested.")
writer.WriteHeader(http.StatusNotImplemented)
}
@@ -81,7 +99,7 @@ func findRunnerMiddleware(runnerPool pool.RunnerPool) func(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)["runnerId"]
runnerId := mux.Vars(request)[RunnerIdKey]
r, ok := runnerPool.GetRunner(runnerId)
if !ok {
writer.WriteHeader(http.StatusNotFound)
@@ -96,8 +114,8 @@ func findRunnerMiddleware(runnerPool pool.RunnerPool) func(handler http.Handler)
func registerRunnerRoutes(router *mux.Router, runnerPool pool.RunnerPool) {
router.HandleFunc("", provideRunner).Methods(http.MethodPost)
runnerRouter := router.PathPrefix("/{runnerId}").Subrouter()
runnerRouter := router.PathPrefix(fmt.Sprintf("/{%s}", RunnerIdKey)).Subrouter()
runnerRouter.Use(findRunnerMiddleware(runnerPool))
runnerRouter.HandleFunc("/execute", executeCommand(runnerRouter)).Methods(http.MethodPost).Name("runner-execute")
runnerRouter.HandleFunc("/websocket", connectToRunner).Methods(http.MethodGet).Name("runner-websocket")
runnerRouter.HandleFunc(ExecutePath, executeCommand(runnerRouter)).Methods(http.MethodPost).Name(ExecutePath)
runnerRouter.HandleFunc(WebsocketPath, connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
}