Skip to content

Begin refactoring module layout #1068

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

Merged
merged 5 commits into from
Sep 27, 2017

Conversation

jtgeibel
Copy link
Member

This PR begins some work towards refactoring the module layout of our rust code. This PR includes several initial changes:

  • Group route definitions in lib.rs and document where those api endpoints are currently used.
  • Move the krate and version api modules into subdirectories. This will be explained further below.
  • Move categories.rs and categories.toml under a new boot module as these are only used during boot of the server process.
  • Pull github related functionality out of http into a separate github module, leaving the http security middleware behind.

The second bullet point sets the groundwork for changes similar to what I prototyped in this branch. I'm hoping that by moving and renaming these to mod.rs now, without actually changing the files yet, will cut down on merge conflicts with any future PRs.

Proposed further changes

As proposed in the previous branch, I believe the next step would be to split the krate and version modules into submodules which support cargo and frontend functionality. At the moment, the api/v1/crates route is the only one used by both. My main motivation for this is that for cargo routes we need to strictly maintain backwards compatibility. For instance, as noted in #712, we always return a 200 response code with an errors array because this is expected by cargo (with the exception of krate::download where it does not). If we ever decide to change this then we will need to take care that we don't impact routes used by cargo. (Of course we need to be careful in general, as we've broken unknown external users in the past, but hopefully this will provide an extra speed bump when authoring and reviewing changes.)

While experimenting on that branch I also got a better feel for how we could split logic between the api/controller level and the model/view level. My notes on this may be a bit dated, but in some modules we mix api functionality with diesel and serde structs. For instance, the following files contain logic for all these concerns:

  • keyword.rs
  • krate/mod.rs
  • token.rs
  • user/mod.rs
  • version/mod.rs

In contrast, the following files contain only diesel and serde functionality, in support of api calls handled by the files above:

  • category.rs
  • dependency.rs
  • download.rs
  • owner.rs

It would be nice if we could pull out the database and json related functionality in the first list, and then group all of those files in a new submodule. At this time, I do not think it is necessary to try to split apart the model and view functionality as this logic is very straightforward.

For now, I hope this is a small enough PR to review independently and provide a place to discuss these suggestions for further refactoring.

/cc @vignesh-sankaran

@vignesh-sankaran
Copy link
Contributor

Thanks @jtgeibel, I’ll try taking a look at this today or tomorrow, I would like to try reviewing this @carols10cents :)

@vignesh-sankaran
Copy link
Contributor

Ok so from an initial look, I like how the endpoints are now organised by what external user utilisees them.

I'm a bit worried that the /crates endpoint is a singular API for both cargo and the frontend. I think we should look into how cargo search and the front end use the /crates endpoint for search since if they don't use all of the returned data or use the data differently, we could split up the functionality accordingly so that we don't accidentally break things if we change this endpoint's return data.

I'll have more feedback tomorrow regarding the rest of the changes and suggestions for future refactoring. Once we've decided on an approach, I think it'd be good to update #912 with a list of tasks so that we can make it E-mentor as well.

Copy link
Contributor

@vignesh-sankaran vignesh-sankaran left a comment

Choose a reason for hiding this comment

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

Could you add a module level doc comment in github.rs?

@@ -0,0 +1,93 @@
use curl;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a doc comment here to give a high level overview of what this module does and where it's used?

@@ -186,9 +186,9 @@ impl Team {
// FIXME: we just set per_page=100 and don't bother chasing pagination
Copy link
Contributor

@vignesh-sankaran vignesh-sankaran Sep 26, 2017

Choose a reason for hiding this comment

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

So it turns out that the only thing using this function is on line 138 in pub fn create(), and that's within this module itself. While you're modifying this function could you remove the pub modifier?

@jtgeibel jtgeibel force-pushed the begin-module-refactor branch from 386c7b3 to b28d23f Compare September 27, 2017 02:22
@jtgeibel
Copy link
Member Author

I've addressed review comments and have rebased on master with the latest rustfmt updates.

src/github.rs Outdated
@@ -1,3 +1,5 @@
//! This module implements functionality for interacting with github.
Copy link
Contributor

Choose a reason for hiding this comment

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

*GitHub, not github :)

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'd hate to burn CI time on this. @carols10cents do you know if the issue with [no-ci] on bors-voyager has been fixed? I know last time I used that it froze something in the pipeline and you had to override it.

@jtgeibel jtgeibel force-pushed the begin-module-refactor branch from b28d23f to 7e88ba5 Compare September 27, 2017 22:42
@jtgeibel
Copy link
Member Author

I amended the last commit and used [skip ci]. Other than the change to the doc comment this group of commits passed on travis at https://travis-ci.org/rust-lang/crates.io/builds/280238869.

@carols10cents
Copy link
Member

LGTM! let's get this to stop having merge conflicts quick!

bors: r+

bors-voyager bot added a commit that referenced this pull request Sep 27, 2017
1068: Begin refactoring module layout r=carols10cents

This PR begins some work towards refactoring the module layout of our rust code.  This PR includes several initial changes:

* Group route definitions in `lib.rs` and document where those api endpoints are currently used.
* Move the `krate` and `version` api modules into subdirectories.  This will be explained further below.
* Move `categories.rs` and `categories.toml` under a new `boot` module as these are only used during boot of the server process.
* Pull github related functionality out of `http` into a separate `github` module, leaving the http security middleware behind.

The second bullet point sets the groundwork for changes similar to what I prototyped in [this branch](3f67a0e...jtgeibel:refactor-api-into-submodules).  I'm hoping that by moving and renaming these to `mod.rs` now, without actually changing the files yet, will cut down on merge conflicts with any future PRs.

# Proposed further changes

As proposed in the previous branch, I believe the next step would be to split the `krate` and `version` modules into submodules which support `cargo` and `frontend` functionality.  At the moment, the `api/v1/crates` route is the only one used by both.  My main motivation for this is that for `cargo` routes we need to strictly maintain backwards compatibility.  For instance, as noted in #712, we always return a `200` response code with an `errors` array because this is expected by `cargo` (with the exception of  `krate::download` where it does not).  If we ever decide to change this then we will need to take care that we don't impact routes used by cargo.  (Of course we need to be careful in general, as we've broken unknown external users in the past, but hopefully this will provide an extra speed bump when authoring and reviewing changes.)

While experimenting on that branch I also got a better feel for how we could split logic between the api/controller level and the model/view level.  My notes on this may be a bit dated, but in some modules we mix api functionality with diesel and serde structs.  For instance, the following files contain logic for all these concerns:

* `keyword.rs`
* `krate/mod.rs`
* `token.rs`
* `user/mod.rs`
* `version/mod.rs`

In contrast, the following files contain only diesel and serde functionality, in support of api calls handled by the files above:

* `category.rs`
* `dependency.rs`
* `download.rs`
* `owner.rs`

It would be nice if we could pull out the database and json related functionality in the first list, and then group all of those files in a new submodule.  At this time, I do not think it is necessary to try to split apart the model and view functionality as this logic is very straightforward.

For now, I hope this is a small enough PR to review independently and provide a place to discuss these suggestions for further refactoring.

/cc @vignesh-sankaran
@bors-voyager
Copy link
Contributor

bors-voyager bot commented Sep 27, 2017

Build succeeded

@bors-voyager bors-voyager bot merged commit 7e88ba5 into rust-lang:master Sep 27, 2017
@vignesh-sankaran
Copy link
Contributor

Yep, I reviewed the functionality and the github + category stuff works fine :). We'll continue the discussion regarding the refactoring in the issue itself.

@jtgeibel jtgeibel deleted the begin-module-refactor branch October 3, 2017 04:53
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.

3 participants