From 69d3f53365568524e18dfb1200a386309e174359 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Sat, 2 Dec 2023 09:49:42 -0600 Subject: Initial commit --- bin/main.ml | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 bin/main.ml (limited to 'bin/main.ml') diff --git a/bin/main.ml b/bin/main.ml new file mode 100644 index 0000000..dcd24aa --- /dev/null +++ b/bin/main.ml @@ -0,0 +1,93 @@ +open Wormhole + +let (fake_post : Post.t) = + { + link = "https://mccd.space"; + summary = "My personal blog"; + tags = [ "cool"; "article" ]; + published = "2020-01-01T00:00:00Z"; + author = "Marc"; + } + +let (fake_post_2 : Post.t) = + { + link = "https://google.com"; + summary = "Some other cool article that I just made"; + tags = [ "cool"; "something" ]; + published = "2020-01-02T00:00:00Z"; + author = "Bob"; + } + +let webfinger = + {| +{ + "subject": "acct:blackhole@galaxy.mccd.space", + + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": "https://galaxy.mccd.space/actor" + } + ] +} +|} + +let actor = + {| +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1" + ], + + "id": "https://galaxy.mccd.space/actor", + "type": "Person", + "preferredUsername": "blackhole", + "inbox": "https://galaxy.mccd.space/inbox", + + "publicKey": { + "id": "https://galaxy.mccd.space/actor#main-key", + "owner": "https://galaxy.mccd.space/actor", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvqa9W2PjNYB6FDuRewSR\nAUyH2TK5iprbfuKWvCGEYq2FRccjmoluMEb0a16AyqqMeZ8J+pI7cPvqpdXm/VVV\niZx3Q5W4H8kIC3I84qAAzVs3wOQWnudk+D+hEE1Il9+yFBFvF7IoyER9axqJEb88\nYQ5okLU/346SMpMrk4wsUFnwaxdVXQPBQ0tVxqVJiLGSMlGXX/1Vl0+lnhgg+5rH\n8rfIbFX4qQC/gYbEU+VS2nzhYjdqn0maL94OrFHYNdrMBgSBOtbBFSJ+kMgojqES\n7Xhf+G9JcuCIqm0T3dHqo50MUSx8lrS78S3uO7WgPAby8qXjcL8sQEfNvJT16sjk\ndwIDAQAB\n-----END PUBLIC KEY-----" + } +} + |} + +let () = + Post.add fake_post; + Post.add fake_post_2; + let port = + Sys.getenv_opt "PORT" |> Option.map int_of_string + |> Option.value ~default:8080 + in + let env = Sys.getenv_opt "ENV" |> Option.value ~default:"PROD" in + let interface = if env = "DEV" then "localhost" else "0.0.0.0" in + Dream.run ~port ~interface @@ Dream.logger + @@ Dream.router + [ + Dream.get "/actor" (fun _ -> + Dream.log "Sending actor"; + Dream.respond actor + ~headers:[ ("Content-Type", "application/json+ld") ]); + Dream.get "/.well-known/webfinger" (fun _ -> + Dream.log "Sending webfinger"; + Dream.respond webfinger + ~headers:[ ("Content-Type", "application/json+ld") ]); + Dream.get "/" (fun request -> + Dream.log "Sending greeting to %s!" (Dream.client request); + let posts = Post.get_all () in + Template.render posts |> Dream.html); + Dream.post "/clicked" (fun _ -> "

You clicked

" |> Dream.html); + Dream.post "/login" (fun _ -> Login.render |> Dream.html); + Dream.post "/inbox" (fun request -> + let%lwt body = Dream.body request in + Dream.log "Got body: %s" body; + let message_object = + Yojson.Safe.from_string body |> Post.mastodon_post_of_yojson + in + message_object |> Post.yojson_of_mastodon_post + |> Yojson.Safe.to_string |> Dream.log "Added post %s"; + message_object |> Post.post_of_mastodon_post |> Post.add; + Dream.json "{}"); + ] -- cgit v1.2.3