Skip to content

Commit

Permalink
added api explanation page
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolai Schimke authored and Nikolai Schimke committed Feb 1, 2024
1 parent 359c95a commit c39ded4
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 7 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@
*/15 * * * * sportshub data update -H
```

- server has the following urls:
- `/` - returns all scraped data
- `/active` - returns all links with active streams (with links to the streams)
- `/id/<id>` - returns the data for the given id
- server has the following routes:

- informational

- `/` - returns a list of all available routes
- `/version` - returns the version of the server
- `/info/leagues` - returns a list of all leagues
- `/info/sports` - returns a list of all sports

- data
- `/all` - returns all events
- `/active` - returns all active events
- `/sport/<sport>` - returns all events of a specific sport
- `/league/<league>` - returns all events of a specific league
- `/id/<id>` - returns a specific event
- `/team/home/<team>` - returns all events where the home team is `<team>`
- `/team/away/<team>` - returns all events where the away team is `<team>`
- `/team/<team>` - returns all events where `<team>` is either home or away
6 changes: 6 additions & 0 deletions src/db/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ pub fn get_active_games(conn: &mut SqliteConnection) -> Result<Vec<Stream>, anyh
pub fn delete_all_streams(conn: &mut SqliteConnection) -> Result<usize, anyhow::Error> {
Ok(diesel::delete(stream).execute(conn)?)
}

pub fn get_streams_by_league(conn: &mut SqliteConnection, search_league: String) -> Result<Vec<Stream>, anyhow::Error> {
Ok(stream
.filter(schema::stream::league.eq(search_league))
.load::<Stream>(conn)?)
}
108 changes: 108 additions & 0 deletions src/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<html>

<body>
<h1>sportshub-{{version}} has the following routes:</h1>

<h2>Informational</h2>
<table>
<tr>
<th>Link</th>
<th>Description</th>
</tr>
<tr>
<td><a href="/">/</a></td>
<td>returns a list of all available routes</td>
</tr>
<tr>
<td><a href="/version">/version</a></td>
<td>returns the version of the server</td>
</tr>
<tr>
<td><a href="/info/leagues">/info/leagues</a></td>
<td>returns a list of all leagues</td>
</tr>
<tr>
<td><a href="/info/sports">/info/sports</a></td>
<td>returns a list of all sports</td>
</tr>

</table>

<h2>API</h2>
<table>
<tr>
<th>Link</th>
<th>Input</th>
<th>Output</th>
</tr>
<tr>
<td><a href="/all">/all</a></td>
<td>none</td>
<td>all events</td>
</tr>
<tr>
<td><a href="/active">/active</a></td>
<td>none</td>
<td>all active events</td>
</tr>
<tr>
<td><a href="/sport/<sport>">/sport/&lt;sport&gt;</a></td>
<td>sport</td>
<td>all events of a specific sport</td>
</tr>
<tr>
<td><a href="/league/<league>">/league/&lt;league&gt;</a></td>
<td>league</td>
<td>all events of a specific league</td>
</tr>
<tr>
<td><a href="/id/<id>">/id/&lt;id&gt;</a></td>
<td>id</td>
<td>a specific event</td>
</tr>
<tr>
<td><a href="/team/home/<team>">/team/home/&lt;team&gt;</a></td>
<td>team</td>
<td>all events where the home team is <team>
</td>
</tr>
<tr>
<td><a href="/team/away/<team>">/team/away/&lt;team&gt;</a></td>
<td>team</td>
<td>all events where the away team is <team>
</td>
</tr>
<tr>
<td><a href="/team/<team>">/team/&lt;team&gt;</a></td>
<td>team</td>
<td>all events where <team> is either home or away</td>
</tr>
</table>


</body>

<style>
:root {
font-family: 'Arial Narrow Bold', sans-serif;
}

table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}

td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}

tr:nth-child(even) {
background-color: #dddddd;
}
</style>

</html>
26 changes: 23 additions & 3 deletions src/web_server_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@
//! It uses the rocket framework.
use db::models::Stream;
use rocket::{get, routes, serde::json::Json, Rocket};
use rocket::{get, response::content::RawHtml, routes, serde::json::Json, Rocket};

use crate::{
constants::{self, sports::Sport},
db::{self, helpers::LeagueWithCountry},
};

pub const INDEX_HTML: &str = include_str!("html/index.html");

#[get("/")]
async fn get_route_desc() -> &'static str {
"Hello, world!"
async fn get_route_desc() -> RawHtml<String> {
let out = INDEX_HTML.replace("{{version}}", env!("CARGO_PKG_VERSION")).to_owned();

RawHtml(out)
}

#[get("/version")]
async fn get_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

#[get("/all")]
Expand Down Expand Up @@ -70,6 +79,14 @@ async fn get_streams_by_either_team(team: &str) -> Json<Vec<Stream>> {
Json(streams)
}

#[get("/leagues/<league>")]
async fn get_streams_by_league(league: &str) -> Json<Vec<Stream>> {
let mut conn = db::helpers::establish_connection().unwrap();
let streams = db::helpers::get_streams_by_league(&mut conn, league.to_owned()).unwrap();

Json(streams)
}

#[get("/leagues")]
async fn info_get_leagues() -> Json<Vec<LeagueWithCountry>> {
let mut conn = db::helpers::establish_connection().unwrap();
Expand All @@ -85,6 +102,7 @@ async fn info_get_sports() -> Json<Vec<Sport>> {
Json(sports)
}


pub async fn run(port: u16, silent: bool) -> anyhow::Result<()> {
Rocket::custom(rocket::Config {
port,
Expand All @@ -99,10 +117,12 @@ pub async fn run(port: u16, silent: bool) -> anyhow::Result<()> {
"/",
routes![
get_route_desc,
get_version,
get_all_streams,
get_active_streams,
get_stream_by_id,
get_streams_by_sport,
get_streams_by_league,
get_streams_by_home_team,
get_streams_by_away_team,
get_streams_by_either_team,
Expand Down

0 comments on commit c39ded4

Please sign in to comment.