Cosmos-Server/client/src/api/index.jsx

249 lines
6.6 KiB
React
Raw Normal View History

2023-05-01 17:33:58 +00:00
import * as _auth from './authentication';
import * as _users from './users';
import * as _config from './config';
import * as _docker from './docker';
2023-06-09 18:48:31 +00:00
import * as _market from './market';
2023-08-20 10:36:52 +00:00
import * as _constellation from './constellation';
2023-10-26 14:40:07 +00:00
import * as _metrics from './metrics';
2023-05-01 17:33:58 +00:00
import * as authDemo from './authentication.demo';
import * as usersDemo from './users.demo';
import * as configDemo from './config.demo';
import * as dockerDemo from './docker.demo';
import * as indexDemo from './index.demo';
2023-06-09 18:48:31 +00:00
import * as marketDemo from './market.demo';
2023-10-07 12:24:22 +00:00
import * as constellationDemo from './constellation.demo';
2023-03-29 20:38:50 +00:00
import wrap from './wrap';
2023-07-28 09:28:15 +00:00
import { redirectToLocal } from '../utils/indexs';
2023-03-29 20:38:50 +00:00
2023-06-17 17:08:26 +00:00
export let CPU_ARCH = 'amd64';
2023-06-15 12:07:10 +00:00
export let CPU_AVX = true;
2023-06-20 17:34:06 +00:00
export let HOME_BACKGROUND;
export let PRIMARY_COLOR;
export let SECONDARY_COLOR;
export let FIRST_LOAD = false;
let getStatus = (initial) => {
2023-03-29 20:38:50 +00:00
return wrap(fetch('/cosmos/api/status', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
2023-06-20 17:34:06 +00:00
}), initial)
2023-06-15 12:07:10 +00:00
.then(async (response) => {
CPU_ARCH = response.data.CPU;
CPU_AVX = response.data.AVX;
2023-06-20 17:34:06 +00:00
HOME_BACKGROUND = response.data.homepage.Background;
PRIMARY_COLOR = response.data.theme.PrimaryColor;
SECONDARY_COLOR = response.data.theme.SecondaryColor;
FIRST_LOAD = true;
2023-06-15 12:07:10 +00:00
return response
2023-06-20 17:34:06 +00:00
}).catch((response) => {
const urlSearch = encodeURIComponent(window.location.search);
2023-07-28 09:28:15 +00:00
const redirectToURL = (window.location.pathname + urlSearch);
2023-06-20 17:34:06 +00:00
if(response.status != 'OK') {
if(
window.location.href.indexOf('/cosmos-ui/newInstall') == -1 &&
window.location.href.indexOf('/cosmos-ui/login') == -1 &&
window.location.href.indexOf('/cosmos-ui/loginmfa') == -1 &&
window.location.href.indexOf('/cosmos-ui/newmfa') == -1 &&
window.location.href.indexOf('/cosmos-ui/register') == -1 &&
window.location.href.indexOf('/cosmos-ui/forgot-password') == -1) {
if(response.status == 'NEW_INSTALL') {
2023-07-28 09:28:15 +00:00
redirectToLocal('/cosmos-ui/newInstall');
2023-06-20 17:34:06 +00:00
} else if (response.status == 'error' && response.code == "HTTP004") {
2023-07-28 09:28:15 +00:00
redirectToLocal('/cosmos-ui/login?redirect=' + redirectToURL);
2023-06-20 17:34:06 +00:00
} else if (response.status == 'error' && response.code == "HTTP006") {
2023-07-28 09:28:15 +00:00
redirectToLocal('/cosmos-ui/loginmfa?redirect=' + redirectToURL);
2023-06-20 17:34:06 +00:00
} else if (response.status == 'error' && response.code == "HTTP007") {
2023-07-28 09:28:15 +00:00
redirectToLocal('/cosmos-ui/newmfa?redirect=' + redirectToURL);
2023-06-20 17:34:06 +00:00
}
} else {
return "nothing";
}
}
2023-07-02 14:28:14 +00:00
return "NOT_AVAILABLE";
2023-06-15 12:07:10 +00:00
});
2023-03-29 20:38:50 +00:00
}
2023-05-01 17:33:58 +00:00
let isOnline = () => {
2023-04-28 18:28:01 +00:00
return fetch('/cosmos/api/status', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
throw e;
});
}
2023-05-19 15:23:05 +00:00
let newInstall = (req, onProgress) => {
2023-06-20 00:04:25 +00:00
if(req.step == '2') {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req)
};
return fetch('/cosmos/api/newInstall', requestOptions)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
// The response body is a ReadableStream. This code reads the stream and passes chunks to the callback.
const reader = response.body.getReader();
// Read the stream and pass chunks to the callback as they arrive
return new ReadableStream({
start(controller) {
function read() {
return reader.read().then(({ done, value }) => {
if (done) {
controller.close();
return;
}
// Decode the UTF-8 text
let text = new TextDecoder().decode(value);
// Split by lines in case there are multiple lines in one chunk
let lines = text.split('\n');
for (let line of lines) {
if (line) {
// Call the progress callback
onProgress(line);
}
}
controller.enqueue(value);
return read();
});
}
return read();
}
});
}).catch((e) => {
console.error(e);
});
} else {
return wrap(fetch('/cosmos/api/newInstall', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req)
}))
}
2023-03-29 20:38:50 +00:00
}
2023-06-09 18:48:31 +00:00
let checkHost = (host) => {
2023-06-04 14:41:26 +00:00
return fetch('/cosmos/api/dns-check?url=' + host, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
e.message = rep.message;
throw e;
});
}
2023-06-09 18:48:31 +00:00
let getDNS = (host) => {
2023-06-04 14:41:26 +00:00
return fetch('/cosmos/api/dns?url=' + host, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
}).then(async (response) => {
let rep;
try {
rep = await response.json();
} catch {
throw new Error('Server error');
}
if (response.status == 200) {
return rep;
}
const e = new Error(rep.message);
e.status = response.status;
e.message = rep.message;
throw e;
});
}
2023-06-20 17:34:06 +00:00
let uploadBackground = (file) => {
const formData = new FormData();
formData.append('background', file);
return wrap(fetch('/cosmos/api/background', {
method: 'POST',
body: formData
}));
};
2023-05-01 17:33:58 +00:00
const isDemo = import.meta.env.MODE === 'demo';
let auth = _auth;
let users = _users;
let config = _config;
let docker = _docker;
2023-06-09 18:48:31 +00:00
let market = _market;
2023-08-20 10:36:52 +00:00
let constellation = _constellation;
2023-10-26 14:40:07 +00:00
let metrics = _metrics;
2023-05-01 17:33:58 +00:00
if(isDemo) {
auth = authDemo;
users = usersDemo;
config = configDemo;
docker = dockerDemo;
2023-06-09 18:48:31 +00:00
market = marketDemo;
2023-05-01 17:33:58 +00:00
getStatus = indexDemo.getStatus;
newInstall = indexDemo.newInstall;
isOnline = indexDemo.isOnline;
2023-06-09 18:48:31 +00:00
checkHost = indexDemo.checkHost;
getDNS = indexDemo.getDNS;
2023-06-20 17:34:06 +00:00
uploadBackground = indexDemo.uploadBackground;
2023-10-07 12:24:22 +00:00
constellation = constellationDemo;
2023-05-01 17:33:58 +00:00
}
2023-03-12 18:17:28 +00:00
export {
2023-03-13 00:49:27 +00:00
auth,
2023-03-16 18:56:36 +00:00
users,
2023-03-25 20:15:00 +00:00
config,
2023-03-29 20:38:50 +00:00
docker,
2023-06-09 18:48:31 +00:00
market,
2023-08-20 10:36:52 +00:00
constellation,
2023-03-29 20:38:50 +00:00
getStatus,
newInstall,
2023-06-04 14:41:26 +00:00
isOnline,
checkHost,
2023-06-20 17:34:06 +00:00
getDNS,
2023-10-26 14:40:07 +00:00
metrics,
2023-06-20 17:34:06 +00:00
uploadBackground
2023-03-12 18:17:28 +00:00
};