Skip to content

Commit

Permalink
Switch from sqlite to postgres for federation sample
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstyle committed Aug 12, 2016
1 parent 3ec661d commit 31f05a7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
6 changes: 4 additions & 2 deletions handlers/federation/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

func TestHandler(t *testing.T) {
db := dbtest.Sqlite().Load(`
CREATE TABLE people (id TEXT, name TEXT, domain TEXT);
db := dbtest.Postgres().Load(`
CREATE TABLE people (id character varying, name character varying, domain character varying);
INSERT INTO people (id, name, domain) VALUES
('GD2GJPL3UOK5LX7TWXOACK2ZPWPFSLBNKL3GTGH6BLBNISK4BGWMFBBG', 'scott', 'stellar.org'),
('GCYMGWPZ6NC2U7SO6SMXOP5ZLXOEC5SYPKITDMVEONLCHFSCCQR2J4S3', 'bartek', 'stellar.org');
Expand All @@ -24,6 +24,8 @@ func TestHandler(t *testing.T) {
LookupReverseRecordQuery: "SELECT name, domain FROM people WHERE id = ?",
}

defer driver.DB.Close()

handler := &Handler{driver}
server := httptest.NewServer(t, handler)
defer server.Close()
Expand Down
29 changes: 3 additions & 26 deletions services/federation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Go implementation of [Federation](https://www.stellar.org/developers/learn/conce

TODO: The section below does not work

[Prebuilt binaries](https://github.com/stellar/federation/releases) of the federation server are available on the [releases page](https://github.com/stellar/federation/releases).
[Prebuilt binaries](https://github.com/stellar/go/releases) of the federation server are available on the [releases page](https://github.com/stellar/go/releases).

| Platform | Binary file name |
|----------------|------------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -86,32 +86,9 @@ reverse-federation = "SELECT username as name, 'acme.org' FROM Users WHERE accou

Notice that SQL fragment `? = 'acme.org"` on the `federation` query: It ensures the incoming query is for the correct domain. Additionally, the `reverse-federation` query always returns `acme.org` for the domain.

## SQLite sample
## Postgresql sample

`federation-sqlite-sample` is a simple SQLite DB file you can use to test federation server quickly. It contains a single `Users` table with following schema and data:

id | name | accountId
--- | --- | ---
1 | bob | GCW667JUHCOP5Y7KY6KGDHNPHFM4CS3FCBQ7QWDUALXTX3PGXLSOEALY
2 | alice | GCVYGVXNRUUOFYB5OKA37UYBF3W7RK7D6JPNV57FZFYAUU5NKJYZMTK2

It should work out of box with following `federation.cfg` file:
```toml
port = 8000

[database]
type = "sqlite3"
url = "./federation-sqlite-sample"

[queries]
federation = "SELECT accountId as id FROM Users WHERE name = ? AND ? = 'stellar.org'"
reverse-federation = "SELECT name, 'stellar.org' FROM Users WHERE accountId = ?"
```

Start the server and then request it:
```
curl "http://localhost:8000/federation?type=name&q=alice*stellar.org"
```
Bundled with the source code of this project is a sample configuration file and a shell script that can be used to populate a sample database. These two items can be used to play around with the service. See (./federation.cfg) and (./build_sample.sh).

## Usage

Expand Down
12 changes: 12 additions & 0 deletions services/federation/build_sample.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /bin/bash

set -e

createdb federation_sample

psql federation_sample -e <<-EOS
CREATE TABLE people (id character varying, name character varying, domain character varying);
INSERT INTO people (id, name, domain) VALUES
('GD2GJPL3UOK5LX7TWXOACK2ZPWPFSLBNKL3GTGH6BLBNISK4BGWMFBBG', 'bob', 'stellar.org'),
('GCYMGWPZ6NC2U7SO6SMXOP5ZLXOEC5SYPKITDMVEONLCHFSCCQR2J4S3', 'alice', 'stellar.org');
EOS
8 changes: 4 additions & 4 deletions services/federation/federation.cfg
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
port = 8000

[database]
type = "sqlite3"
url = "./federation-sqlite-sample"
type = "postgres"
url = "postgres://localhost/federation_sample?sslmode=disable"

[queries]
federation = "SELECT accountId as id FROM Users WHERE name = ? AND ? = 'stellar.org'"
reverse-federation = "SELECT name, 'stellar.org' FROM Users WHERE accountId = ?"
federation = "SELECT id FROM people WHERE name = ? AND domain = ?"
reverse-federation = "SELECT name, domain FROM people WHERE id = ?"
4 changes: 3 additions & 1 deletion services/federation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/stellar/go/handlers/federation"
"github.com/stellar/go/internal/app"
"github.com/stellar/go/internal/config"
"github.com/stellar/go/internal/db"
"github.com/stellar/go/internal/errors"
Expand Down Expand Up @@ -82,7 +83,8 @@ func run(cmd *cobra.Command, args []string) {
ListenAddr: addr,
Handler: mux,
OnStarting: func() {
log.Infof("Starting server on %s", addr)
log.Infof("starting federation server - %s", app.Version())
log.Infof("listening on %s", addr)
},
})
}
Expand Down

0 comments on commit 31f05a7

Please sign in to comment.