2021-11-05 16:55:19 +01:00
|
|
|
let get_title content =
|
|
|
|
|
let open Soup in
|
|
|
|
|
try
|
|
|
|
|
let soup = content |> parse in
|
|
|
|
|
soup $ "h1" |> R.leaf_text
|
|
|
|
|
with
|
|
|
|
|
| Failure _e -> "Permap"
|
|
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let render ?title content request =
|
2021-11-05 16:55:19 +01:00
|
|
|
let title =
|
|
|
|
|
match title with
|
|
|
|
|
| None -> get_title content
|
|
|
|
|
| Some title -> title
|
|
|
|
|
in
|
|
|
|
|
Dream.html
|
|
|
|
|
@@ Template.render_unsafe ~title:(Dream.html_escape title)
|
|
|
|
|
~content:(Dream.html_escape content)
|
2021-11-07 10:21:01 +01:00
|
|
|
request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let render_unsafe ?title content request =
|
2021-11-05 16:55:19 +01:00
|
|
|
let title =
|
|
|
|
|
match title with
|
|
|
|
|
| None -> get_title content
|
|
|
|
|
| Some title -> title
|
|
|
|
|
in
|
2021-11-07 10:21:01 +01:00
|
|
|
Dream.html @@ Template.render_unsafe ~title ~content request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
|
|
|
|
let asset_loader _root path _request =
|
|
|
|
|
match Content.read ("assets/" ^ path) with
|
|
|
|
|
| None -> Dream.empty `Not_Found
|
|
|
|
|
| Some asset -> Dream.respond asset
|
|
|
|
|
|
2021-11-07 10:38:19 +01:00
|
|
|
let page name request =
|
|
|
|
|
match Content.read (name ^ ".md") with
|
2021-11-05 16:55:19 +01:00
|
|
|
| None -> Dream.empty `Not_Found
|
2021-11-07 10:38:19 +01:00
|
|
|
| Some page ->
|
|
|
|
|
let content = Omd.of_string page |> Omd.to_html in
|
|
|
|
|
render_unsafe content request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:38:19 +01:00
|
|
|
let homepage request = page "index" request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let register_get request = render_unsafe (Register.f request) request
|
2021-11-07 01:32:38 +01:00
|
|
|
|
|
|
|
|
let register_post request =
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("email", email); ("nick", nick); ("password", password) ] ->
|
2021-11-07 10:21:01 +01:00
|
|
|
render_unsafe (Register.f ~nick ~email ~password request) request
|
2021-11-08 15:24:10 +01:00
|
|
|
| `Ok _
|
|
|
|
|
| `Many_tokens _
|
|
|
|
|
| `Missing_token _
|
|
|
|
|
| `Invalid_token _
|
|
|
|
|
| `Wrong_session _
|
|
|
|
|
| `Expired _
|
|
|
|
|
| `Wrong_content_type ->
|
|
|
|
|
assert false
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let login_get request = render_unsafe (Login.f request) request
|
2021-11-07 01:32:38 +01:00
|
|
|
|
|
|
|
|
let login_post request =
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("nick", nick); ("password", password) ] ->
|
2021-11-07 10:21:01 +01:00
|
|
|
render_unsafe (Login.f ~nick ~password request) request
|
2021-11-07 01:32:38 +01:00
|
|
|
| _ -> assert false
|
|
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let user request = render_unsafe (User.list ()) request
|
2021-11-07 01:42:18 +01:00
|
|
|
|
2021-11-07 10:32:17 +01:00
|
|
|
let user_profile request = render_unsafe (User.public_profile request) request
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:26:50 +01:00
|
|
|
let logout request =
|
|
|
|
|
let _ = Dream.invalidate_session request in
|
|
|
|
|
let content = "Logged out !" in
|
|
|
|
|
render_unsafe content request
|
|
|
|
|
|
2021-11-08 15:24:10 +01:00
|
|
|
let profile_get request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick ->
|
|
|
|
|
let bio =
|
|
|
|
|
match User.get_bio nick with
|
|
|
|
|
| Ok bio -> bio
|
|
|
|
|
| Error e -> e
|
|
|
|
|
in
|
|
|
|
|
render_unsafe (User_profile.f nick bio request) request
|
2021-11-07 18:52:31 +01:00
|
|
|
|
|
|
|
|
let profile_post request =
|
2021-11-08 15:24:10 +01:00
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("bio", bio) ] ->
|
|
|
|
|
let res =
|
|
|
|
|
match User.update_bio bio nick with
|
|
|
|
|
| Ok () -> "Bio updated!"
|
|
|
|
|
| Error e -> e
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
|
|
|
|
| `Ok _
|
|
|
|
|
| `Many_tokens _
|
|
|
|
|
| `Missing_token _
|
|
|
|
|
| `Invalid_token _
|
|
|
|
|
| `Wrong_session _
|
|
|
|
|
| `Expired _
|
|
|
|
|
| `Wrong_content_type -> (
|
|
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok [ ("files", files) ] ->
|
|
|
|
|
let res =
|
|
|
|
|
match User.upload_avatar files nick with
|
|
|
|
|
| Ok () -> "Avatar was uploaded!"
|
|
|
|
|
| Error e -> e
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
|
|
|
|
| `Ok _ -> Dream.empty `Bad_Request
|
|
|
|
|
| `Expired _
|
|
|
|
|
| `Many_tokens _
|
|
|
|
|
| `Missing_token _
|
|
|
|
|
| `Invalid_token _
|
|
|
|
|
| `Wrong_session _
|
|
|
|
|
| `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request ) )
|
|
|
|
|
|
|
|
|
|
let avatar_image request =
|
|
|
|
|
let nick = Dream.param "user" request in
|
|
|
|
|
let avatar = User.get_avatar nick in
|
|
|
|
|
match avatar with
|
|
|
|
|
| Ok (Some avatar) ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
|
|
|
|
|
| Ok None
|
|
|
|
|
| Error _ -> (
|
|
|
|
|
match Content.read "/assets/img/default_avatar.png" with
|
|
|
|
|
| None -> Dream.empty `Not_Found
|
|
|
|
|
| Some avatar -> Dream.respond ~headers:[ ("Content-Type", "image") ] avatar
|
|
|
|
|
)
|
2021-11-07 10:32:17 +01:00
|
|
|
|
2021-12-07 23:18:07 +01:00
|
|
|
let plant_image request =
|
|
|
|
|
let plant_id = Dream.param "plant_id" request in
|
2021-12-09 01:57:53 +01:00
|
|
|
let nb = int_of_string (Dream.param "nb" request) in
|
2021-12-07 23:18:07 +01:00
|
|
|
let image = User.get_plant_image plant_id nb in
|
|
|
|
|
match image with
|
|
|
|
|
| Ok (Some image) ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "image") ] image
|
|
|
|
|
| Ok None
|
|
|
|
|
| Error _ ->
|
|
|
|
|
Dream.empty `Not_Found
|
|
|
|
|
|
2021-12-06 19:44:27 +01:00
|
|
|
let map request = page "map" request
|
|
|
|
|
|
2021-12-07 23:18:07 +01:00
|
|
|
let add_plant_get request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> render_unsafe (Add_plant.f nick request) request
|
|
|
|
|
|
|
|
|
|
let add_plant_post request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
match%lwt Dream.multipart request with
|
2021-12-09 07:27:44 +01:00
|
|
|
| `Ok [ ("files", files); ("lat_lng", lat_lng); ("tags", tags) ]
|
|
|
|
|
| `Ok (("files", files) :: ("lat_lng", lat_lng) :: ("tags", tags) :: _ :: _)
|
|
|
|
|
-> (
|
2021-12-07 23:18:07 +01:00
|
|
|
match tags with
|
|
|
|
|
| [] -> render_unsafe "Field tag is empty" request
|
2021-12-09 07:27:44 +01:00
|
|
|
| [ (_, tags) ] -> (
|
|
|
|
|
match lat_lng with
|
|
|
|
|
| [] -> render_unsafe "Field tag is empty" request
|
|
|
|
|
| [ (_, lat_lng) ] ->
|
|
|
|
|
let res =
|
|
|
|
|
match User.add_plant lat_lng tags files nick with
|
|
|
|
|
| Ok () -> "Your plant was uploaded!"
|
|
|
|
|
| Error e -> e
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
|
|
|
|
| _lat_lng -> Dream.empty `Bad_Request )
|
2021-12-07 23:18:07 +01:00
|
|
|
| _tags -> Dream.empty `Bad_Request )
|
|
|
|
|
| `Ok _ -> Dream.empty `Bad_Request
|
|
|
|
|
| `Expired _
|
|
|
|
|
| `Many_tokens _
|
|
|
|
|
| `Missing_token _
|
|
|
|
|
| `Invalid_token _
|
|
|
|
|
| `Wrong_session _
|
|
|
|
|
| `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request )
|
|
|
|
|
|
2021-11-05 16:55:19 +01:00
|
|
|
let () =
|
2021-11-07 10:26:50 +01:00
|
|
|
Dream.run @@ Dream.logger @@ Dream.memory_sessions
|
2021-11-05 16:55:19 +01:00
|
|
|
@@ Dream.router
|
|
|
|
|
[ Dream.get "/assets/**" (Dream.static ~loader:asset_loader "")
|
|
|
|
|
; Dream.get "/" homepage
|
2021-11-07 01:32:38 +01:00
|
|
|
; Dream.get "/register" register_get
|
|
|
|
|
; Dream.post "/register" register_post
|
|
|
|
|
; Dream.get "/login" login_get
|
|
|
|
|
; Dream.post "/login" login_post
|
2021-12-06 19:44:27 +01:00
|
|
|
; Dream.get "/map" map
|
2021-11-07 01:32:38 +01:00
|
|
|
; Dream.get "/user" user
|
2021-11-07 01:42:18 +01:00
|
|
|
; Dream.get "/user/:user" user_profile
|
2021-11-08 15:24:10 +01:00
|
|
|
; Dream.get "/user/:user/avatar" avatar_image
|
2021-11-07 10:26:50 +01:00
|
|
|
; Dream.get "/logout" logout
|
2021-11-07 18:52:31 +01:00
|
|
|
; Dream.get "/profile" profile_get
|
|
|
|
|
; Dream.post "/profile" profile_post
|
2021-12-07 23:18:07 +01:00
|
|
|
; Dream.get "/add_plant" add_plant_get
|
|
|
|
|
; Dream.post "/add_plant" add_plant_post
|
|
|
|
|
; Dream.get "/plant_pic/:plant_id/:nb" plant_image
|
2021-11-05 16:55:19 +01:00
|
|
|
]
|
|
|
|
|
@@ Dream.not_found
|