Final commit
This commit is contained in:
parent
91fffb3294
commit
c35e0716af
372 changed files with 16591 additions and 1 deletions
190
backend/main.ts
Normal file
190
backend/main.ts
Normal file
|
@ -0,0 +1,190 @@
|
|||
const PORT = parseInt(Deno.env.get("PORT") || "8000");
|
||||
import {
|
||||
GetNewSuggestInput,
|
||||
GetNewSuggestOutput,
|
||||
GetSongsTextParams,
|
||||
GetTrackInput,
|
||||
GetYoutubeInput,
|
||||
} from "./types/api.ts";
|
||||
import { getSong } from "./routes/get-song.ts";
|
||||
import { getSpotifyTrackById } from "./routes/get-track.ts";
|
||||
import { getVids } from "./routes/get-video.ts";
|
||||
import { getSpotifyAccessToken } from "./spotify-api.ts";
|
||||
import { generateVibes } from "./ai-suggestion.ts";
|
||||
import { getNewSuggestion } from "./routes/get-new-suggest.ts";
|
||||
|
||||
const routes = [
|
||||
{
|
||||
pattern: new URLPattern({ pathname: "/music" }),
|
||||
handler: async (req: Request) => {
|
||||
const params = await req.json();
|
||||
const songs = await getSong(params);
|
||||
|
||||
return new Response(JSON.stringify(songs), {
|
||||
status: 200,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: new URLPattern({ pathname: "/health" }),
|
||||
handler: (_req: Request) => {
|
||||
return new Response("All good", {
|
||||
status: 200,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: new URLPattern({ pathname: "/video" }),
|
||||
handler: async (req: Request) => {
|
||||
const params: GetYoutubeInput = await req.json();
|
||||
const vids = await getVids(params);
|
||||
|
||||
return new Response(JSON.stringify(vids), {
|
||||
status: 200,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: new URLPattern({ pathname: "/track" }),
|
||||
handler: async (req: Request) => {
|
||||
const params: GetTrackInput = await req.json();
|
||||
const track = await getSpotifyTrackById(params.trackId);
|
||||
|
||||
return new Response(JSON.stringify(track), {
|
||||
status: 200,
|
||||
});
|
||||
},
|
||||
},
|
||||
{
|
||||
pattern: new URLPattern({ pathname: "/newSuggestions" }),
|
||||
handler: async (req: Request) => {
|
||||
const params: GetNewSuggestInput = await req.json();
|
||||
const suggestions: GetNewSuggestOutput = await getNewSuggestion(params);
|
||||
return new Response(JSON.stringify(suggestions), {
|
||||
status: 200,
|
||||
});
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const cors = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
||||
};
|
||||
|
||||
const handler = async (req: Request): Promise<Response> => {
|
||||
if (req.method === "OPTIONS") {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: cors,
|
||||
});
|
||||
}
|
||||
|
||||
let response: Response | undefined;
|
||||
try {
|
||||
for (const route of routes) {
|
||||
const match = route.pattern.exec(req.url);
|
||||
if (match) {
|
||||
response = await route.handler(req);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Server error:", error);
|
||||
response = new Response(error.toString() || "Internal server error", {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
|
||||
response ??= new Response("Not found", { status: 404 });
|
||||
|
||||
for (const [key, value] of Object.entries(cors)) {
|
||||
response.headers.set(key, value);
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
Deno.serve({ hostname: "0.0.0.0", port: PORT }, handler);
|
||||
|
||||
Deno.test("music API complete workflow", async () => {
|
||||
const spotify_api_key = getSpotifyAccessToken();
|
||||
if (!spotify_api_key) throw Error("Testing token not found");
|
||||
|
||||
const testInput: GetSongsTextParams = {
|
||||
prompt: "i want summer feeling music",
|
||||
rules: "make it fun and suitable for children",
|
||||
};
|
||||
const res = await fetch(`http://localhost:${PORT}/music`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(testInput),
|
||||
});
|
||||
|
||||
if (res.status !== 200) throw new Error(`Unexpected status: ${res.status}`);
|
||||
const json = await res.json();
|
||||
console.log("API Response:", json);
|
||||
});
|
||||
|
||||
Deno.test("video API complete workflow", async () => {
|
||||
const yt_api_key = Deno.env.get("YT_API_KEY");
|
||||
if (!yt_api_key) throw Error("Testing token not found");
|
||||
|
||||
const input: GetYoutubeInput = {
|
||||
title: "Here Comes The Sun",
|
||||
artist: "The Beatles",
|
||||
};
|
||||
const res = await fetch(`http://localhost:${PORT}/video`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
if (res.status !== 200) throw new Error(`Unexpected status: ${res.status}`);
|
||||
const json = await res.json();
|
||||
console.log("Video API Response:", json);
|
||||
});
|
||||
|
||||
Deno.test("track API complete workflow", async () => {
|
||||
const spotify_api_key = getSpotifyAccessToken();
|
||||
if (!spotify_api_key) throw Error("Testing token not found");
|
||||
|
||||
const input: GetTrackInput = { trackId: "6dGnYIeXmHdcikdzNNDMm2" };
|
||||
const res = await fetch(`http://localhost:${PORT}/track`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
|
||||
if (res.status !== 200) throw new Error(`Unexpected status: ${res.status}`);
|
||||
const json = await res.json();
|
||||
console.log("Track API Response:", json);
|
||||
});
|
||||
|
||||
Deno.test("new suggestion API complete workflow", async () => {
|
||||
const testInput: GetNewSuggestInput = {
|
||||
type: "from-songs-suggestion",
|
||||
rules: "The songs should be upbeat and suitable for a party",
|
||||
songs: [
|
||||
{ song: "Thriller - Michael Jackson" },
|
||||
{ song: "Uptown Funk - Bruno Mars" },
|
||||
{ song: "24K Magic - Bruno Mars" },
|
||||
],
|
||||
};
|
||||
|
||||
const res = await fetch(`http://localhost:${PORT}/newSuggestions`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(testInput),
|
||||
});
|
||||
|
||||
if (res.status !== 200) throw new Error(`Unexpected status: ${res.status}`);
|
||||
|
||||
const json = await res.json();
|
||||
console.log("New Suggestion API Response:", json);
|
||||
|
||||
// Validate that the response contains a songs array.
|
||||
if (!json.songs || !Array.isArray(json.songs)) {
|
||||
throw new Error("Response does not contain a valid songs array");
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue