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
|
2022-02-18 02:40:04 +01:00
|
|
|
with Failure _e -> "Permap"
|
2021-11-05 16:55:19 +01:00
|
|
|
|
2021-11-07 10:21:01 +01:00
|
|
|
let render ?title content request =
|
2021-11-05 16:55:19 +01:00
|
|
|
let title =
|
2022-02-18 02:40:04 +01:00
|
|
|
match title with None -> get_title content | Some title -> title
|
2021-11-05 16:55:19 +01:00
|
|
|
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 =
|
2022-02-18 02:40:04 +01:00
|
|
|
match title with None -> get_title content | Some title -> title
|
2021-11-05 16:55:19 +01:00
|
|
|
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
|
|
|
|
2022-02-16 17:00:29 +01:00
|
|
|
let about request = page "about" 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
|
2022-02-18 02:40:04 +01:00
|
|
|
| `Ok _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Expired _ | `Wrong_content_type ->
|
2022-02-20 18:02:25 +01:00
|
|
|
Dream.empty `Bad_Request
|
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
|
2022-02-20 18:02:25 +01:00
|
|
|
| `Ok [ ("nick", nick); ("password", password) ] -> (
|
|
|
|
|
match User.login ~nick ~password request with
|
|
|
|
|
| Error e -> render_unsafe e request
|
|
|
|
|
| Ok () ->
|
|
|
|
|
let url =
|
|
|
|
|
match Dream.query request "redirect" with
|
2022-02-20 18:37:24 +01:00
|
|
|
| None -> "/"
|
2022-02-20 18:02:25 +01:00
|
|
|
| Some redirect -> Dream.from_percent_encoded redirect
|
|
|
|
|
in
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", url) ]
|
|
|
|
|
"Logged in: Happy geo-posting!" )
|
|
|
|
|
| `Ok _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Expired _ | `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request
|
2021-11-07 01:32:38 +01:00
|
|
|
|
2022-02-23 22:39:48 +01:00
|
|
|
let admin_get request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None ->
|
|
|
|
|
let redirect_url =
|
|
|
|
|
Format.sprintf "/login?redirect=%s" (Dream.to_percent_encoded "/admin")
|
|
|
|
|
in
|
|
|
|
|
Dream.respond ~status:`See_Other ~headers:[ ("Location", redirect_url) ] ""
|
|
|
|
|
| Some nick ->
|
|
|
|
|
if not (User.is_admin nick) then Dream.respond ~status:`Forbidden ""
|
|
|
|
|
else
|
|
|
|
|
let res =
|
|
|
|
|
match Babillard.get_reports () with
|
|
|
|
|
| Error e -> e
|
|
|
|
|
| Ok (posts, reports) ->
|
|
|
|
|
Pp_babillard.admin_page_content posts reports request
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
|
|
|
|
|
|
|
|
|
let admin_post request =
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
if not (User.is_admin nick) then Dream.respond ~status:`Forbidden ""
|
|
|
|
|
else
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("action", action); ("post_id", id) ] -> (
|
|
|
|
|
let res =
|
|
|
|
|
match Babillard.get_post id with
|
|
|
|
|
| Error _e as e -> e
|
|
|
|
|
| Ok post -> (
|
|
|
|
|
let evil_nick = post.nick in
|
|
|
|
|
match action with
|
|
|
|
|
| "delete" -> Babillard.try_delete_post ~nick id
|
|
|
|
|
| "banish" -> User.banish evil_nick
|
|
|
|
|
| "ignore" -> Babillard.ignore_report id
|
|
|
|
|
| a -> Error (Format.sprintf "invalid action: `%s`" a) )
|
|
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Error e -> render_unsafe e request
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/admin") ]
|
|
|
|
|
"" )
|
|
|
|
|
| `Ok _ | `Expired _ | `Many_tokens _ | `Missing_token _
|
|
|
|
|
| `Invalid_token _ | `Wrong_session _ | `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request )
|
|
|
|
|
|
2022-02-21 00:19:35 +01:00
|
|
|
let catalog request =
|
|
|
|
|
let catalog_content =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.catalog_content ())
|
|
|
|
|
in
|
|
|
|
|
render_unsafe (Catalog_page.f catalog_content) request
|
|
|
|
|
|
2022-02-21 09:46:27 +01:00
|
|
|
let delete_get request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
let post_preview =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.view_post post_id)
|
|
|
|
|
in
|
|
|
|
|
render_unsafe (Delete_page.f post_preview post_id request) request
|
|
|
|
|
|
|
|
|
|
let delete_post request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
2022-02-22 07:10:52 +01:00
|
|
|
(* match on Dream.form needed for hidden csrf field *)
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [] -> (
|
|
|
|
|
match Babillard.try_delete_post ~nick post_id with
|
|
|
|
|
| Error e -> render_unsafe e request
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/") ]
|
|
|
|
|
"Your post was deleted!" )
|
|
|
|
|
| `Ok _ | `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request )
|
|
|
|
|
|
|
|
|
|
let report_get request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
let post_preview =
|
|
|
|
|
Result.fold ~ok:Fun.id ~error:Fun.id (Pp_babillard.view_post post_id)
|
|
|
|
|
in
|
|
|
|
|
render_unsafe (Report_page.f post_preview post_id request) request
|
|
|
|
|
|
|
|
|
|
let report_post request =
|
|
|
|
|
let post_id = Dream.param request "post_id" in
|
|
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
match%lwt Dream.form request with
|
|
|
|
|
| `Ok [ ("reason", reason) ] ->
|
|
|
|
|
let res =
|
|
|
|
|
match Babillard.report ~nick ~reason post_id with
|
|
|
|
|
| Error e -> e
|
|
|
|
|
| Ok () -> "The post was reported!"
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
|
|
|
|
| `Ok _ | `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Wrong_content_type ->
|
|
|
|
|
Dream.empty `Bad_Request )
|
2022-02-21 09:46:27 +01:00
|
|
|
|
2022-02-20 02:50:49 +01:00
|
|
|
let user request =
|
2022-02-18 01:37:25 +01:00
|
|
|
render_unsafe (Result.fold ~ok:Fun.id ~error:Fun.id (User.list ())) request
|
2021-11-07 01:42:18 +01:00
|
|
|
|
2022-01-14 21:36:25 +01:00
|
|
|
let user_profile request =
|
2022-02-19 18:17:03 +01:00
|
|
|
let nick = Dream.param request "user" in
|
2022-02-23 14:30:06 +01:00
|
|
|
if User.exist nick then
|
2022-02-19 18:17:03 +01:00
|
|
|
render_unsafe
|
|
|
|
|
(Result.fold ~ok:Fun.id ~error:Fun.id (User.public_profile nick))
|
|
|
|
|
request
|
2022-02-20 21:36:56 +01:00
|
|
|
else Dream.respond ~status:`Not_Found "User does not exists"
|
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 ->
|
2022-02-23 22:39:48 +01:00
|
|
|
if User.exist nick then
|
|
|
|
|
let bio = match User.get_bio nick with Ok bio -> bio | Error e -> e in
|
|
|
|
|
render_unsafe (User_profile.f nick bio request) request
|
|
|
|
|
else Dream.respond ~status:`Not_Found "User does not exists"
|
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
|
2022-01-14 21:36:25 +01:00
|
|
|
| `Ok [ ("bio", bio) ] -> (
|
|
|
|
|
match User.update_bio bio nick with
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
|
|
|
|
"Your bio was updated!"
|
|
|
|
|
| Error e -> render_unsafe e request )
|
2022-02-18 02:40:04 +01:00
|
|
|
| `Ok _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Expired _ | `Wrong_content_type -> (
|
2021-11-08 15:24:10 +01:00
|
|
|
match%lwt Dream.multipart request with
|
2022-01-14 21:36:25 +01:00
|
|
|
| `Ok [ ("file", file) ] -> (
|
|
|
|
|
match User.upload_avatar file nick with
|
|
|
|
|
| Ok () ->
|
|
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", "/profile") ]
|
|
|
|
|
"Your avatar was updated!"
|
|
|
|
|
| Error e -> render_unsafe e request )
|
2021-11-08 15:24:10 +01:00
|
|
|
| `Ok _ -> Dream.empty `Bad_Request
|
2022-02-18 02:40:04 +01:00
|
|
|
| `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Wrong_content_type ->
|
2021-11-08 15:24:10 +01:00
|
|
|
Dream.empty `Bad_Request ) )
|
|
|
|
|
|
|
|
|
|
let avatar_image request =
|
2022-02-19 21:21:35 +01:00
|
|
|
let nick = Dream.param request "user" in
|
2022-02-23 14:30:06 +01:00
|
|
|
if User.exist nick then
|
2022-02-19 18:17:03 +01:00
|
|
|
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
|
2022-02-20 01:58:33 +01:00
|
|
|
| None -> failwith "can't find default avatar"
|
2022-02-19 18:17:03 +01:00
|
|
|
| Some avatar ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "image") ] avatar )
|
2022-02-20 21:36:56 +01:00
|
|
|
else Dream.respond ~status:`Not_Found "User does not exists"
|
2021-11-07 10:32:17 +01:00
|
|
|
|
2021-12-29 21:07:17 +01:00
|
|
|
let post_image request =
|
2022-02-19 21:21:35 +01:00
|
|
|
let post_id = Dream.param request "post_id" in
|
2022-02-23 14:30:06 +01:00
|
|
|
if Babillard.post_exist post_id then
|
2022-02-19 18:17:03 +01:00
|
|
|
let image = Babillard.get_post_image_content post_id in
|
|
|
|
|
match image with
|
|
|
|
|
| Ok image -> Dream.respond ~headers:[ ("Content-Type", "image") ] image
|
|
|
|
|
| Error _ -> Dream.empty `Not_Found
|
2022-02-20 21:36:56 +01:00
|
|
|
else Dream.respond ~status:`Not_Found "Image does not exists"
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-17 03:59:23 +01:00
|
|
|
let markers request =
|
|
|
|
|
let markers = Pp_babillard.get_markers () in
|
2022-01-14 13:23:45 +01:00
|
|
|
match markers with
|
|
|
|
|
| Ok markers ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "application/json") ] markers
|
2021-12-29 21:07:17 +01:00
|
|
|
| Error e -> render_unsafe e request
|
|
|
|
|
|
2022-02-17 03:59:23 +01:00
|
|
|
let babillard_get request = render_unsafe (Babillard_page.f request) request
|
2021-12-29 21:07:17 +01:00
|
|
|
|
2022-02-22 06:07:29 +01:00
|
|
|
let babillard_post request =
|
2021-12-29 21:07:17 +01:00
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok
|
2022-01-25 14:07:28 +01:00
|
|
|
[ ("alt", [ (_, alt) ])
|
|
|
|
|
; ("file", file)
|
2022-02-18 20:17:24 +01:00
|
|
|
; ("lat-input", [ (_, lat) ])
|
|
|
|
|
; ("lng-input", [ (_, lng) ])
|
2021-12-29 21:07:17 +01:00
|
|
|
; ("subject", [ (_, subject) ])
|
|
|
|
|
; ("tags", [ (_, tags) ])
|
2022-02-18 20:17:24 +01:00
|
|
|
; ("thread-comment", [ (_, comment) ])
|
2022-01-29 10:11:36 +01:00
|
|
|
] -> (
|
2021-12-29 21:07:17 +01:00
|
|
|
match (Float.of_string_opt lat, Float.of_string_opt lng) with
|
|
|
|
|
| None, _ -> render_unsafe "Invalide coordinate" request
|
|
|
|
|
| _, None -> render_unsafe "Invalide coordinate" request
|
|
|
|
|
| Some lat, Some lng -> (
|
2022-01-29 10:21:53 +01:00
|
|
|
let res =
|
|
|
|
|
match file with
|
2022-02-17 03:59:23 +01:00
|
|
|
| [] -> Babillard.make_op ~comment ~lat ~lng ~subject ~tags nick
|
2022-01-29 10:21:53 +01:00
|
|
|
| _ :: _ :: _ -> Error "More than one image"
|
|
|
|
|
| [ (image_name, image_content) ] ->
|
2022-02-18 19:40:19 +01:00
|
|
|
let image = ((image_name, alt), image_content) in
|
2022-02-17 03:59:23 +01:00
|
|
|
Babillard.make_op ~comment ~image ~lat ~lng ~subject ~tags nick
|
2022-01-29 10:21:53 +01:00
|
|
|
in
|
|
|
|
|
match res with
|
|
|
|
|
| Ok thread_id ->
|
2022-02-19 17:46:01 +01:00
|
|
|
let adress = Format.asprintf "/thread/%s" thread_id in
|
2022-01-29 10:21:53 +01:00
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", adress) ]
|
|
|
|
|
"Your thread was posted!"
|
|
|
|
|
| Error e -> render_unsafe e request ) )
|
2021-12-29 21:07:17 +01:00
|
|
|
| `Ok _ -> Dream.empty `Bad_Request
|
2022-02-18 02:40:04 +01:00
|
|
|
| `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Wrong_content_type ->
|
2021-12-29 21:07:17 +01:00
|
|
|
Dream.empty `Bad_Request )
|
|
|
|
|
|
2022-02-27 13:45:43 +01:00
|
|
|
let thread_feed_get request =
|
|
|
|
|
let thread_id = Dream.param request "thread_id" in
|
|
|
|
|
if Babillard.thread_exist thread_id then
|
2022-02-27 19:58:32 +01:00
|
|
|
match Pp_babillard.feed thread_id with
|
2022-02-27 13:45:43 +01:00
|
|
|
| Error e -> render_unsafe e request
|
|
|
|
|
| Ok feed ->
|
|
|
|
|
Dream.respond ~headers:[ ("Content-Type", "application/atom+xml") ] feed
|
|
|
|
|
else Dream.respond ~status:`Not_Found "Thread not found"
|
|
|
|
|
|
2021-12-29 21:07:17 +01:00
|
|
|
let thread_get request =
|
2022-02-19 21:21:35 +01:00
|
|
|
let thread_id = Dream.param request "thread_id" in
|
2022-02-23 14:30:06 +01:00
|
|
|
if Babillard.thread_exist thread_id then
|
2022-02-19 18:17:03 +01:00
|
|
|
let thread_view = Pp_babillard.view_thread thread_id in
|
2022-02-20 01:58:33 +01:00
|
|
|
let res =
|
|
|
|
|
match thread_view with
|
|
|
|
|
| Error e -> e
|
|
|
|
|
| Ok thread_view -> Thread_page.f thread_view thread_id request
|
|
|
|
|
in
|
|
|
|
|
render_unsafe res request
|
2022-02-20 21:36:56 +01:00
|
|
|
else Dream.respond ~status:`Not_Found "Thread not found"
|
2021-12-29 21:07:17 +01:00
|
|
|
|
|
|
|
|
(*form to reply to a thread *)
|
2022-01-12 19:34:55 +01:00
|
|
|
let reply_post request =
|
2021-12-29 21:07:17 +01:00
|
|
|
match Dream.session "nick" request with
|
|
|
|
|
| None -> render_unsafe "Not logged in" request
|
|
|
|
|
| Some nick -> (
|
|
|
|
|
match%lwt Dream.multipart request with
|
|
|
|
|
| `Ok
|
2022-01-25 14:07:28 +01:00
|
|
|
[ ("alt", [ (_, alt) ])
|
|
|
|
|
; ("file", file)
|
2022-02-18 20:17:24 +01:00
|
|
|
; ("reply-comment", [ (_, comment) ])
|
2021-12-29 21:07:17 +01:00
|
|
|
; ("tags", [ (_, tags) ])
|
2022-01-29 10:11:36 +01:00
|
|
|
] -> (
|
2022-02-19 21:21:35 +01:00
|
|
|
let parent_id = Dream.param request "thread_id" in
|
2021-12-29 21:07:17 +01:00
|
|
|
let res =
|
|
|
|
|
match file with
|
2022-01-11 15:01:59 +01:00
|
|
|
| [] -> Babillard.make_reply ~comment ~tags ~parent_id nick
|
2022-01-25 14:07:28 +01:00
|
|
|
| [ (image_name, image_content) ] ->
|
2022-02-18 19:40:19 +01:00
|
|
|
let image = ((image_name, alt), image_content) in
|
2022-01-25 14:07:28 +01:00
|
|
|
Babillard.make_reply ~comment ~image ~tags ~parent_id nick
|
2021-12-29 21:07:17 +01:00
|
|
|
| _ :: _ :: _ -> Error "More than one image"
|
|
|
|
|
in
|
2022-01-10 18:39:29 +01:00
|
|
|
match res with
|
|
|
|
|
| Ok post_id ->
|
2022-02-19 17:46:01 +01:00
|
|
|
let adress = Format.sprintf "/thread/%s#%s" parent_id post_id in
|
2022-01-17 21:55:09 +01:00
|
|
|
Dream.respond ~status:`See_Other
|
|
|
|
|
~headers:[ ("Location", adress) ]
|
2022-01-14 21:36:25 +01:00
|
|
|
"Your reply was posted!"
|
2022-01-10 18:39:29 +01:00
|
|
|
| Error e -> render_unsafe e request )
|
2021-12-29 21:07:17 +01:00
|
|
|
| `Ok _ -> Dream.empty `Bad_Request
|
2022-02-18 02:40:04 +01:00
|
|
|
| `Expired _ | `Many_tokens _ | `Missing_token _ | `Invalid_token _
|
|
|
|
|
| `Wrong_session _ | `Wrong_content_type ->
|
2021-12-29 21:07:17 +01:00
|
|
|
Dream.empty `Bad_Request )
|
|
|
|
|
|
2022-02-20 21:36:56 +01:00
|
|
|
let error_template _error _debug_info response =
|
|
|
|
|
let status = Dream.status response in
|
|
|
|
|
let code = Dream.status_to_int status in
|
|
|
|
|
|
|
|
|
|
(*TODO improve: can't use template.elm.html because it needs "request" *)
|
|
|
|
|
let%lwt body = Dream.body response in
|
|
|
|
|
let reason =
|
|
|
|
|
if String.equal "" body then Dream.status_to_string status else body
|
|
|
|
|
in
|
|
|
|
|
Dream.set_body response (Format.sprintf "%d: %s" code reason);
|
|
|
|
|
Lwt.return response
|
|
|
|
|
|
2022-02-19 22:13:45 +01:00
|
|
|
let routes =
|
|
|
|
|
(* this is just so that they're visually aligned *)
|
|
|
|
|
let get_ = Dream.get in
|
|
|
|
|
let post = Dream.post in
|
|
|
|
|
|
|
|
|
|
[ get_ "/" babillard_get
|
2022-02-22 06:07:29 +01:00
|
|
|
; post "/" babillard_post
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/about" about
|
2022-02-23 22:39:48 +01:00
|
|
|
; get_ "/admin" admin_get
|
|
|
|
|
; post "/admin" admin_post
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/assets/**" (Dream.static ~loader:asset_loader "")
|
2022-02-22 00:01:35 +01:00
|
|
|
; get_ "/catalog" catalog
|
|
|
|
|
; get_ "/delete/:post_id" delete_get
|
|
|
|
|
; post "/delete/:post_id" delete_post
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/img/:post_id" post_image
|
|
|
|
|
; get_ "/login" login_get
|
|
|
|
|
; post "/login" login_post
|
|
|
|
|
; get_ "/logout" logout
|
|
|
|
|
; get_ "/markers" markers
|
|
|
|
|
; get_ "/post_pic/:post_id" post_image
|
|
|
|
|
; get_ "/profile" profile_get
|
|
|
|
|
; post "/profile" profile_post
|
2022-02-22 07:10:52 +01:00
|
|
|
; get_ "/report/:post_id" report_get
|
|
|
|
|
; post "/report/:post_id" report_post
|
2022-02-22 00:01:35 +01:00
|
|
|
; get_ "/thread/:thread_id" thread_get
|
|
|
|
|
; post "/thread/:thread_id" reply_post
|
2022-02-27 13:45:43 +01:00
|
|
|
; get_ "/thread/:thread_id/feed" thread_feed_get
|
2022-02-20 02:50:49 +01:00
|
|
|
; get_ "/user" user
|
2022-02-19 22:13:45 +01:00
|
|
|
; get_ "/user/:user" user_profile
|
|
|
|
|
; get_ "/user/:user/avatar" avatar_image
|
|
|
|
|
]
|
2022-02-19 17:46:01 +01:00
|
|
|
@
|
|
|
|
|
if App.open_registration then
|
|
|
|
|
[ get_ "/register" register_get; post "/register" register_post ]
|
|
|
|
|
else []
|
|
|
|
|
|
2021-11-05 16:55:19 +01:00
|
|
|
let () =
|
2022-02-19 22:29:17 +01:00
|
|
|
let logger = if App.log then Dream.logger else Fun.id in
|
2022-02-20 21:36:56 +01:00
|
|
|
Dream.run ~port:App.port ~error_handler:(Dream.error_template error_template)
|
|
|
|
|
@@ logger @@ Dream.cookie_sessions
|
2022-02-18 03:24:23 +01:00
|
|
|
(* this should replace memory/cookie sessions but it doesn't work :-(
|
|
|
|
|
@@ Dream.sql_pool Db.db_uri
|
|
|
|
|
@@ Dream.sql_sessions
|
|
|
|
|
*)
|
2022-02-19 22:13:45 +01:00
|
|
|
@@ Dream.router routes
|