mirror of
https://github.com/unraid/api.git
synced 2026-01-07 00:59:48 -06:00
fix: return local server if no my_server key exists
This commit is contained in:
4
app/cache/user.ts
vendored
4
app/cache/user.ts
vendored
@@ -18,10 +18,10 @@ export interface CachedServer {
|
||||
apikey: string;
|
||||
name: string;
|
||||
status: Status;
|
||||
wanip: IpAddress;
|
||||
wanip: IpAddress | null;
|
||||
lanip: IpAddress;
|
||||
localurl: URL;
|
||||
remoteurl: string;
|
||||
remoteurl: string | null;
|
||||
}
|
||||
|
||||
export interface CachedServers {
|
||||
|
||||
@@ -48,53 +48,7 @@ type makeNullUndefinedAndOptional<T> = {
|
||||
|
||||
type Server = makeNullUndefinedAndOptional<CachedServer>;
|
||||
|
||||
export const getServers = async (): Promise<Server[]> => {
|
||||
// Check if we have the servers already cached, if so return them
|
||||
const cachedServers = userCache.get<CachedServers>('mine')?.servers;
|
||||
if (cachedServers) {
|
||||
return cachedServers;
|
||||
}
|
||||
|
||||
// For now use the my_servers key
|
||||
// Later we should return the correct one for the current user with the correct scope, etc.
|
||||
const apiKey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString()!;
|
||||
|
||||
// No cached servers found
|
||||
if (!cachedServers) {
|
||||
// Fetch servers from mothership
|
||||
const servers = await getUserServers(apiKey);
|
||||
|
||||
graphqlLogger.debug('Using upstream for /servers endpoint');
|
||||
|
||||
// No servers found
|
||||
if (!servers || servers.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Cache servers
|
||||
userCache.set<CachedServers>('mine', {
|
||||
servers
|
||||
});
|
||||
|
||||
// Get first server's owner object
|
||||
const owner = servers[0].owner;
|
||||
|
||||
try {
|
||||
throw new Error('THIS_HERE');
|
||||
} catch (error: unknown) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
// Publish owner event
|
||||
await pubsub.publish('owner', {
|
||||
owner
|
||||
});
|
||||
|
||||
// Return servers from mothership
|
||||
return servers;
|
||||
}
|
||||
|
||||
graphqlLogger.debug('Falling back to local state for /servers endpoint');
|
||||
const getLocalServer = (apiKey: string): [CachedServer] => {
|
||||
const guid = varState?.data?.regGuid;
|
||||
const name = varState?.data?.name;
|
||||
const wanip = null;
|
||||
@@ -119,3 +73,52 @@ export const getServers = async (): Promise<Server[]> => {
|
||||
remoteurl
|
||||
}];
|
||||
};
|
||||
|
||||
export const getServers = async (): Promise<Server[]> => {
|
||||
// Check if we have the servers already cached, if so return them
|
||||
const cachedServers = userCache.get<CachedServers>('mine')?.servers;
|
||||
if (cachedServers) {
|
||||
return cachedServers;
|
||||
}
|
||||
|
||||
// For now use the my_servers key
|
||||
// Later we should return the correct one for the current user with the correct scope, etc.
|
||||
const apiKey = apiManager.getValidKeys().find(key => key.name === 'my_servers')?.key.toString()!;
|
||||
|
||||
// Return only current server if we have no key
|
||||
if (!apiKey) {
|
||||
return getLocalServer(apiKey);
|
||||
}
|
||||
|
||||
// No cached servers found
|
||||
if (!cachedServers) {
|
||||
// Fetch servers from mothership
|
||||
const servers = await getUserServers(apiKey);
|
||||
|
||||
graphqlLogger.debug('Using upstream for /servers endpoint');
|
||||
|
||||
// No servers found
|
||||
if (!servers || servers.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Cache servers
|
||||
userCache.set<CachedServers>('mine', {
|
||||
servers
|
||||
});
|
||||
|
||||
// Get first server's owner object
|
||||
const owner = servers[0].owner;
|
||||
|
||||
// Publish owner event
|
||||
await pubsub.publish('owner', {
|
||||
owner
|
||||
});
|
||||
|
||||
// Return servers from mothership
|
||||
return servers;
|
||||
}
|
||||
|
||||
graphqlLogger.debug('Falling back to local state for /servers endpoint');
|
||||
return getLocalServer(apiKey);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user