-
Notifications
You must be signed in to change notification settings - Fork 359
Cookbook: "Start a Web Server and render HTML template" with dream and cohttp #3185
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
Open
dinakajoy
wants to merge
2
commits into
ocaml:main
Choose a base branch
from
dinakajoy:add-web-server-that-renders-html-cookbooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
data/cookbook/start-a-web-server-html-template/00-cohttp-server.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
packages: | ||
- name: "cohttp-lwt-unix" | ||
tested_version: "5.3.0" | ||
used_libraries: | ||
- cohttp-lwt-unix | ||
- name: "cohttp" | ||
tested_version: "6.1.0" | ||
used_libraries: | ||
- cohttp | ||
- name: "tyxml" | ||
tested_version: "4.6.0" | ||
used_libraries: | ||
- tyxml | ||
discussion: | | ||
This example demonstrates how to build an HTTP server using `cohttp-lwt-unix` and safely render an HTML template with `Tyxml` in OCaml | ||
--- | ||
(* The server: | ||
- Handles any incoming request on port 8080 | ||
- Responds with a static HTML template that displays "Hello World!" | ||
- Uses Server.respond_string to return the template with headers set as html/text | ||
- Uses Cohttp_lwt_unix for asynchronous operations | ||
- Uses Cohttp for HTTP handling | ||
*) | ||
|
||
open Cohttp | ||
open Cohttp_lwt_unix | ||
open Tyxml.Html | ||
|
||
let html_template = | ||
html | ||
(head (title (txt "Template Page")) [meta ~a:[a_charset "UTF-8"] ()]) | ||
(body [h1 [txt "Hello World!"]]) | ||
|
||
let response_body = Format.asprintf "%a" (pp ()) html_template | ||
|
||
let () = | ||
let callback _conn _req _body = | ||
Server.respond_string ~status:`OK ~body:response_body | ||
~headers:(Header.init_with "Content-Type" "text/html") () | ||
in | ||
let server = Server.make ~callback () in | ||
let port = 8080 in | ||
let mode = `TCP (`Port port) in | ||
Format.printf "listening on http://localhost:%d\n%!" port; | ||
Server.create ~mode server |> Lwt_main.run |
39 changes: 39 additions & 0 deletions
39
data/cookbook/start-a-web-server-html-template/01-dream-server.ml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
packages: | ||
- name: "dream" | ||
tested_version: "1.0.0~alpha8" | ||
used_libraries: | ||
- dream | ||
discussion: | | ||
This example uses Dream, a simple and type-safe web framework for OCaml. | ||
To avoid errors, the file name should end with `.eml.ml`. | ||
Set the dune file following the guide here: | ||
[Dream Template](https://aantron.github.io/dream/#templates) | ||
--- | ||
|
||
(* The server: | ||
- Handles any incoming request on port 8080 | ||
- Logs requests | ||
- Responds with a static HTML template that displays "Hello World!" | ||
- Returns 404 error for other routes *) | ||
|
||
let template = | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Template Page</title> | ||
</head> | ||
<body> | ||
<h1>Hello World!</h1> | ||
</body> | ||
</html> | ||
|
||
let () = | ||
Dream.run | ||
@@ Dream.logger | ||
@@ Dream.router [ | ||
Dream.get "/" (fun _ -> Dream.html template); | ||
Dream.any "/" (fun _ -> Dream.empty `Not_Found); | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave this out as Dream will just handle this automatically.