Skip to content

Commit

Permalink
add comments, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Jul 31, 2023
1 parent e7a39f1 commit 73034b4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 39 deletions.
48 changes: 33 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![Quality] ![API Response] ![CDN Response]

Source code for the API powering [**wanderer.moe**](https://wanderer.moe) — using **Cloudflare Workers** with **R2 Storage** for the CDN, and **D1** for the Database.
Source code for the API powering [**wanderer.moe**](https://wanderer.moe) — using **Cloudflare Workers** with **R2 Storage** for the CDN, and **Planetscale** for the Database.

</div>

Expand All @@ -16,6 +16,8 @@ Source code for the API powering [**wanderer.moe**](https://wanderer.moe) — us

Configuration is in `wrangler.toml` - this includes the R2 Bucket and D1 Database.

You will need to setup environment variables for the Discord Bot Token for `/contributors` route: `DISCORD_TOKEN` using `wrangler secret put`.

- Run `wrangler dev` to preview locally.
- Run `wrangler deploy` to publish to Cloudflare Workers.

Expand All @@ -27,43 +29,59 @@ Configuration is in `wrangler.toml` - this includes the R2 Bucket and D1 Databas

## API Reference

### Search (Assets)
### Assets

#### Search Assets

```http
GET /search/assets?query=query&tags=tag1,tag2&after=after
GET /search?query=query&game=game1,game2&asset=asset
```

| Parameter | Type | Description |
| :-------- | :------- | :----------------------------------------------------------------------- |
| `query` | `string` | **Optional**. Search query for assets |
| `game` | `string` | **Optional**. Can be a comma seperated list of game names to search for |
| `asset` | `string` | **Optional**. Can be a comma seperated list of asset types to search for |

The max response the API will give is always 1500 assets.

### Recent Assets

```
GET /recent
```

| Parameter | Type | Description |
| :-------- | :------- | :--------------------------------------------------------------------------------------------- |
| `query` | `string` | **Optional**. Search query for assets |
| `tags` | `string` | **Optional**. Comma-separated list of tags to filter by (in order of `game`, `asset-category`) |
| `after` | `string` | **Optional**. Cursor to paginate through results |
Returns the 30 most recent assets based off of the `uploaded_date` field.

If no query is provided, the API will return a list of 100 most recently uploaded assets.
### Download Asset

```http
GET /download/:assetId
```

Downloads the asset with the given `assetId`.

### Search Users

#### Search Users (Name)

```http
GET /user/search/:query
GET /user/s/:query
```

| Parameter | Type | Description |
| :-------- | :------- | :----------------------------------- |
| `query` | `string` | **Required**. Search query for users |

#### Get User Data (ID)
#### Get Username

```http
GET /user/:id
GET /user/:username
```

| Parameter | Type | Description |
| :-------- | :------- | :----------------------------------- |
| `id` | `string` | **Required**. Search query for users |
| Parameter | Type | Description |
| :--------- | :------- | :--------------------- |
| `username` | `string` | **Required**. Username |

### OC Generator

Expand Down
1 change: 1 addition & 0 deletions src/lib/helpers/responses/notFoundResponse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// helper function to create a 404 Not Found response
export function createNotFoundResponse(errorMessage, responseHeaders) {
const responseBody = {
success: false,
Expand Down
1 change: 1 addition & 0 deletions src/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { responseHeaders } from "@/lib/responseHeaders";

// generic error handler wrapper
export const errorHandler =
(handler: (request: Request, env: Env) => Promise<Response>) =>
async (request: Request, env: Env): Promise<Response> => {
Expand Down
6 changes: 0 additions & 6 deletions src/middleware/unwantedPrefixes.ts

This file was deleted.

4 changes: 3 additions & 1 deletion src/routes/download/downloadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const downloadFile = async (
if (!row)
return createNotFoundResponse("Asset ID not found", responseHeaders);

const response = await fetch(`https://cdn.wanderer.moe/${row.url}`);
const response = await fetch(
`https://files.wanderer.moe/assets/${row.url}`
);
const headers = new Headers(response.headers);
headers.set("Content-Disposition", `attachment; filename="${row.name}"`);
const blob = await response.blob();
Expand Down
1 change: 1 addition & 0 deletions src/routes/games/allGames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const allGames = async (

if (response) return response;

// TODO: fix getting data from old D1 database but using Planetscale DB
const row: D1Result<Game> = await env.database
.prepare(`SELECT * FROM games`)
.run();
Expand Down
2 changes: 2 additions & 0 deletions src/routes/oc-generators/getGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const getGenerators = async (

if (response) return response;

// listing all files inside of oc-generators subfolder, as they can't be manually inputted
// by users but instead stored on the oc-generators repo
const files = await listBucket(env.bucket, {
prefix: "oc-generators/",
delimiter: "/",
Expand Down
35 changes: 18 additions & 17 deletions src/routes/search/search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { responseHeaders } from "@/lib/responseHeaders";
import type { Asset } from "@/lib/types/asset";
import * as queryLib from "@/lib/query";
import { getSearchResults } from "@/lib/query";
import { getConnection } from "@/lib/planetscale";
import { getQueryParam } from "@/lib/helpers/getQueryParams";

Expand All @@ -16,6 +16,7 @@ export const getSearch = async (
params[paramName] = getQueryParam(url, paramName);
}

// TODO: fix this cuz idk what's going on here
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { query, game, asset, tags } = params;
Expand All @@ -27,22 +28,22 @@ export const getSearch = async (

if (response) return response;

const results = (
await queryLib.getSearchResults(query, game, asset, tags, env)
).map((results) => {
return {
id: results.id,
name: results.name,
game: results.game,
asset_category: results.asset_category,
url: results.url,
tags: results.tags,
status: results.status,
uploaded_by: results.uploaded_by,
uploaded_date: results.uploaded_date,
file_size: results.file_size,
};
});
const results = (await getSearchResults(query, game, asset, tags, env)).map(
(results) => {
return {
id: results.id,
name: results.name,
game: results.game,
asset_category: results.asset_category,
url: results.url,
tags: results.tags,
status: results.status,
uploaded_by: results.uploaded_by,
uploaded_date: results.uploaded_date,
file_size: results.file_size,
};
}
);

response = new Response(
JSON.stringify({
Expand Down

0 comments on commit 73034b4

Please sign in to comment.