From 875525e4192d6b93865790b004225fee5abd721e Mon Sep 17 00:00:00 2001 From: Joel Carlbark Date: Tue, 19 Jul 2016 16:40:42 +0200 Subject: [PATCH] Fix for using serving host:port if not specified --- Lagun.elm | 47 +++++++++++++++++++++++++++-------------------- Main.elm | 2 +- Makefile | 2 +- README.md | 11 ++++++++--- index.html | 3 ++- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Lagun.elm b/Lagun.elm index b4b4496..45bf0fd 100644 --- a/Lagun.elm +++ b/Lagun.elm @@ -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) @@ -47,33 +48,33 @@ 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 ) @@ -81,14 +82,15 @@ update action model = ( 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 ) @@ -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 = @@ -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 = diff --git a/Main.elm b/Main.elm index 8bb87fe..460e4d4 100644 --- a/Main.elm +++ b/Main.elm @@ -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 diff --git a/Makefile b/Makefile index c63cef4..a65d079 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 440bb26..ae31d9b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 + diff --git a/index.html b/index.html index f23d5e8..32580c9 100644 --- a/index.html +++ b/index.html @@ -23,7 +23,8 @@