WIP server

This commit is contained in:
Nathan Anderson 2023-08-13 22:44:10 -06:00
commit ed29333a87
17 changed files with 214 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
_build/

1
.ignore Normal file
View File

@ -0,0 +1 @@
_build/

0
.ocamlformat Normal file
View File

4
bin/dune Normal file
View File

@ -0,0 +1,4 @@
(executable
(public_name oasis_server)
(name main)
(libraries oasis_server dream core))

13
bin/main.ml Normal file
View File

@ -0,0 +1,13 @@
let () =
print_endline "Hello, World!";
Dream.run @@ Dream.logger
@@ Dream.router
[
Dream.get "/static/**" @@ Dream.static "static";
Oasis_server.Utils.html_page_route "/";
Dream.get "/echo/:message" (fun request ->
Dream.html (Dream.param request "message"));
Dream.get "/test" (fun _ ->
Unix.sleep 1;
Dream.html "<p>You got me!</p>");
]

BIN
data.db Normal file

Binary file not shown.

26
dune-project Normal file
View File

@ -0,0 +1,26 @@
(lang dune 3.10)
(name oasis_server)
(generate_opam_files true)
(source
(github username/reponame))
(authors "Author Name")
(maintainers "Maintainer Name")
(license LICENSE)
(documentation https://url/to/documentation)
(package
(name oasis_server)
(synopsis "A short synopsis")
(description "A longer description")
(depends ocaml core dune dream lwt lwt_ppx caqti caqti-lwt caqti-driver-sqlite3)
(tags
(topics "to describe" your project)))
; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project

39
html/index.html Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Peimono</title>
</html>
<style>
:root {
--peach: #eca880;
--cherry: #b03c3c;
--slate: #909090;
--dark: #6c6c6c;
--amber: #dcb468;
--lime: #81f499;
--plum: #533a7b;
}
body {
background-color: var(--slate);
}
h1 {
color: var(--peach);
}
</style>
<script src="https://unpkg.com/htmx.org@1.9.4"
integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script>
</head>
<body>
<h1>Hello there!!</h1>
<button hx-get="/test" hx-indicator="#spinner" hx-target="#button_results">Get stuff button</button>
<img id="spinner" class="htmx-indicator" src="static/tail-spin.svg" />
<div id="button_results"></div>
</body>
</html>

17
html/page_template.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</html>
<script src="https://unpkg.com/htmx.org@1.9.4"
integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script>
</head>
<body>
<h1>Hello there!!</h1>
</body>
</html>

31
lib/db.ml Normal file
View File

@ -0,0 +1,31 @@
type user = { id : int; name : string }
let conn_str = "sqlite3:./data.db"
module type DB = Caqti_lwt.CONNECTION
let get_all_users_q = Caqti_request.Infix()
let pool =
let uri = Uri.of_string conn_str in
match Caqti_lwt.connect_pool ~max_size:10 uri with
| Ok pool -> pool
| Error err -> failwith (Caqti_error.show err)
let get_users =
let get_users' (module C: Caqti_lwt.CONNECTION) =
C.fold get_all_users_q
let find_users =
let open Lwt_result.Infix in
fun (module Db : Caqti_lwt.CONNECTION) ->
Db.collect_list find_users_request () >>| List.map make_user
let () =
let uri = Uri.of_string conn_str in
match Lwt_main.run (Caqti_lwt.connect uri >>= find_users) with
| Ok users ->
List.iter (fun user -> Printf.printf "%d: %s\n" user.id user.name) users
| Error err ->
Format.eprintf "%a@." Caqti_error.pp err

3
lib/dune Normal file
View File

@ -0,0 +1,3 @@
(library
(name oasis_server)
(libraries dream lwt_ppx caqti-lwt caqti-driver-sqlite3))

2
lib/math.ml Normal file
View File

@ -0,0 +1,2 @@
let add x y = x + y
let sub x y = x - y

5
lib/utils.ml Normal file
View File

@ -0,0 +1,5 @@
let html_page_route path =
let ic = open_in ("html" ^ path ^ "index.html") in
let lines = In_channel.input_all ic in
In_channel.close ic;
Dream.get path (fun _ -> Dream.html lines)

38
oasis_server.opam Normal file
View File

@ -0,0 +1,38 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "A short synopsis"
description: "A longer description"
maintainer: ["Maintainer Name"]
authors: ["Author Name"]
license: "LICENSE"
tags: ["topics" "to describe" "your" "project"]
homepage: "https://github.com/username/reponame"
doc: "https://url/to/documentation"
bug-reports: "https://github.com/username/reponame/issues"
depends: [
"ocaml"
"core"
"dune" {>= "3.10"}
"dream"
"lwt"
"lwt_ppx"
"caqti"
"caqti-lwt"
"caqti-driver-sqlite3"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/username/reponame.git"

32
static/tail-spin.svg Normal file
View File

@ -0,0 +1,32 @@
<!-- By Sam Herbert (@sherb), for everyone. More @ http://goo.gl/7AJzbL -->
<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient x1="8.042%" y1="0%" x2="65.682%" y2="23.865%" id="a">
<stop stop-color="#fff" stop-opacity="0" offset="0%"/>
<stop stop-color="#fff" stop-opacity=".631" offset="63.146%"/>
<stop stop-color="#fff" offset="100%"/>
</linearGradient>
</defs>
<g fill="none" fill-rule="evenodd">
<g transform="translate(1 1)">
<path d="M36 18c0-9.94-8.06-18-18-18" id="Oval-2" stroke="url(#a)" stroke-width="2">
<animateTransform
attributeName="transform"
type="rotate"
from="0 18 18"
to="360 18 18"
dur="0.9s"
repeatCount="indefinite" />
</path>
<circle fill="#fff" cx="36" cy="18" r="1">
<animateTransform
attributeName="transform"
type="rotate"
from="0 18 18"
to="360 18 18"
dur="0.9s"
repeatCount="indefinite" />
</circle>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

2
test/dune Normal file
View File

@ -0,0 +1,2 @@
(test
(name oasis_server))

0
test/oasis_server.ml Normal file
View File