diff --git a/handlers/federation/handler_test.go b/handlers/federation/handler_test.go index c32c3b64d6..e80afe0670 100644 --- a/handlers/federation/handler_test.go +++ b/handlers/federation/handler_test.go @@ -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'); @@ -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() diff --git a/services/federation/README.md b/services/federation/README.md index 3e5d8c415b..03781e4003 100644 --- a/services/federation/README.md +++ b/services/federation/README.md @@ -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 | |----------------|------------------------------------------------------------------------------------------| @@ -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 diff --git a/services/federation/build_sample.sh b/services/federation/build_sample.sh new file mode 100755 index 0000000000..679ec4de74 --- /dev/null +++ b/services/federation/build_sample.sh @@ -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 \ No newline at end of file diff --git a/services/federation/federation.cfg b/services/federation/federation.cfg index abe30f4103..e90ead3afd 100644 --- a/services/federation/federation.cfg +++ b/services/federation/federation.cfg @@ -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 = ?" \ No newline at end of file +federation = "SELECT id FROM people WHERE name = ? AND domain = ?" +reverse-federation = "SELECT name, domain FROM people WHERE id = ?" \ No newline at end of file diff --git a/services/federation/main.go b/services/federation/main.go index 6c22d82049..12c406c593 100644 --- a/services/federation/main.go +++ b/services/federation/main.go @@ -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" @@ -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) }, }) }