-
Notifications
You must be signed in to change notification settings - Fork 44
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
Refactor API: replace Warp with Axum #152
Refactor API: replace Warp with Axum #152
Conversation
We are going to reimplement the API with Axum.
9c8c3e2
to
7471820
Compare
- Test scaffolding - Dummy entrypoint
Code for API testing have been reorganized.
2fb0612
to
892f285
Compare
892f285
to
6a9e2d5
Compare
src/setup.rs
Outdated
if config.http_api.enabled { | ||
// Temporarily running the new API in the 1313 port | ||
let bind_address = config.http_api.bind_address.clone(); | ||
let mut bind_socket: SocketAddr = bind_address.parse().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer expects over unwrap?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed it, but we have a lot of "unwraps". For example like this:
InfoHash::from_str(&hash).unwrap();
I think it would not be very pleasant to write a message for all of them:
InfoHash::from_str(&hash).expect("string should be a valid infohash");
I have to think about it, but I would say it makes sense when the case is unique.
Maybe the problem is we are duplicating code, and we should add a function like this:
fn parse_socket_addr_or_fail(bind_address: &str) -> SocketAddr {
bind_address.parse().expect("the bind address string should contain a valid socket address like this 127.0.1.1:8080")
}
- Extract domain logic: `Tracker::get_torrents_metrics`. - Move domain logic from web framework controllers to domain services: `get_metrics`. - Remove duplicate code in current Warp API and new Axum API.
de65a97
to
1c6db6e
Compare
It keeps the same contract of the API. It returns 500 status code with error message in "debug" format.
It was added to test Axum configuration.
hi @da2ce7 @WarmBeer It seems Axum does not support SSL directly. I can use the axum-server, which is the one used in this fork. |
The new API implementation uses Axum. Axum does not support SSL configuration. The "axum-server" crate provides it.
I did it using axum-server. |
I'm working on the
I have commented on this issue serde-rs/json#502 I'm going to find out why it's working in the current API (if it's) |
…ndpoint Not all cases finished yet. Not found case is pending.
The happy path for the endpoint |
…ET /api/torrent/:info_hash endpoint
…nt. Not found case
It works if I do not use
Probably for the same reason it's working on the old Warp implementation. |
hi, @WarmBeer I think I've found a bug in both the current API implementation and the new one with Axum. It seems the
I suppose that is not the expected behavior. |
It seems the problem is only with |
…rrent twice Previous behavior: When you try to remove a non exisinting whitelisted torrent the response is 500 error. New bahavior: The enpoints checks if the torrent is included in the whitelist. If it does not, then it ignores the reqeust returning a 200 code. It should return a 204 or 404 but the current API only uses these codes: 200, 400, 405, 500. In the new API version we are planning to refctor all endpoints.
436bc96
to
8d32628
Compare
hi @da2ce7 @WarmBeer this is ready to review. I would like to refactor a little bit more, but I think the PR is getting too big. We can merge it as it is and I will create more issues. I have added some I also want to make some improvements, like reorganising the API mod structure. Right now, the folder structure is like this: src/apis/
├── handlers.rs
├── middlewares
│ ├── auth.rs
│ └── mod.rs
├── mod.rs
├── resources
│ ├── auth_key.rs
│ ├── mod.rs
│ ├── peer.rs
│ ├── stats.rs
│ └── torrent.rs
├── responses.rs
├── routes.rs
└── server.rs It's an implementation-detail or technical-detail organization. I would like to use something more like this: └── src
├── apis
│ ├── contexts
│ │ ├── auth_key
│ │ │ ├── handlers.rs
│ │ │ ├── mod.rs
│ │ │ ├── resources.rs
│ │ │ ├── responses.rs
│ │ │ └── routes.rs
│ │ ├── mod.rs
│ │ ├── stats
│ │ │ ├── handlers.rs
│ │ │ ├── mod.rs
│ │ │ ├── resources.rs
│ │ │ ├── responses.rs
│ │ │ └── routes.rs
│ │ ├── torrent
│ │ │ ├── handlers.rs
│ │ │ ├── mod.rs
│ │ │ ├── resources.rs
│ │ │ ├── responses.rs
│ │ │ └── routes.rs
│ │ └── whitelist
│ │ ├── handlers.rs
│ │ ├── mod.rs
│ │ ├── resources.rs
│ │ ├── responses.rs
│ │ └── routes.rs
│ ├── middlewares
│ │ ├── auth.rs
│ │ └── mod.rs
│ ├── mod.rs
│ ├── routes.rs
│ └── server.rs
└── mod.rs I like the folder structure to be more domain-oriented, and it's also more scalable. If you agree I can add a new issue for that and we can implement it before implementing the new API version. |
ACK 0c3ca87 |
UPDATED: 12/01/2022
The tracker API uses the web framework Warp.
This PR replaces Warp with Axum.
Tasks
ActionStatus::Err
error with a message.200
) responses./api/xxx
.start
andstart_tls
)info_hash
andseconds_valid
Path params, andoffset
andlimit
GET params.token
. The token must be the first param in thequery
otherwise, it does not work. In the end, it was not a bug. It only happens withcurl
.:
Unhandled rejection: Err { reason: "failed to remove torrent from whitelist" }
Endpoints subtasks
Stats:
GET /api/stats
Torrents:
GET /api/torrents?offset=:u32&limit=:u32
GET /api/torrent/:info_hash
Whitelisted torrents:
POST /api/whitelist/:info_hash
DELETE /api/whitelist/:info_hash
Whitelist commands:
GET /api/whitelist/reload
Keys:
POST /api/key/:seconds_valid
DELETE /api/key/:key
Key commands
GET /api/keys/reload