Skip to content

Commit

Permalink
feat: edit api path
Browse files Browse the repository at this point in the history
  • Loading branch information
phillychi3 committed Dec 27, 2024
1 parent 227984a commit 778f2d4
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions frontend/web/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const APIUrl = import.meta.env.DEV ? 'http://localhost:8000' : '/api'
3 changes: 2 additions & 1 deletion frontend/web/src/lib/components/Filepreview.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { onMount } from 'svelte'
import { APIUrl } from '$lib/api';
export let filePath: string
export let fileType: string
Expand All @@ -12,7 +13,7 @@
async function loadFile() {
try {
const response = await fetch(`http://localhost:8000/stream/file/${fileId}`)
const response = await fetch(`${APIUrl}/stream/file/${fileId}`)
if (!response.ok) throw new Error('檔案載入失敗')
if (fileType === 'txt') {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/lib/stores/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writable } from 'svelte/store'
import type { MusicTrack, Video } from '$lib/types'
import type { MediaPlayerElement } from 'vidstack/elements'
import { playerliststat } from '$lib/stores/playerlist'
import { APIUrl } from '$lib/api'

interface PlayerState {
type: 'video' | 'audio' | null
Expand Down Expand Up @@ -50,7 +51,7 @@ function createPlayerState() {
return
}
try {
currentState.player.src = `http://localhost:8000/stream/music/${track.id}`
currentState.player.src = `${APIUrl}/stream/music/${track.id}`
await new Promise<void>((resolve, reject) => {
const onCanPlay = () => {
currentState.player?.removeEventListener('can-play', onCanPlay)
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import type { MusicTrack, Video } from '$lib/types'
import Cd from '$lib/image/cd.svelte'
import Cookies from 'js-cookie'
import { APIUrl } from '$lib/api'
let player = $state<MediaPlayerElement | null>(null)
Expand Down Expand Up @@ -77,7 +78,7 @@
}
})
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=300`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=300`
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import CD from '$lib/image/cd.svelte'
import Album from '$lib/image/album.svelte'
import { goto } from '$app/navigation'
import { APIUrl } from '$lib/api'
export let data
const { info, error, status } = data
Expand All @@ -24,7 +25,7 @@
playVideo(video)
}
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=400`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=400`
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/+page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load = (async ({ fetch }) => {
try {
const responsemusic = await fetch('http://localhost:8000/file/info')
const responsemusic = await fetch(`${APIUrl}/file/info`)

if (!responsemusic.ok) {
return {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/albums/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import { getContext } from 'svelte'
import type { MusicTrack } from '$lib/types'
import Album from '$lib/image/album.svelte'
import { APIUrl } from '$lib/api'
export let data: PageData
const { albums, error, status } = data
const playMusic = getContext<(track: MusicTrack) => void>('playMusic')
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=400`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=400`
}
function playAlbum(album: any) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/albums/+page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load = (async ({ fetch }) => {
try {
const response = await fetch('http://localhost:8000/file/album')
const response = await fetch(`${APIUrl}/file/album`)

if (!response.ok) {
return {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/albums/[albumid]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import { getContext } from 'svelte'
import type { MusicTrack } from '$lib/types'
import Album from '$lib/image/album.svelte'
import { APIUrl } from '$lib/api'
export let data: PageData
const { album } = data
const playMusic = getContext<(track: MusicTrack) => void>('playMusic')
const playMusiclist = getContext<(track: MusicTrack) => void>('playMusiclist')
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=400`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=400`
}
function formatDuration(seconds: number) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/albums/[albumid]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load: PageLoad = async ({ fetch, params }) => {
const response = await fetch(`http://localhost:8000/file/album?album_id=${params.albumid}`)
const response = await fetch(`${APIUrl}/file/album?album_id=${params.albumid}`)
const album = await response.json()
return { album }
}
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/file/+page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load = (async ({ fetch }) => {
try {
const response = await fetch('http://localhost:8000/file/file')
const response = await fetch(`${APIUrl}/file/file`)

if (!response.ok) {
return {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/music/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { MusicTrack } from '$lib/types'
import { Play } from 'lucide-svelte'
import CD from '$lib/image/cd.svelte'
import { APIUrl } from '$lib/api'
const { musics, error, status } = data
const playMusic = getContext<(track: MusicTrack) => void>('playMusic')
Expand All @@ -12,7 +13,7 @@
}
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=400`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=400`
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/music/+page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load = (async ({ fetch }) => {
try {
const response = await fetch('http://localhost:8000/file/music')
const response = await fetch(`${APIUrl}/file/music`)

if (!response.ok) {
return {
Expand Down
8 changes: 5 additions & 3 deletions frontend/web/src/routes/app/setting/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { on } from 'svelte/events'
import type { PageData } from './$types'
import { Folder, File } from 'lucide-svelte'
import { APIUrl } from '$lib/api'
let { data }: { data: PageData } = $props()
interface DirectoryContent {
Expand All @@ -27,7 +29,7 @@
async function fetchDirs(path: string) {
try {
const response = await fetch(
`http://localhost:8000/setting/dir?path=${encodeURIComponent(path)}`
`${APIUrl}/setting/dir?path=${encodeURIComponent(path)}`
)
const data: DirectoryContent = await response.json()
Expand Down Expand Up @@ -69,7 +71,7 @@
async function handleScan() {
try {
scanStatus = '掃描中...'
const response = await fetch(`http://localhost:8000/file/parse_file`, {
const response = await fetch(`${APIUrl}/file/parse_file`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down Expand Up @@ -101,7 +103,7 @@
async function handleScanDir() {
try {
scanStatus = '掃描中...'
const response = await fetch(`http://localhost:8000/file/scanall`, {
const response = await fetch(`${APIUrl}/file/scanall`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/video/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { getContext } from 'svelte'
import type { Video } from '$lib/types'
import { Play } from 'lucide-svelte'
import { APIUrl } from '$lib/api'
export let data
const { videos, error, status } = data
Expand All @@ -23,7 +24,7 @@
return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`
}
function getCoverArtUrl(coverArt: string) {
return `http://localhost:8000/file/image?image_id=${coverArt}&image_size=400`
return `${APIUrl}/file/image?image_id=${coverArt}&image_size=400`
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/video/+page.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { PageLoad } from './$types'
import { APIUrl } from '$lib/api'

export const load = (async ({ fetch }) => {
try {
const response = await fetch('http://localhost:8000/file/video')
const response = await fetch(`${APIUrl}/file/video`)

if (!response.ok) {
return {
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/videoplay/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import type { PageData } from './$types'
import 'vidstack/bundle'
import type { MediaPlayerElement } from 'vidstack/elements'
import { APIUrl } from '$lib/api'
export let data: PageData
const { video } = data
Expand Down Expand Up @@ -42,7 +43,7 @@
<div class="fixed inset-0 bg-black">
<media-player bind:this={player} class="h-full" autoplay>
<media-provider>
<source src={`http://localhost:8000/stream/video/${video.id}`} type="video/mp4" />
<source src={`${APIUrl}/stream/video/${video.id}`} type="video/mp4" />
</media-provider>
<media-video-layout>
<media-controls></media-controls>
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/app/videoplay/+page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PageLoad } from './$types'
import { error } from '@sveltejs/kit'
import { APIUrl } from '$lib/api'

export const load: PageLoad = async ({ url, fetch }) => {
const videoId = url.searchParams.get('id')
Expand All @@ -9,7 +10,7 @@ export const load: PageLoad = async ({ url, fetch }) => {
}

try {
const response = await fetch(`http://localhost:8000/file/video?video_id=${videoId}`)
const response = await fetch(`${APIUrl}/file/video?video_id=${videoId}`)
if (!response.ok) throw new Error('影片載入失敗')

const video = await response.json()
Expand Down
3 changes: 2 additions & 1 deletion frontend/web/src/routes/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { goto } from '$app/navigation'
import { Music } from 'lucide-svelte'
import Cookies from 'js-cookie'
import { APIUrl } from '$lib/api';
interface LoginResponse {
access_token: string
Expand All @@ -20,7 +21,7 @@
}
try {
const response = await fetch('http://localhost:8000/auth/login', {
const response = await fetch(`${APIUrl}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
Expand Down
7 changes: 6 additions & 1 deletion frontend/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ import { vite as vidstack } from 'vidstack/plugins'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [vidstack(), sveltekit()]
plugins: [vidstack(), sveltekit()],
server: {
proxy: {
'/api': 'http://localhost:8000'
}
}
})

0 comments on commit 778f2d4

Please sign in to comment.