diff --git a/components/Item.vue b/components/Item.vue index 308ff26..2d02bc3 100644 --- a/components/Item.vue +++ b/components/Item.vue @@ -8,6 +8,7 @@ import type { BaseService } from '~/types' const props = defineProps() -const type = capitalize(props.type || 'base') -const component = defineAsyncComponent(() => import(`~/components/service/${type}.vue`)) +const component = props.type + ? defineAsyncComponent(() => import(`~/components/service/${capitalize(props.type!)}.vue`)) + : resolveComponent('ServiceBase') diff --git a/components/service/Base.vue b/components/service/Base.vue deleted file mode 100644 index 4adbef2..0000000 --- a/components/service/Base.vue +++ /dev/null @@ -1,42 +0,0 @@ - - - diff --git a/components/service/Ping.vue b/components/service/Ping.vue deleted file mode 100644 index 443f613..0000000 --- a/components/service/Ping.vue +++ /dev/null @@ -1,23 +0,0 @@ - - - diff --git a/components/service/base/Icon.vue b/components/service/base/Icon.vue new file mode 100644 index 0000000..1d95113 --- /dev/null +++ b/components/service/base/Icon.vue @@ -0,0 +1,25 @@ + + + diff --git a/components/service/base/Index.vue b/components/service/base/Index.vue new file mode 100644 index 0000000..70edf52 --- /dev/null +++ b/components/service/base/Index.vue @@ -0,0 +1,37 @@ + + + diff --git a/components/service/base/Status.vue b/components/service/base/Status.vue new file mode 100644 index 0000000..530c901 --- /dev/null +++ b/components/service/base/Status.vue @@ -0,0 +1,23 @@ + + + diff --git a/composables/services.ts b/composables/services.ts new file mode 100644 index 0000000..2d32ed9 --- /dev/null +++ b/composables/services.ts @@ -0,0 +1,29 @@ +import type { BaseService } from '~/types' + +export interface ServiceDataOptions { + immediate?: boolean + updateInterval?: number +} + +export function useServiceData(service: T, options?: ServiceDataOptions) { + const immediate = options?.immediate || true + const updateInterval = (options?.updateInterval || 60) * 1000 + const type = service.type || 'base' + + const { data, pending, status, refresh, execute } = useFetch(`/api/services/${type}`, { + immediate, + query: { id: service.id }, + timeout: 15000, + }) + + const { pause, resume } = useIntervalFn(refresh, updateInterval, { immediate }) + + return { + data, + pending, + status, + execute, + pauseUpdate: pause, + resumeUpdate: resume, + } +} diff --git a/server/api/services/base.ts b/server/api/services/base.ts new file mode 100644 index 0000000..9521ad6 --- /dev/null +++ b/server/api/services/base.ts @@ -0,0 +1,35 @@ +import type { BaseService } from '~/types' + +export interface State { + ping: { + status: boolean + time: number + } +} + +export default defineEventHandler(async (event): Promise => { + const service = await getService(event) + const state: State = { + ping: { + status: false, + time: -1, + }, + } + + if (service?.status?.enabled) { + try { + const startTime = new Date().getTime() + await $fetch(service.link, { timeout: 15000 }) + const endTime = new Date().getTime() + + state.ping = { + status: true, + time: endTime - startTime, + } + } catch (e) { + logger.error(e) + } + } + + return state +}) diff --git a/server/api/services/ping.ts b/server/api/services/ping.ts deleted file mode 100644 index 1e075eb..0000000 --- a/server/api/services/ping.ts +++ /dev/null @@ -1,18 +0,0 @@ -import type { PingService } from '~/types' - -export default defineEventHandler(async (event) => { - const service = await getService(event) - - try { - const startTime = new Date().getTime() - await $fetch(service.link) - const endTime = new Date().getTime() - const time = endTime - startTime - - return { time } - } catch (e) { - logger.error(e) - } - - return { time: -1 } -}) diff --git a/types/services.d.ts b/types/services.d.ts index e4e24d9..33678d7 100644 --- a/types/services.d.ts +++ b/types/services.d.ts @@ -1,22 +1,24 @@ +export interface ServiceStatus { + enabled?: boolean + interval?: number +} + +export interface ServiceIcon { + url?: string + name?: string + wrap?: boolean + background?: string + color?: string +} + export interface BaseService { id: string type?: string title: string description?: string link: string - icon?: { - url?: string - name?: string - wrap?: boolean - background?: string - color?: string - } + icon?: ServiceIcon + status?: ServiceStatus options?: Record secrets?: Record } - -export interface PingService extends BaseService { - options?: { - interval?: number - } -}