Skip to content

Conversation

@thomashoneyman
Copy link
Member

@thomashoneyman thomashoneyman commented Jan 10, 2023

This PR implements a server for the registry using HTTPure. You can run it with:

spago run -p registry-app -m Registry.App.Server

You can then send requests to localhost:8080/app/v1/<operation> to try out different operations. Be careful! This is going to actually do what you ask for: it will publish, unpublish, or transfer in the production environment.

Here's what you should see on startup:

 ┌───────────────────────────────────────────┐
 │ Server now up on port 8080                │
 │                                           │
 │ To test, run:                             │
 │  > curl -v localhost:8080/api/v1/publish  │
 └───────────────────────────────────────────┘

You can then send requests. For example, to publish a package:

curl --location --request POST 'localhost:8080/api/v1/publish' \
--header 'Content-Type: application/json' \
--data-raw '{
  "compiler": "0.15.6",
  "location": {
    "githubOwner": "purescript",
    "githubRepo": "purescript-prelude"
    },
  "name": "prelude",
  "ref": "v1.0.0"
}'

You should see in the terminal:

[NOTIFY] Package source does not have a purs.json file. Creating one from your bower.json and/or spago.dhall files...
[NOTIFY] Verifying package...
[ERROR] You tried to upload a version that already exists: 1.0.0
Its metadata is:
```json
{
  "bytes": 13945,
  "hash": "sha256-SIdCAn2TXRpxz3dHSLeDTCzbjuMpO7xHsKhkCgRgdrw=",
  "publishedTime": "2016-06-01T00:46:17.000Z",
  "ref": "v1.0.0"
}

and you should receive the error via curl. Same goes for say a bad JSON payload:

curl --location --request POST 'localhost:8080/api/v1/publish' \
--header 'Content-Type: application/json' \
--data-raw '{
  "compiler": "0.15.6",
  "location": {
    "githuOwner": "purescript",
    "githubRepo": "purescript-prelude"
    },
  "name": "prelude",
  "ref": "v1.0.0"
}'

yields the result in the console:

An error occurred while decoding a JSON value:
  Under 'Publish':
  At object key location:
  Under 'GitHub':
  At object key githubOwner:
  No value was found.

Next Steps

This is meant as the first step in implementing a server just demonstrating that we can do it. But we should do more:

  • We should probably implement a dry-run mode for the server so we can run tests against it.
  • We need to specify a machine configuration for NixOS so we can deploy this to Digital Ocean
  • We should probably store logs somewhere better than the local logs directory, and with better names, too.
  • We should probably have requests return an identifier rather than an immediate result, and then allow users to look up the job by that identifier later on (ie. return 12345 if the json is valid, and have a /status/12345 endpoint that has all its logs, and a /poll/12345 that just returns 'pending' or the terminal result). Compiling a package, for example, can take a while and cause the request to drop.

That said, it does work, and we can build from here.

import Node.Path as Path
import Registry.Manifest as Manifest
import Test.Assert as Assert
import Registry.Test.Assert as Assert
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because Test.Assert is already a well-known module, and this rename avoids conflicts.

}

createServerEnv :: Aff ServerEnv
createServerEnv = do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all information we want to persist from request to request.

@f-f
Copy link
Member

f-f commented Jan 11, 2023

This is great! 👏

As I mentioned in #578 (comment) could we switch to httpurple? It uses routing-duplex, which I strongly prefer over the way httpure does it.

@thomashoneyman
Copy link
Member Author

thomashoneyman commented Jan 11, 2023

Sure — in fact, I originally used httpurple, which is why I had to rename the Test.Assert module, but decided against it since none of us have direct maintainer's access and our routing is so simple (we aren't doing much with the extra features). But they are quite similar and I'm cool with either one.

@thomashoneyman
Copy link
Member Author

Just leaving this as a note for myself — npmlock2nix now supports package-lock v2 formats (which we use), and purescript2nix looks like it will support our repository setup with spago.yaml, so I can now nixify the build way more easily.

@sigma-andex
Copy link
Contributor

Sure — in fact, I originally used httpurple, which is why I had to rename the Test.Assert module, but decided against it since none of us have direct maintainer's access and our routing is so simple (we aren't doing much with the extra features). But they are quite similar and I'm cool with either one.

You mean direct access to httpurple? if so, happy to give you access

@f-f f-f merged commit 82f444e into master Mar 13, 2023
@f-f f-f deleted the trh/server branch March 13, 2023 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants