Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up server using express #21

Merged
merged 3 commits into from
Jul 15, 2020
Merged

Set up server using express #21

merged 3 commits into from
Jul 15, 2020

Conversation

joachimvh
Copy link
Member

This allows setting up the (very basic) Solid server using node bin/server.js (with optionally -p port). Until there is dependency injection this class also handles the linking of all the classes.

I looked into Koa and the implementation would pretty much be 100% the same due to so little express code being used. Since there already was a dependency on express through another dependency I went for that one.

Some changes from the architecture:

  • The constructor takes a single handler instead of an array. Currently didn't see the point of an array if we have CompositeAsyncHandler.
  • The listen function returns an http.Server, makes testing much easier and makes it more similar to the actual listen interface of http.Server.

Currently CORS is handled in the new server class. An OptionOperationHandler might be more in line with the rest of the architecture, but currently there is not really a clean way for such a handler to add headers to the output. I do think we will need this at some point so potentially this can be changed then.

I looked into making the server extend http.Server to be consistent with that interface, but then many more overloads for listen are necessary, and making everything work with express is going to be much more of a hassle since there you only get a Server object when you call the listen function.

This PR also fixed the identifier extraction and a bug in the SimpleResourceStore.

@joachimvh joachimvh added 🐛 bug Something isn't working ☀️ enhancement New feature or request labels Jul 10, 2020
bin/server.ts Outdated
import { SimpleRequestParser } from '../src/ldp/http/SimpleRequestParser';
import { SimpleResourceStore } from '../src/storage/SimpleResourceStore';
import { SimpleResponseWriter } from '../src/ldp/http/SimpleResponseWriter';
import { SimpleTargetExtractor } from '../src/ldp/http/SimpleTargetExtractor';
Copy link
Member

Choose a reason for hiding this comment

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

We probably want a /src/index.ts that exports them all, and then import them from there.

export { SimpleResponseWriter } from '../src/ldp/http/SimpleResponseWriter'; etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Will also need this for Components.js.

bin/server.ts Outdated

let port = 3000;

if (process.argv.length >= 4 && process.argv[2] === '-p') {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea I didn't bother since this file should be very temporary, but it's true that such things have the tendency to then stay where they are

Copy link
Member

Choose a reason for hiding this comment

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

Just indicate it as such then?

bin/server.ts Outdated
const authorizer = new SimpleAuthorizer();

// Will have to see how to best handle this
const store = new SimpleResourceStore('http://localhost:3000/');
Copy link
Member

Choose a reason for hiding this comment

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

Probably want to inject port in there too?

bin/server.ts Outdated
httpServer.listen(port);

// eslint-disable-next-line no-console
console.log(`Running on port ${port}.`);
Copy link
Member

Choose a reason for hiding this comment

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

or process.stdout.print (don't forget to add newline)

Copy link
Member

Choose a reason for hiding this comment

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

And actually, log the full URL (so it's clickable etc)

package.json Outdated
@@ -2,12 +2,13 @@
"name": "@solid/community-server",
"private": true,
"version": "0.0.1",
"main": "index.js",
"main": "bin/server.js",
Copy link
Member

Choose a reason for hiding this comment

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

Oh no, this should the bin property. main is the code entry point.

Copy link
Contributor

Choose a reason for hiding this comment

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

That reminds me that we also need a typings entry, like here: https://github.com/rubensworks/jsonld-context-parser.js/blob/master/package.json#L14

Copy link
Member Author

Choose a reason for hiding this comment

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

Apparently the typings field is ignored if you also have a files field.

Also note that if your package.json includes the "files" property the "types" property will be ignored. In that case you need to pass your main declaration file to the "files" property as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

TIL :-)

src/storage/SimpleResourceStore.ts Show resolved Hide resolved

class SimpleHttpHandler extends HttpHandler {
public async canHandle(): Promise<void> {
return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

I've seen this a couple of times now, and always do a double-take.
Should we make this a Promise<boolean> (but still make it the contract that it's either true or an error)?
Or alternatively, rename this to ensureSupported or whatever?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have no strong opinions on this so can go any way for me

Copy link
Member

Choose a reason for hiding this comment

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

Is just return possible actually? It's the undefined that makes me think.

Copy link
Member Author

Choose a reason for hiding this comment

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

The linter then complains about having an unnecessary return statement. And if you completely remove it, it complains about having an empty function. :D

Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest boolean then; can be a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

For completeness, the previous discussion where this changed to void was here

test/unit/server/ExpressHttpServer.test.ts Show resolved Hide resolved
@joachimvh joachimvh mentioned this pull request Jul 13, 2020
Copy link
Contributor

@rubensworks rubensworks left a comment

Choose a reason for hiding this comment

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

Nothing major to add next to @RubenVerborgh's review.

bin/server.ts Outdated
import { SimpleRequestParser } from '../src/ldp/http/SimpleRequestParser';
import { SimpleResourceStore } from '../src/storage/SimpleResourceStore';
import { SimpleResponseWriter } from '../src/ldp/http/SimpleResponseWriter';
import { SimpleTargetExtractor } from '../src/ldp/http/SimpleTargetExtractor';
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Will also need this for Components.js.

package.json Outdated
@@ -2,12 +2,13 @@
"name": "@solid/community-server",
"private": true,
"version": "0.0.1",
"main": "index.js",
"main": "bin/server.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

That reminds me that we also need a typings entry, like here: https://github.com/rubensworks/jsonld-context-parser.js/blob/master/package.json#L14

@joachimvh
Copy link
Member Author

Latest push should fix most issues (except handle function returning a boolean, that will be for later).

I used the cors package now, but the allow-origin header still wasn't correct out of the box. By default it would always return * so I still had to add a line for that here.

@joachimvh joachimvh merged commit a9dc59b into master Jul 15, 2020
@joachimvh joachimvh deleted the express branch July 15, 2020 08:59
joachimvh pushed a commit that referenced this pull request Sep 14, 2020
Add changes of resource mapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working ☀️ enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants