Kevin Siegler b85f5330ee
Feat/admin command (#2022)
* WIP: cli admin command

* configure admin command to load ledger admin view

an express service was implemented, which serves the default site from
the instances

test plan: run `sourcecred admin` and browse to the ledger admin
instance to ensure it loads

* implement GET and POST handlers for ledger.json

The GET localhost:6006/data/ledger.json request will return
the ledger.json file if it exists

The POST request will write the submitted json to disk at
./data/ledger.json

test plan: run `$ sourcecred admin` or `$ yarn admin` then
post some json to the path by doing something like
```
$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"derp","password":"slerp"}' \
http://localhost:6006/data/ledger.json
```
and ensure there are no errors.

then
```
$ curl http://localhost:6006/data/ledger.json
```
and ensure the submitted json is returned

* remove unused variables

* Add Ledger to the UI load result

Note: This will fail if there is no ledger file. An empty JSON array is
technically a valid ledger, so if you want to update the example
instance just use:

`echo [] > data/ledger.json`

Test plan: Not yet tested, integrate into frontend

* implement button to save ledger to disk

test-plan: put and empty array ("[]") in the instance's ledger.json
file, then start the admin service "yarn admin" or "sourcecred admin"
then generate some ledger data using the identity interface and ensure
it saves back to disk

* Load ledger from disk for real

* add text middleware to express libdefs

* move ledger disk saving from application/json to text/plain

ledger.json files are encoded in such a way that they are indented and
escaped for some readability from the CLI. using json content-typing
will discard that escaping. By treating the payload as raw text, that
encoding is preserved

test-plan: modify and save a canonically serialized ledger.json file
from the browser frontend and observe that the spacing is preserved in
the file saved to disk

* remove unused deps and improve server console prompt

test-plan: start the admin server and ensure the frontend can create and
modify identities, then save the ledger.json file to disk successfully

* comments and capitalization

test-plan: `yarn flow` should still pass after this change

* remove redundant static middleware

we don't need to configure a static service for a subdirectory of a
statically-served directory

test-plan: ensure ledger.json can still be fetched either via curl or
the web frontend when the admin service is running

* remove address from fetch post request

if we're fetching to/from the parent address that served the javascript
we're calling from, an address is unnecessary

test-plan: make sure updated ledger.json files still POST back to disk

* add instance check

attempt to load the instance configurtion. If the config cannot be
loaded, an error will be throw indicating that the sourcecred.json file
cannot be found

test-plan: run `sourcecred admin` outside of an instance directory and
ensure it fails while looking for sourcecred.json

run `sourcecred admin` in an instance directory and ensure the service
starts up

Co-authored-by: Dandelion Mané <decentralion@dandelion.io>
2020-07-23 11:00:21 -05:00
..
2020-07-23 11:00:21 -05:00