Skip to content

Commit

Permalink
Fix for using serving host:port if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Carlbark committed Jul 19, 2016
1 parent 96b657e commit 875525e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
47 changes: 27 additions & 20 deletions Lagun.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ type alias Model =
spec : Maybe Spec,
expanded : Set String,
paramValues : ParameterValues,
requestResults : RequestResults }
requestResults : RequestResults,
servingHost: String }


init : { specUrl : String } -> ( Model, Cmd Msg )
init : { specUrl : String, servingHost: String } -> ( Model, Cmd Msg )
init flags =
( Model flags.specUrl Maybe.Nothing Set.empty Dict.empty Dict.empty, getJsonSpec flags.specUrl)
( Model flags.specUrl Maybe.Nothing Set.empty Dict.empty Dict.empty flags.servingHost, getJsonSpec flags.specUrl flags.servingHost)



Expand All @@ -47,48 +48,49 @@ update action model =
url =
(Maybe.withDefault model.specUrl maybeUrl)
in
( Model url model.spec model.expanded model.paramValues model.requestResults
, getJsonSpec url
( Model url model.spec model.expanded model.paramValues model.requestResults model.servingHost
, getJsonSpec url model.servingHost
)

FetchSpecOk spec ->
( Model model.specUrl (Maybe.Just spec) model.expanded model.paramValues model.requestResults
( Model model.specUrl (Maybe.Just spec) model.expanded model.paramValues model.requestResults model.servingHost
, Cmd.none
)

FetchSpecFail (Http.UnexpectedPayload error) ->
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults model.servingHost
, debugCmd (debugOutput "Spec parse failure" error)) -- TODO: Actually show the error message to the user

FetchSpecFail Http.Timeout ->
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults model.servingHost
, debugCmd (debugOutput "Spec fetch timed out" ""))

FetchSpecFail Http.NetworkError ->
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults model.servingHost
, debugCmd (debugOutput "Spec fetch failed due to a network error" ""))

FetchSpecFail (Http.BadResponse code msg) ->
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults
(Model model.specUrl Maybe.Nothing model.expanded model.paramValues model.requestResults model.servingHost
, debugCmd (debugOutput "Spec fetch failed due to a http error" msg))

ExpansionToggled expanded ->
( Model model.specUrl model.spec expanded model.paramValues model.requestResults
( Model model.specUrl model.spec expanded model.paramValues model.requestResults model.servingHost
, Cmd.none
)

TryRequest (path', verb) request ->
( model, tryRequest path' verb request )

RequestResult key result ->
( Model model.specUrl model.spec model.expanded model.paramValues (Dict.insert key result model.requestResults),
Cmd.none )
( Model model.specUrl model.spec model.expanded model.paramValues (Dict.insert key result model.requestResults) model.servingHost
, Cmd.none
)

RequestFail errorMsg ->
(model, Cmd.none) -- TODO Actually show the error message

ParameterInput paramValues ->
( Model model.specUrl model.spec model.expanded paramValues model.requestResults
( Model model.specUrl model.spec model.expanded paramValues model.requestResults model.servingHost
, Cmd.none
)

Expand All @@ -110,9 +112,9 @@ tryRequest path' verb req =
Task.perform RequestFail (\r -> RequestResult (path', verb) r) (Http.send settings req)


getJsonSpec : String -> Cmd Msg
getJsonSpec url =
Task.perform FetchSpecFail FetchSpecOk (Http.get (decodeSpec (extractHost url)) url)
getJsonSpec : String -> String -> Cmd Msg
getJsonSpec url servingHost =
Task.perform FetchSpecFail FetchSpecOk (Http.get (decodeSpec (extractHost url servingHost)) url)


type alias RequestResults =
Expand Down Expand Up @@ -238,12 +240,17 @@ optionalFieldWithDefault : String -> String -> Json.Decoder String
optionalFieldWithDefault field default =
Json.oneOf [ field := Json.string, Json.succeed default ]

extractHost : String -> String
extractHost url =
extractHost : String -> String -> String
extractHost url servingHost = -- expects servingHost to be something like: "localhost:1337"
let
parts = String.split "/" url
isHttp = (String.left 4 url) == "http"
in
Maybe.withDefault "localhost" (List.drop 2 parts |> List.head)
case isHttp of
True ->
Maybe.withDefault servingHost (List.drop 2 parts |> List.head)
False ->
servingHost

decodeSpec : String -> Json.Decoder Spec
decodeSpec defaultHost =
Expand Down
2 changes: 1 addition & 1 deletion Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import View exposing (view)
import Html.App as Html


main : Program { specUrl : String }
main : Program { specUrl : String, servingHost : String }
main =
Html.programWithFlags
{ init = init
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ build: elm.js ;
clean:
rm elm.js

elm.js:
elm.js: Main.elm Lagun.elm View.elm elm-package.json
elm-make Main.elm --warn --output elm.js
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ Uses:
- [Font Awesome](https://fortawesome.github.io/Font-Awesome/)
- [heroku-buildpack-elm](https://github.com/srid/heroku-buildpack-elm)

[Demo on Heroku](http://trylagun.herokuapp.com/) (also available on [http://vorce.github.io/lagun/](http://vorce.github.io/lagun/))
[Demo on Github pages for swagger's petstore sample API](http://vorce.github.io/lagun/) (Also available on [trylagun.herokuapp.com](http://trylagun.herokuapp.com/))

## Build

elm-make Main.elm --warn --output elm.js

Open `index.html`
or

make

If everything compiles fine open `index.html` to use Lagun.
If you want to change the default specification url simply edit `index.html`, and modify the `specUrl` setting passed into `Elm.Main.fullscreen`.

## Supported specification formats

Expand All @@ -32,4 +37,4 @@ I doubt I will add support for other specs (such as [RAML](http://raml.org/)). P
- Some tests would be fun and useful
- Show version of Lagun somewhere on the page (bottom?)
- Structure the code in a nicer way
- Make it friendlier for real usage: set url of spec, disable input of url of spec etc

3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

<script type="text/javascript">
Elm.Main.fullscreen({
specUrl: 'http://petstore.swagger.io/v2/swagger.json'
specUrl: 'http://petstore.swagger.io/v2/swagger.json',
servingHost: location.hostname+(location.port ? ':'+location.port: '')
});
</script>

Expand Down

0 comments on commit 875525e

Please sign in to comment.