-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create v1 of handles-server using Go (#13)
Closes #12
- Loading branch information
Showing
21 changed files
with
1,394 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Format | ||
|
||
on: | ||
- push | ||
|
||
jobs: | ||
golangci-lint: | ||
name: Run golangci-lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "go.mod" | ||
- run: go build -v . | ||
- uses: golangci/golangci-lint-action@v3 | ||
tidy: | ||
name: Tidy go modules | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "go.mod" | ||
- uses: katexochen/go-tidy-check@v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: Test | ||
|
||
on: | ||
- push | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: "go.mod" | ||
- name: Build | ||
run: "go build -v ." | ||
- name: Test | ||
run: "go test -v ." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
handles-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Shrink Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Handles Server | ||
|
||
A very simple server that verifies Bluesky (atproto) handles using the | ||
[HTTPS well-known Method][atproto/resolution/well-known]; an alternative to | ||
managing many DNS records. | ||
|
||
## Implementation | ||
|
||
A `handle` is a hostname (e.g: `alice.example.com`) which the server may or may | ||
not be able to provide a Decentralized ID for. A handle is made up of a `domain` | ||
(e.g: `example.com`) and a `username` (e.g: `alice`). A provider | ||
(`ProvidesDecentralizedIDs`) is responsible for getting a Decentralized ID from | ||
a handle. | ||
|
||
## Providers | ||
|
||
- [x] Postgres | ||
- [x] Memory | ||
- [ ] Google Sheets | ||
- [ ] Filesystem | ||
|
||
## Configuration | ||
|
||
| Environment Variable | Description | Example | | ||
| -------------------------- | -------------------------------------------------- | -------------------------------------- | | ||
| **`DID_PROVIDER`** | **Required** Name of a supported provider | `postgres` `memory` | | ||
| `REDIRECT_DID_TEMPLATE` | URL template for redirects when a DID is found | `https://bsky.app/profile/{did}` | | ||
| `REDIRECT_HANDLE_TEMPLATE` | URL template for redirects when a DID is not found | `https://example.com/?handle={handle}` | | ||
|
||
### `memory` provider | ||
|
||
| Environment Variable | Description | Example | | ||
| -------------------- | ----------------------------------------------------- | ------------------------------------------------------------------------- | | ||
| **`MEMORY_DIDS`** | **Required** Comma separated list of handle@did pairs | `alice.example.com@did:plc:example001,bob.example.com@did:plc:example002` | | ||
| **`MEMORY_DOMAINS`** | **Required** Comma separate list of supported domains | `example.com,example.net` | | ||
|
||
### `postgres` provider | ||
|
||
| Environment Variable | Description | Example | | ||
| ------------------------ | -------------------------------------- | -------------------------------------------- | | ||
| **`DATABASE_URL`** | **Required** Postgres database URL | `postgres://postgres@localhost:5432/handles` | | ||
| `DATABASE_TABLE_DIDS` | Table containing `handle` + `did` rows | `dids` `active_handles` | | ||
| `DATABASE_TABLE_DOMAINS` | Table containing `domain` rows | `domains` `active_domains` | | ||
|
||
### URL templates | ||
|
||
A string containing zero or more tokens which are replaced when rendering. | ||
|
||
| Token | Value | Example(s) | | ||
| ------------------- | ----------------------------------------------- | -------------------------- | | ||
| `{handle}` | Formatted handle from the request | `alice.example.com` | | ||
| `{did}` | Decentralized ID found for the request's handle | `did:plc:example001` ` ` | | ||
| `{handle.domain}` | Top level domain from the handle | `example.com` | | ||
| `{handle.username}` | Username part of the handle | `alice` `bob` | | ||
| `{request.scheme}` | Request's scheme | `https` `http` | | ||
| `{request.host}` | Request's host | `alice.example.com` | | ||
| `{request.path}` | Path included in the request | `/hello-world` ` ` | | ||
| `{request.query}` | Query included in the request | `greeting=Hello+World` ` ` | | ||
|
||
[atproto/resolution/well-known]: https://atproto.com/specs/handle#handle-resolution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
version: "3" | ||
|
||
silent: true | ||
|
||
dotenv: | ||
- "Taskfile.env.example" | ||
- "Taskfile.env" | ||
|
||
tasks: | ||
default: | ||
cmds: | ||
- task --list | ||
|
||
run: | ||
desc: "Run local development copy of the server" | ||
cmds: | ||
- go run . | ||
|
||
test: | ||
desc: "Run test suite" | ||
cmds: | ||
- go test . -v | ||
|
||
format: | ||
desc: "Format code" | ||
deps: | ||
- golang-ci | ||
cmds: | ||
- go fmt . | ||
- golangci-lint run | ||
- go mod tidy | ||
|
||
golang-ci: | ||
status: | ||
- golangci-lint version && exit 0 | ||
cmds: | ||
- > | ||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | | ||
sh -s -- -b $(go env GOPATH)/bin v1.53.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
PORT=8888 | ||
LOG_LEVEL=debug | ||
DATABASE_URL="postgres://postgres@localhost:5432/handles" | ||
DID_PROVIDER="postgres" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
"reflect" | ||
|
||
"github.com/caarlos0/env/v11" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
type Config struct { | ||
Host string `env:"HOST" envDefault:"localhost"` | ||
Port string `env:"PORT" envDefault:"80"` | ||
LogLevel slog.Level `env:"LOG_LEVEL" envDefault:"error"` | ||
|
||
RedirectDIDTemplate URLTemplate `env:"REDIRECT_DID_TEMPLATE" envDefault:"https://bsky.app/profile/{did}"` | ||
RedirectHandleTemplate URLTemplate `env:"REDIRECT_HANDLE_TEMPLATE" envDefault:"https://{handle.domain}?handle={handle}"` | ||
|
||
Postgres *pgxpool.Config `env:"DATABASE_URL"` | ||
PostgresDidsTable string `env:"DATABASE_TABLE_DIDS" envDefault:"dids"` | ||
PostgresDomainsTable string `env:"DATABASE_TABLE_DOMAINS" envDefault:"domains"` | ||
|
||
MemoryDids map[string]string `env:"MEMORY_DIDS" envKeyValSeparator:"@"` | ||
MemoryDomains []string `env:"MEMORY_DOMAINS"` | ||
|
||
Provider ProvidesDecentralizedIDs `env:"DID_PROVIDER,required"` | ||
} | ||
|
||
func ConfigFromEnvironment() (Config, error) { | ||
config := Config{} | ||
|
||
err := env.ParseWithOptions(&config, env.Options{ | ||
FuncMap: map[reflect.Type]env.ParserFunc{ | ||
reflect.TypeFor[slog.Level](): func(v string) (interface{}, error) { | ||
var level slog.Level | ||
return level, level.UnmarshalText([]byte(v)) | ||
}, | ||
reflect.TypeFor[pgxpool.Config](): func(v string) (interface{}, error) { | ||
config, err := pgxpool.ParseConfig(v) | ||
return *config, err | ||
}, | ||
reflect.TypeFor[ProvidesDecentralizedIDs](): func(v string) (interface{}, error) { | ||
switch v { | ||
case "postgres": | ||
if config.Postgres == nil { | ||
return &PostgresHandles{}, errors.New("a database connection (`DATABASE_URL`) is required to use the postgres provider") | ||
} | ||
|
||
return NewPostgresHandlesProvider( | ||
config.Postgres, | ||
config.PostgresDidsTable, | ||
config.PostgresDomainsTable, | ||
) | ||
case "memory": | ||
if config.MemoryDids == nil || config.MemoryDomains == nil { | ||
return nil, errors.New("a map of Decentralized IDs (`MEMORY_DIDS`) and domains (`MEMORY_DOMAINS`) is required to use the memory provider") | ||
} | ||
|
||
dids := make(MapOfDids) | ||
|
||
for handle, did := range config.MemoryDids { | ||
dids[Hostname(handle)] = DecentralizedID(did) | ||
} | ||
|
||
domains := make(MapOfDomains) | ||
|
||
for _, domain := range config.MemoryDomains { | ||
domains[Domain(domain)] = true | ||
} | ||
|
||
provider := NewInMemoryProvider(dids, domains) | ||
return provider, nil | ||
default: | ||
return nil, errors.New("no valid provider of decentralized IDs specified") | ||
} | ||
}, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module handles-server | ||
|
||
go 1.23.1 | ||
|
||
require ( | ||
github.com/caarlos0/env/v11 v11.3.1 | ||
github.com/gin-gonic/gin v1.10.0 | ||
github.com/jackc/pgx/v5 v5.7.2 | ||
github.com/samber/slog-gin v1.14.1 | ||
github.com/stretchr/testify v1.10.0 | ||
) | ||
|
||
require ( | ||
github.com/bytedance/sonic v1.11.9 // indirect | ||
github.com/bytedance/sonic/loader v0.1.1 // indirect | ||
github.com/cloudwego/base64x v0.1.4 // indirect | ||
github.com/cloudwego/iasm v0.2.0 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.22.0 // indirect | ||
github.com/goccy/go-json v0.10.3 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||
github.com/jackc/puddle/v2 v2.2.2 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect | ||
github.com/leodido/go-urn v1.4.0 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/rogpeppe/go-internal v1.13.1 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.12 // indirect | ||
go.opentelemetry.io/otel v1.29.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.29.0 // indirect | ||
golang.org/x/arch v0.8.0 // indirect | ||
golang.org/x/crypto v0.31.0 // indirect | ||
golang.org/x/net v0.26.0 // indirect | ||
golang.org/x/sync v0.10.0 // indirect | ||
golang.org/x/sys v0.28.0 // indirect | ||
golang.org/x/text v0.21.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.