Add initial framework and serve webfinger requests

parents
root = true
[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
/docs/
/lib/
/bin/
/.shards/
*.dwarf
This diff is collapsed.
# pub-relay
TODO: Write a description here
## Installation
TODO: Write installation instructions here
## Usage
TODO: Write usage instructions here
## Development
TODO: Write development instructions here
## Contributing
1. Fork it (<https://github.com/your-github-user/pub-relay/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
## Contributors
- [your-github-user](https://github.com/your-github-user) RX14 - creator, maintainer
name: pub-relay
version: 0.1.0
authors:
- RX14 <chris@rx14.co.uk>
targets:
pub-relay:
main: src/entrypoint.cr
crystal: 0.25.1
license: AGPL3
require "./spec_helper"
describe PubRelay do
describe "webfinger" do
it "works" do
status_code, body = request("GET", "/.well-known/webfinger?resource=acct%3Arelay%40example.com")
status_code.should eq(200)
body.should eq(<<-HERE)
{"subject":"acct:relay@example.com","links":[{"rel":"self","type":"application/activity+json","href":"https://example.com/actor"}]}
HERE
end
it "fails with resource parameter missing" do
status_code, body = request("GET", "/.well-known/webfinger?resourc=misspelling")
status_code.should eq(400)
body.should contain("Resource query parameter not present")
end
it "handles not found resource parameter" do
status_code, body = request("GET", "/.well-known/webfinger?resource=notrelay@example.com")
status_code.should eq(404)
body.should contain("Resource not found")
end
end
end
require "spec"
require "../src/pub_relay"
def request(method, resource, headers = nil, body = nil)
request = HTTP::Request.new(method, resource, headers, body)
response = HTTP::Server::Response.new(IO::Memory.new)
response_body = IO::Memory.new
response.output = response_body
context = HTTP::Server::Context.new(request, response)
PubRelay.new("example.com").call(context)
{response.status_code, response_body.to_s, response.headers}
end
require "./pub_relay"
handlers = [] of HTTP::Handler
handlers << HTTP::LogHandler.new if ENV["RELAY_DEBUG"]?
handlers << PubRelay.new(ENV["RELAY_DOMAIN"])
server = HTTP::Server.new(handlers)
bind_ip = server.bind_tcp(
host: ENV["RELAY_HOST"]? || "localhost",
port: (ENV["RELAY_PORT"]? || 8085).to_i,
reuse_port: !!ENV["RELAY_REUSEPORT"]?
)
puts "Listening on #{bind_ip}"
server.listen
require "http"
require "json"
class PubRelay
VERSION = "0.1.0"
include HTTP::Handler
def initialize(@host : String)
@account_uri = "acct:relay@#{@host}"
end
def call(context : HTTP::Server::Context)
case {context.request.method, context.request.path}
when {"GET", "/.well-known/webfinger"}
serve_webfinger(context)
# when {"GET", "/actor"}
# serve_actor(context)
# when {"POST", "/inbox"}
# handle_inbox(context)
end
end
private def serve_webfinger(ctx)
resource = ctx.request.query_params["resource"]?
return error(ctx, 400, "Resource query parameter not present") unless resource
return error(ctx, 404, "Resource not found") unless resource == @account_uri
{
subject: @account_uri,
links: {
{
rel: "self",
type: "application/activity+json",
href: "https://#{@host}/actor",
},
},
}.to_json(ctx.response)
end
private def error(context, status_code, message)
context.response.status_code = status_code
context.response.puts message
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment