Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions data/cookbook/start-a-web-server-html-template/00-cohttp-server.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
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
discussion: |
This example shows how to use `cohttp-lwt-unix` to start an HTTP server and render a HTML template 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

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 () =
let callback _conn _req _body =
Server.respond_string ~status:`OK ~body:template
~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 data/cookbook/start-a-web-server-html-template/01-dream-server.ml
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);
]
Loading