Skip to content

Commit

Permalink
Add optional questionnaire websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
janslifka committed Apr 18, 2024
1 parent 318968c commit 9e15a73
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions engine-shared/elm/Shared/AbstractAppState.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Shared.Data.BootstrapConfig exposing (BootstrapConfig)
type alias AbstractAppState a =
{ a
| apiUrl : String
, webSocketUrl : Maybe String
, session : Session
, config : BootstrapConfig
}
16 changes: 12 additions & 4 deletions engine-shared/elm/Shared/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ httpPut url body appState toMsg =

wsUrl : String -> AbstractAppState b -> String
wsUrl url appState =
String.replace "http" "ws" <| authorizedUrl url appState
String.replace "http" "ws" <| authorizedWebSocketUrl url appState


authorizationHeaders : AbstractAppState b -> List Http.Header
Expand All @@ -232,17 +232,25 @@ authorizationHeaders appState =
[]


authorizedUrl : String -> AbstractAppState b -> String
authorizedUrl url appState =
authorizedWebSocketUrl : String -> AbstractAppState b -> String
authorizedWebSocketUrl url appState =
let
token =
if not <| String.isEmpty appState.session.token.token then
"?" ++ "Authorization=Bearer%20" ++ appState.session.token.token

else
""

apiUrl =
case appState.webSocketUrl of
Just webSocketUrl ->
webSocketUrl

Nothing ->
String.replace "http" "ws" appState.apiUrl
in
appState.apiUrl ++ url ++ token
apiUrl ++ url ++ token


expectJson : ToMsg a msg -> Decoder a -> Http.Expect msg
Expand Down
2 changes: 2 additions & 0 deletions engine-wizard/elm/Wizard/Common/AppState.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type alias AppState =
, key : Key
, apiUrl : String
, clientUrl : String
, webSocketUrl : Maybe String
, config : BootstrapConfig
, provisioning : Provisioning
, valid : Bool
Expand Down Expand Up @@ -111,6 +112,7 @@ init flagsValue key =
, key = key
, apiUrl = flags.apiUrl
, clientUrl = flags.clientUrl
, webSocketUrl = flags.webSocketUrl
, config = flags.config
, provisioning = provisioning
, valid = flags.success
Expand Down
3 changes: 3 additions & 0 deletions engine-wizard/elm/Wizard/Common/Flags.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type alias Flags =
, seed : Int
, apiUrl : String
, clientUrl : String
, webSocketUrl : Maybe String
, config : BootstrapConfig
, provisioning : Provisioning
, localProvisioning : Provisioning
Expand All @@ -39,6 +40,7 @@ decoder =
|> D.required "seed" D.int
|> D.required "apiUrl" D.string
|> D.required "clientUrl" D.string
|> D.optional "webSocketUrl" (D.maybe D.string) Nothing
|> D.required "config" BootstrapConfig.decoder
|> D.optional "provisioning" Provisioning.decoder Provisioning.default
|> D.optional "localProvisioning" Provisioning.decoder Provisioning.default
Expand All @@ -57,6 +59,7 @@ default =
, seed = 0
, apiUrl = ""
, clientUrl = ""
, webSocketUrl = Nothing
, config = BootstrapConfig.default
, provisioning = Provisioning.default
, localProvisioning = Provisioning.default
Expand Down
5 changes: 5 additions & 0 deletions engine-wizard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ function localProvisioning() {
return null
}

function getWebSocketUrl() {
return window.app && window.app['websocketUrl']
}

function bootstrapErrorHTML(errorCode) {
const title = errorCode ? (errorCode === 423 ? 'Plan expired' : 'Bootstrap Error') : 'Bootstrap Error'
const message = errorCode ? (errorCode === 423 ? 'The application does not have any active plan.' : 'Server responded with an error code ' + errorCode + '.') : 'Configuration cannot be loaded due to server unavailable.'
Expand Down Expand Up @@ -107,6 +111,7 @@ function loadApp(config, locale, provisioning) {
selectedLocale: JSON.parse(localStorage.locale || null),
apiUrl: getApiUrl(config),
clientUrl: clientUrl(),
webSocketUrl : getWebSocketUrl(),
config: config,
provisioning: provisioning,
localProvisioning: localProvisioning(),
Expand Down

0 comments on commit 9e15a73

Please sign in to comment.