Skip to content

Commit 97fc405

Browse files
committed
forgot some files
1 parent bbec6b5 commit 97fc405

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

examples/ElmWebApp/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Web App with Elm
2+
3+
A minimal web app with an Elm [frontend](https://chatgpt.com/share/93575daf-49ef-48ba-b39d-ab04672e4019) and Roc [backend](https://chatgpt.com/share/7ac35a32-dab5-46d0-bb17-9d584469556f). The Roc backend uses the [basic-webserver](https://github.com/roc-lang/basic-webserver) platform.
4+
5+
## Why Elm + Roc?
6+
7+
Roc was inspired by Elm, so it's nice to be able to use a similar language for the frontend. Elm also has a mature collection of re-usable packages.
8+
9+
## Alternatives
10+
11+
We've also enjoyed using [htmx with Roc](https://github.com/lukewilliamboswell/roc-htmx-playground). It allows you to use Roc for the frontend and the backend.
12+
13+
## Full Code
14+
15+
src/Main.elm:
16+
```elm
17+
file:src/Main.elm
18+
```
19+
20+
elm.json:
21+
```json
22+
file:elm.json
23+
```
24+
25+
index.html:
26+
```html
27+
file:index.html
28+
```
29+
30+
webserver.roc:
31+
```roc
32+
file:webserver.roc
33+
```
34+
## Running
35+
36+
### Roc
37+
38+
You can change the port on which the Roc server runs with ROC_BASIC_WEBSERVER_PORT.
39+
```
40+
cd examples/ElmWebApp/
41+
42+
# development
43+
roc backend.roc
44+
45+
# production
46+
roc build backend.roc --optimize
47+
./backend
48+
```
49+
50+
### Elm
51+
52+
> Note: for non-trivial Elm development we recommend using [elm-watch](https://github.com/lydell/elm-watch).
53+
54+
Compile elm code to javascript.
55+
56+
```
57+
cd examples/ElmWebApp/frontend
58+
# development
59+
elm make src/Main.elm --output elm.js
60+
# production
61+
elm make src/Main.elm --output elm.js --optimize
62+
```
63+
64+
Serve the frontend:
65+
```
66+
elm reactor --port 8001 # Roc backend will be on 8000
67+
```
68+
For production; use a [battle-tested HTTP server](https://chatgpt.com/share/5809a606-10ea-4ee6-b821-732465016254) instead of elm reactor.
69+
70+
Open localhost:8001/index.html in your browser.

examples/ElmWebApp/backend

7.33 MB
Binary file not shown.

examples/ElmWebApp/backend.roc

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
app [main] { pf: platform "https://github.com/roc-lang/basic-webserver/releases/download/0.4.0/iAiYpbs5zdVB75golcg_YMtgexN3e2fwhsYPLPCeGzk.tar.br" }
2+
3+
import pf.Stdout
4+
import pf.Task exposing [Task]
5+
import pf.Http exposing [Request, Response]
6+
import pf.Utc
7+
8+
# [backend](https://chatgpt.com/share/7ac35a32-dab5-46d0-bb17-9d584469556f) Roc server
9+
10+
main : Request -> Task Response []
11+
main = \req ->
12+
# Log request datetime, method and url
13+
datetime = Utc.now! |> Utc.toIso8601Str
14+
15+
Stdout.line! "$(datetime) $(Http.methodToStr req.method) $(req.url)"
16+
17+
Task.ok {
18+
status: 200,
19+
headers: [
20+
# TODO check if this header is a good idea
21+
{ name: "Access-Control-Allow-Origin", value: Str.toUtf8 "*" },
22+
],
23+
body: Str.toUtf8 "Hi, Elm! This is from Roc: 🎁\n",
24+
}

examples/ElmWebApp/frontend/elm.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"type": "application",
3+
"source-directories": [
4+
"src"
5+
],
6+
"elm-version": "0.19.1",
7+
"dependencies": {
8+
"direct": {
9+
"elm/browser": "1.0.2",
10+
"elm/core": "1.0.5",
11+
"elm/html": "1.0.0",
12+
"elm/http": "2.0.0",
13+
"elm/json": "1.1.3"
14+
},
15+
"indirect": {
16+
"elm/bytes": "1.0.8",
17+
"elm/file": "1.0.5",
18+
"elm/time": "1.0.0",
19+
"elm/url": "1.0.0",
20+
"elm/virtual-dom": "1.0.3"
21+
}
22+
},
23+
"test-dependencies": {
24+
"direct": {},
25+
"indirect": {}
26+
}
27+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head> </head>
4+
<body>
5+
<!-- app div is used by elm -->
6+
<div id="app"></div>
7+
<!-- elm is compiled to js -->
8+
<script src="elm.js"></script>
9+
<script>
10+
Elm.Main.init({ node: document.getElementById("app") });
11+
</script>
12+
</body>
13+
</html>
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
module Main exposing (..)
2+
3+
-- Importing necessary modules
4+
import Browser
5+
import Html exposing (Html, div, text, h1)
6+
import Html.Attributes exposing (..)
7+
import Http
8+
9+
-- MAIN
10+
11+
-- The main function is the entry point of an Elm application
12+
main =
13+
Browser.element
14+
{ init = init
15+
, update = update
16+
, subscriptions = subscriptions
17+
, view = view
18+
}
19+
20+
-- MODEL
21+
22+
-- Model represents the state of our application
23+
type Model
24+
= Failure String
25+
| Loading
26+
| Success String
27+
28+
-- init function sets up the initial state and any commands to run on startup
29+
init : () -> (Model, Cmd Msg)
30+
init _ =
31+
( Loading
32+
, fetchData
33+
)
34+
35+
-- UPDATE
36+
37+
-- Msg represents the different types of messages our app can receive
38+
type Msg
39+
= GotResponse (Result Http.Error String)
40+
41+
-- update function handles how the model changes in response to messages
42+
update : Msg -> Model -> (Model, Cmd Msg)
43+
update msg model =
44+
case msg of
45+
GotResponse result ->
46+
case result of
47+
Ok body ->
48+
(Success body, Cmd.none)
49+
Err error ->
50+
(Failure (Debug.toString error), Cmd.none)
51+
52+
-- VIEW
53+
54+
-- view function determines what to display in the browser based on the current model
55+
view : Model -> Html Msg
56+
view model =
57+
case model of
58+
Failure errorMsg ->
59+
text ("Is the Roc webserver running? I hit an error: " ++ errorMsg)
60+
Loading ->
61+
text "Loading..."
62+
Success body ->
63+
div [ id "app" ]
64+
[
65+
h1 [] [ text body ]
66+
]
67+
68+
-- HTTP
69+
70+
-- fetchData sends an HTTP GET request to the Roc backend
71+
fetchData : Cmd Msg
72+
fetchData =
73+
Http.get
74+
{ url = "http://localhost:8000/"
75+
, expect = Http.expectString GotResponse
76+
}
77+
78+
-- SUBSCRIPTIONS
79+
80+
-- subscriptions allow the app to listen for external input (e.g., time, websockets)
81+
-- In this case, we're not using any subscriptions
82+
subscriptions : Model -> Sub Msg
83+
subscriptions _ =
84+
Sub.none

0 commit comments

Comments
 (0)