Add basic websocket structure and request upgrader

This commit is contained in:
Maximilian Paß
2021-05-07 14:10:12 +02:00
parent e07e8216f2
commit 465577fea6
4 changed files with 81 additions and 10 deletions

View File

@ -32,12 +32,12 @@ func provideRunner(writer http.ResponseWriter, request *http.Request) {
writeNotFound(writer, err)
return
}
runner, err := env.NextRunner()
nextRunner, err := env.NextRunner()
if err != nil {
writeInternalServerError(writer, err, dto.ErrorNomadOverload)
return
}
sendJson(writer, &dto.RunnerResponse{Id: runner.Id()}, http.StatusOK)
sendJson(writer, &dto.RunnerResponse{Id: nextRunner.Id()}, http.StatusOK)
}
// executeCommand takes an ExecutionRequest and stores it for a runner.
@ -80,14 +80,6 @@ func executeCommand(router *mux.Router) func(w http.ResponseWriter, r *http.Requ
}
}
// 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()[ExecutionIdKey]
log.WithField("executionId", executionId).Info("Websocket for execution requested.")
writer.WriteHeader(http.StatusNotImplemented)
}
// The findRunnerMiddleware looks up the runnerId for routes containing it
// and adds the runner to the context of the request.
func findRunnerMiddleware(runnerPool environment.RunnerPool) func(handler http.Handler) http.Handler {

46
api/websocket.go Normal file
View File

@ -0,0 +1,46 @@
package api
import (
"errors"
"github.com/gorilla/websocket"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api/dto"
"gitlab.hpi.de/codeocean/codemoon/poseidon/runner"
"net/http"
)
var connUpgrade = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// connectToRunner is a placeholder for now and will become the endpoint for websocket connections.
func connectToRunner(writer http.ResponseWriter, request *http.Request) {
r, ok := runner.FromContext(request.Context())
if !ok {
log.Error("Runner not set in request context.")
writeInternalServerError(writer, errors.New("findRunnerMiddleware failure"), dto.ErrorUnknown)
return
}
executionId := request.URL.Query().Get(ExecutionIdKey)
connClient, err := connUpgrade.Upgrade(writer, request, nil)
if err != nil {
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
defer func(connClient *websocket.Conn) {
err := connClient.Close()
if err != nil {
writeInternalServerError(writer, err, dto.ErrorUnknown)
}
}(connClient)
// ToDo: Implement communication forwarding
err, ok = r.Execute(runner.ExecutionId(executionId))
if !ok {
writeBadRequest(writer, errors.New("invalid Execution Id"))
return
} else if err != nil {
writeInternalServerError(writer, err, dto.ErrorUnknown)
return
}
}

12
api/websocket_test.go Normal file
View File

@ -0,0 +1,12 @@
package api
import (
"testing"
)
func TestInvalidExecutionId(t *testing.T) {
}
func TestEstablishWebsocketConnection(t *testing.T) {
}