Skip to content

Commit 79fcf41

Browse files
authored
feat(frontend): lookup server by subdomain (#7210)
feat(frontend): lookup server by subdomain admin
1 parent 10b1d70 commit 79fcf41

6 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎apps/frontend/src/layouts/default.vue‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@
427427
tone: 'brand',
428428
shown: isAdmin(auth.user),
429429
},
430+
{
431+
id: 'servers-lookup',
432+
label: 'Server lookup',
433+
type: 'link',
434+
to: '/admin/servers/lookup',
435+
tone: 'brand',
436+
shown: isAdmin(auth.user),
437+
},
430438
{
431439
id: 'servers-notices',
432440
label: formatMessage(messages.manageServerNotices),
@@ -480,6 +488,7 @@
480488
<template #file-lookup>
481489
<FileIcon aria-hidden="true" /> {{ formatMessage(messages.fileLookup) }}
482490
</template>
491+
<template #servers-lookup> <ServerIcon aria-hidden="true" /> Server lookup </template>
483492
<template #servers-notices>
484493
<IssuesIcon aria-hidden="true" /> {{ formatMessage(messages.manageServerNotices) }}
485494
</template>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<template>
2+
<div class="normal-page no-sidebar">
3+
<h1>Server lookup</h1>
4+
<div class="normal-page__content">
5+
<form class="card flex flex-col gap-3" @submit.prevent="lookupServer">
6+
<div class="flex flex-col gap-2">
7+
<label for="server-subdomain">
8+
<span class="text-lg font-semibold text-contrast">
9+
Server subdomain
10+
<span class="text-brand-red">*</span>
11+
</span>
12+
</label>
13+
<StyledInput
14+
id="server-subdomain"
15+
v-model="subdomainInput"
16+
:icon="ServerIcon"
17+
:error="submitted && !!validationError"
18+
placeholder="example or example.modrinth.gg"
19+
autocomplete="off"
20+
autocapitalize="none"
21+
:spellcheck="false"
22+
/>
23+
<span v-if="submitted && validationError" class="text-sm font-medium text-red">
24+
{{ validationError }}
25+
</span>
26+
</div>
27+
28+
<div>
29+
<Button type="colored" color="brand" native-type="submit" :loading="isLoading">
30+
<SearchIcon aria-hidden="true" />
31+
Look up server
32+
</Button>
33+
</div>
34+
35+
<Admonition v-if="lookupError" type="critical" header="Lookup failed">
36+
{{ lookupError }}
37+
</Admonition>
38+
</form>
39+
</div>
40+
</div>
41+
</template>
42+
43+
<script setup lang="ts">
44+
import { ModrinthApiError } from '@modrinth/api-client'
45+
import { SearchIcon, ServerIcon } from '@modrinth/assets'
46+
import { Admonition, Button, injectModrinthClient, StyledInput } from '@modrinth/ui'
47+
48+
const client = injectModrinthClient()
49+
50+
const subdomainInput = ref('')
51+
const submitted = ref(false)
52+
const isLoading = ref(false)
53+
const lookupError = ref('')
54+
55+
const normalizedSubdomain = computed(() =>
56+
subdomainInput.value
57+
.trim()
58+
.replace(/\.modrinth\.gg$/i, '')
59+
.toLowerCase(),
60+
)
61+
const validationError = computed(() => {
62+
if (!normalizedSubdomain.value) {
63+
return 'Enter a server subdomain.'
64+
}
65+
66+
if (normalizedSubdomain.value.length > 63) {
67+
return 'Subdomain must be 63 characters or fewer.'
68+
}
69+
70+
if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(normalizedSubdomain.value)) {
71+
return 'Use only letters, numbers, and hyphens, without a leading or trailing hyphen.'
72+
}
73+
74+
return undefined
75+
})
76+
77+
watch(subdomainInput, (value) => {
78+
const withoutDomain = value.replace(/\.modrinth\.gg$/i, '')
79+
if (withoutDomain !== value) {
80+
subdomainInput.value = withoutDomain
81+
}
82+
lookupError.value = ''
83+
})
84+
85+
async function lookupServer() {
86+
if (isLoading.value) {
87+
return
88+
}
89+
90+
submitted.value = true
91+
lookupError.value = ''
92+
93+
if (validationError.value) {
94+
return
95+
}
96+
97+
subdomainInput.value = normalizedSubdomain.value
98+
isLoading.value = true
99+
100+
try {
101+
const server = await client.archon.servers_internal.getBySubdomain(normalizedSubdomain.value)
102+
await navigateTo(`/hosting/manage/${server.id}`)
103+
} catch (error) {
104+
if (error instanceof ModrinthApiError && error.statusCode === 404) {
105+
lookupError.value = `No server was found for ${normalizedSubdomain.value}.modrinth.gg.`
106+
} else {
107+
lookupError.value = 'The server could not be looked up. Try again.'
108+
}
109+
} finally {
110+
isLoading.value = false
111+
}
112+
}
113+
</script>

‎packages/api-client/src/modules/archon/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from './backups/v1'
33
export * from './backups-queue/v1'
44
export * from './content/v1'
55
export * from './properties/v1'
6+
export * from './servers/internal'
67
export * from './servers/v0'
78
export * from './servers/v1'
89
export * from './types'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AbstractModule } from '../../../core/abstract-module'
2+
import type { Archon } from '../types'
3+
4+
export class ArchonServersInternalModule extends AbstractModule {
5+
public getModuleID(): string {
6+
return 'archon_servers_internal'
7+
}
8+
9+
/**
10+
* Get the server assigned to a subdomain.
11+
* GET /_internal/servers/by-subdomain/:subdomain
12+
*/
13+
public async getBySubdomain(subdomain: string): Promise<Archon.Servers.Internal.Lookup> {
14+
return this.client.request<Archon.Servers.Internal.Lookup>(
15+
`/servers/by-subdomain/${encodeURIComponent(subdomain)}`,
16+
{
17+
api: 'archon',
18+
version: 'internal',
19+
method: 'GET',
20+
},
21+
)
22+
}
23+
}

‎packages/api-client/src/modules/archon/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,12 @@ export namespace Archon {
573573
}
574574

575575
export namespace Servers {
576+
export namespace Internal {
577+
export type Lookup = {
578+
id: string
579+
}
580+
}
581+
576582
export namespace v0 {
577583
export type ServerGetResponse = {
578584
servers: Server[]

‎packages/api-client/src/modules/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ArchonNoticesV0Module } from './archon/notices/v0'
99
import { ArchonOptionsV1Module } from './archon/options/v1'
1010
import { ArchonPropertiesV1Module } from './archon/properties/v1'
1111
import { ArchonServerUsersV1Module } from './archon/server-users/v1'
12+
import { ArchonServersInternalModule } from './archon/servers/internal'
1213
import { ArchonServersV0Module } from './archon/servers/v0'
1314
import { ArchonServersV1Module } from './archon/servers/v1'
1415
import { ArchonTransfersInternalModule } from './archon/transfers/internal'
@@ -87,6 +88,7 @@ export const MODULE_REGISTRY = {
8788
archon_options_v1: ArchonOptionsV1Module,
8889
archon_properties_v1: ArchonPropertiesV1Module,
8990
archon_server_users_v1: ArchonServerUsersV1Module,
91+
archon_servers_internal: ArchonServersInternalModule,
9092
archon_servers_v0: ArchonServersV0Module,
9193
archon_servers_v1: ArchonServersV1Module,
9294
archon_transfers_internal: ArchonTransfersInternalModule,

0 commit comments

Comments
 (0)