Skip to content

Commit

Permalink
WIP upgrade to gren 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
blaix committed Feb 7, 2025
1 parent b37b335 commit 3aaa2c4
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 50 deletions.
37 changes: 32 additions & 5 deletions examples/v3/hello-world/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/v3/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
]
},
"dependencies": {
"gren-lang": "^0.4.5",
"gren-lang": "^0.5.2",
"prettynice": "file:../../../cli"
},
"devDependencies": {
Expand Down
14 changes: 7 additions & 7 deletions examples/v3/hello-world/server/gren.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
"src",
".prettynice"
],
"gren-version": "0.4.5",
"gren-version": "0.5.2",
"dependencies": {
"direct": {
"blaix/prettynice": "local:../../../..",
"gren-lang/core": "5.0.2",
"gren-lang/node": "4.1.1",
"icidasset/html-gren": "4.2.0"
"gren-lang/core": "6.0.1",
"gren-lang/node": "5.0.0",
"icidasset/html-gren": "local:../../../../../html-gren/html-gren"
},
"indirect": {
"gren-lang/parser": "4.0.0",
"gren-lang/url": "4.0.0",
"joeybright/gren-static-serve": "2.0.4"
"gren-lang/parser": "5.0.0",
"gren-lang/url": "5.0.0",
"joeybright/gren-static-serve": "3.0.0"
}
}
}
2 changes: 1 addition & 1 deletion examples/v3/hello-world/server/src/Main.gren
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Msg =

