-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(federation): add basic federation for trips (#11)
- Loading branch information
Showing
18 changed files
with
195 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { FetchError, ofetch } from "ofetch" | ||
|
||
import { FEDERATION_TARGETS } from "@karr/config" | ||
import { Trip } from "@karr/db/schemas/trips.js" | ||
import logger from "@karr/util/logger" | ||
|
||
import { DataResponse } from "@/lib/types" | ||
|
||
export async function getFederatedTrips(): Promise<Trip[]> { | ||
const trips: Trip[] = [] | ||
try { | ||
for await (const target of FEDERATION_TARGETS) { | ||
const t = await ofetch<DataResponse<Trip[]>>( | ||
"/api/v1/federation/trips/search", | ||
{ | ||
baseURL: target.url, | ||
headers: { | ||
Cookie: `auth-token=federation` | ||
} | ||
} | ||
) | ||
for (const trip of t.data) { | ||
trip.origin = target.name | ||
trips.push(trip) | ||
} | ||
} | ||
} catch (error) { | ||
logger.error("Error fetching trips from federation:", (error as FetchError)?.data) | ||
} | ||
return trips | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Hono } from "hono" | ||
|
||
import trips from "./trips" | ||
|
||
/** | ||
* Federation endpoint | ||
*/ | ||
const hono = new Hono() | ||
|
||
hono.route("/trips", trips) | ||
|
||
export default hono |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Hono } from "hono" | ||
|
||
import logger from "@karr/util/logger" | ||
|
||
import { getTrips } from "@/db/trips" | ||
import { handleRequest } from "@/lib/helpers" | ||
|
||
/** | ||
* Federation trips endpoint | ||
*/ | ||
const hono = new Hono() | ||
|
||
hono.get("/search", async (c) => { | ||
logger.debug("GET /trips") | ||
return handleRequest(c, getTrips) | ||
}) | ||
|
||
export default hono |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.