Skip to content

Add HTTP server example #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions http-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Basic HTTP Server Example

```
gren make src/Main.gren && node app
```

Starts server on <http://localhost:3000/>
16 changes: 16 additions & 0 deletions http-server/gren.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "application",
"platform": "node",
"source-directories": [
"src"
],
"gren-version": "0.3.0",
"dependencies": {
"direct": {
"gren-lang/core": "4.0.1",
"gren-lang/node": "3.0.1",
"gren-lang/url": "3.0.0"
},
"indirect": {}
}
}
90 changes: 90 additions & 0 deletions http-server/src/Main.gren
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
module Main exposing (main)

import Node
import Stream exposing (Stream)
import Node exposing (Environment, Program)
import HttpServer as Http exposing (ServerError(..))
import HttpServer.Response as Response exposing (Response)
import Init
import Task
import Url


main : Program Model Msg
main =
Node.defineProgram
{ init = init
, update = update
, subscriptions = subscriptions
}


type alias Model =
{ stdout : Stream
, stderr : Stream
, server : Maybe Http.Server
}


type Msg
= CreateServerResult (Result Http.ServerError Http.Server)
| GotRequest Http.Request Response


init : Environment -> Init.Task { model : Model, command : Cmd Msg }
init env =
Init.await Http.initialize <| \serverPermission ->
Node.startProgram
{ model =
{ stdout = env.stdout
, stderr = env.stderr
, server = Nothing
}
, command =
Task.attempt CreateServerResult <|
Http.createServer serverPermission
{ host = "0.0.0.0"
, port_ = 3000
}
}


update : Msg -> Model -> { model : Model, command : Cmd Msg }
update msg model =
case msg of
CreateServerResult result ->
case result of
Ok server ->
{ model = { model | server = Just server }
, command = Stream.sendLine model.stdout
"Server started"
}
Err (ServerError code message) ->
{ model = model
, command = Stream.sendLine model.stderr <|
"Server failed to start: " ++ code ++ "\n" ++ message
}

GotRequest req res ->
let
body =
Url.toString req.url
in
{ model = model
, command =
res
|> Response.setStatus 200
|> Response.setHeader "Content-type" "text/html"
|> Response.setBody ("<html>" ++ body ++ "</html>")
|> Response.send
}


subscriptions : Model -> Sub Msg
subscriptions model =
case model.server of
Just server ->
Http.onRequest server GotRequest

Nothing ->
Sub.none