update : Msg -> Model -> { model : Model, command : Cmd Msg }
update msg model =
case msg of
when msg is
NoOp ->
{ model = model
, command = Cmd.none
Expand Down
12 changes: 6 additions & 6 deletions gren.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"Prettynice.Request",
"Prettynice.Response"
],
"gren-version": "0.4.5 <= v < 0.5.0",
"gren-version": "0.5.0 <= v < 0.6.0",
"dependencies": {
"gren-lang/core": "5.0.2 <= v < 6.0.0",
"gren-lang/node": "4.1.1 <= v < 5.0.0",
"gren-lang/parser": "4.0.0 <= v < 5.0.0",
"gren-lang/url": "4.0.0 <= v < 5.0.0",
"gren-lang/core": "6.0.0 <= v < 7.0.0",
"gren-lang/node": "5.0.0 <= v < 6.0.0",
"gren-lang/parser": "5.0.0 <= v < 6.0.0",
"gren-lang/url": "5.0.0 <= v < 6.0.0",
"icidasset/html-gren": "4.2.0 <= v < 5.0.0",
"joeybright/gren-static-serve": "2.0.4 <= v < 3.0.0"
"joeybright/gren-static-serve": "3.0.0 <= v < 4.0.0"
}
}
50 changes: 48 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"dependencies": {
"gren-lang": "^0.4.5",
"gren-lang": "^0.5.2",
"prettynice": "file:cli"
},
"devDependencies": {
Expand Down
60 changes: 40 additions & 20 deletions src/Prettynice.gren
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and [Node.startProgram](https://packages.gren-lang.org/package/gren-lang/node/ve
-}

import Bytes exposing (Bytes)
import FileSystem
import HttpServer as Http exposing (ServerError(..), Method(..))
import HttpServer.Response as HttpResponse
Expand All @@ -40,7 +41,7 @@ import Node exposing (Environment, Program)
import Prettynice.Request exposing (Request)
import Prettynice.Response exposing (Response)
import Server.Static as Static exposing (Mode(..))
import Stream exposing (Stream)
import Stream
import Task exposing (Task)
import Transmutable.Html as H
import Transmutable.Html.Attributes as A
Expand All @@ -49,8 +50,8 @@ import Url exposing (Url)

type alias Model appModel =
{ server : Maybe Http.Server
, stdout : Stream
, stderr : Stream
, stdout : Stream.Writable Bytes
, stderr : Stream.Writable Bytes
, host : String
, port_ : Int
, env : Environment
Expand Down Expand Up @@ -223,11 +224,19 @@ startProgramRecord config =

type Msg appModel appMsg
= CreateServerResult (Result Http.ServerError Http.Server)
| GotRequest Http.Request HttpResponse.Response
| GotRequest { request : Http.Request, response : HttpResponse.Response }
| ResponseReady Response
| MsgToApp appMsg


makeGotRequestMsg : Http.Request -> HttpResponse.Response -> Msg appModel appMsg
makeGotRequestMsg request response =
GotRequest
{ request = request
, response = response
}


type alias AppUpdate appModel appMsg =
appMsg -> appModel -> { model : appModel, command : Cmd appMsg }

Expand All @@ -240,25 +249,23 @@ update :
-> Model appModel
-> { model : Model appModel, command : Cmd (Msg appModel appMsg) }
update appUpdate msg model =
case msg of
when msg is
CreateServerResult result ->
case result of
when result is
Ok server ->
{ model = { model | server = Just server }
, command =
Task.execute <|
Stream.sendLine model.stdout <|
"Server started on http://" ++ model.host ++ ":" ++ String.fromInt model.port_
print model.stdout <|
"Server started on http://" ++ model.host ++ ":" ++ String.fromInt model.port_
}
Err (ServerError code message) ->
Err (ServerError { code, message }) ->
{ model = model
, command =
Task.execute <|
Stream.sendLine model.stderr <|
"Server failed to start: " ++ code ++ "\n" ++ message
print model.stderr <|
"Server failed to start: " ++ code ++ "\n" ++ message
}

GotRequest req res ->
GotRequest { request = req, response = res } ->
let
request =
Prettynice.Request.new req
Expand Down Expand Up @@ -297,7 +304,7 @@ update appUpdate msg model =
}

MsgToApp appMsg ->
case appUpdate.update of
when appUpdate.update is
Nothing ->
-- non-programs can't receive messages
{ model = model
Expand All @@ -316,12 +323,15 @@ update appUpdate msg model =

{-| Log a request with a custom message if task succeeds.
-}
logSuccess : Stream -> Http.Request -> String -> Task x a -> Task x a
logSuccess : Stream.Writable Bytes -> Http.Request -> String -> Task x a -> Task x a
logSuccess stream request message task =
let
printMsg =
fullMessage =
Http.requestInfo request ++ " : " ++ message
|> Stream.sendLine stream

printMsg =
Stream.writeLineAsBytes fullMessage stream
|> Task.onError (\_ -> Task.succeed stream)
in
task
|> Task.andThen
Expand All @@ -336,12 +346,22 @@ logSuccess stream request message task =

subscriptions : (appModel -> Sub appMsg) -> Model appModel -> Sub (Msg appModel appMsg)
subscriptions appSubs model =
case model.server of
when model.server is
Just server ->
Sub.batch
[ Http.onRequest server GotRequest
[ Http.onRequest server makeGotRequestMsg
, Sub.map MsgToApp <| appSubs model.appModel
]

Nothing ->
Sub.none


-- UTIL


print : Stream.Writable Bytes -> String -> Cmd (Msg appModel appMsg)
print stream string =
Stream.writeLineAsBytes string stream
|> Task.onError (\_ -> Task.succeed stream)
|> Task.execute
4 changes: 2 additions & 2 deletions src/Prettynice/FormData.gren
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ fromString body =
body
|> String.split "&"
|> Array.map (String.split "=")
|> Array.filterMap
|> Array.mapAndKeepJust
(\field ->
case field of
when field is
[ name, value ] ->
decode name value

Expand Down
4 changes: 2 additions & 2 deletions src/Prettynice/Internal/Props.gren
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ addFieldSig name fieldType sigs =

fieldTypeToString : FieldType -> String
fieldTypeToString fieldType =
case fieldType of
when fieldType is
IntType ->
"Int"

Expand Down Expand Up @@ -150,7 +150,7 @@ addFieldEncoder name fieldType encoders =

fieldTypeToEncoder : FieldType -> String
fieldTypeToEncoder fieldType =
case fieldType of
when fieldType is
IntType ->
"Encode.int"

Expand Down
Loading

0 comments on commit 3aaa2c4

Please sign in to comment.