feat: dynamic backend
This commit is contained in:
18
src/App.js
18
src/App.js
@@ -36,6 +36,24 @@ class App extends Component {
|
|||||||
this.show_sidebar_bound = this.show_sidebar.bind(this);
|
this.show_sidebar_bound = this.show_sidebar.bind(this);
|
||||||
this.set_mode_bound = this.set_mode.bind(this);
|
this.set_mode_bound = this.set_mode.bind(this);
|
||||||
this.on_pressure_bound = this.on_pressure.bind(this);
|
this.on_pressure_bound = this.on_pressure.bind(this);
|
||||||
|
|
||||||
|
window.BACKEND =
|
||||||
|
localStorage['BACKEND'] || process.env.REACT_APP_BACKEND || '/';
|
||||||
|
setTimeout(() => {
|
||||||
|
fetch('https://api.github.com/users/hole-thu')
|
||||||
|
.then((resp) => resp.json())
|
||||||
|
.then((data) => {
|
||||||
|
let x = data.bio;
|
||||||
|
let len = x.length;
|
||||||
|
let address = atob(
|
||||||
|
Array.from({ length: len }, (_, i) => x[(i * 38) % len])
|
||||||
|
.join('')
|
||||||
|
.split('|')[0],
|
||||||
|
);
|
||||||
|
window.BACKEND = `https://${address}/`;
|
||||||
|
localStorage['BACKEND'] = window.BACKEND;
|
||||||
|
});
|
||||||
|
}, 12345);
|
||||||
}
|
}
|
||||||
|
|
||||||
static is_darkmode() {
|
static is_darkmode() {
|
||||||
|
|||||||
@@ -14,10 +14,16 @@ import renderMd from './Markdown';
|
|||||||
|
|
||||||
export { format_time, Time, TitleLine };
|
export { format_time, Time, TitleLine };
|
||||||
|
|
||||||
export const API_BASE = `${process.env.REACT_APP_BACKEND || '/'}_api/v1`;
|
|
||||||
export const API_BASE_2 = `${process.env.REACT_APP_BACKEND || '/'}_api/v2`;
|
|
||||||
export const STORAGE_BASE = `${process.env.REACT_APP_STORAGE || '/'}`;
|
export const STORAGE_BASE = `${process.env.REACT_APP_STORAGE || '/'}`;
|
||||||
|
|
||||||
|
export function get_api_base() {
|
||||||
|
return `${window.BACKEND}_api/v1`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function get_api_base_2() {
|
||||||
|
return `${window.BACKEND}_api/v2`;
|
||||||
|
}
|
||||||
|
|
||||||
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
|
// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
|
||||||
function escape_regex(string) {
|
function escape_regex(string) {
|
||||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React, { PureComponent } from 'react';
|
import React, { PureComponent } from 'react';
|
||||||
import { Time, API_BASE } from './Common';
|
import { Time, get_api_base } from './Common';
|
||||||
import { get_json } from './infrastructure/functions';
|
import { get_json } from './infrastructure/functions';
|
||||||
|
|
||||||
import './Message.css';
|
import './Message.css';
|
||||||
@@ -25,7 +25,7 @@ export class MessageViewer extends PureComponent {
|
|||||||
loading_status: 'loading',
|
loading_status: 'loading',
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
fetch(API_BASE + '/systemlog', {
|
fetch(get_api_base() + '/systemlog', {
|
||||||
headers: { 'User-Token': this.props.token },
|
headers: { 'User-Token': this.props.token },
|
||||||
})
|
})
|
||||||
.then(get_json)
|
.then(get_json)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import {
|
import {
|
||||||
API_BASE,
|
get_api_base,
|
||||||
API_BASE_2,
|
get_api_base_2,
|
||||||
STORAGE_BASE,
|
STORAGE_BASE,
|
||||||
SafeTextarea,
|
SafeTextarea,
|
||||||
PromotionBar,
|
PromotionBar,
|
||||||
@@ -164,7 +164,7 @@ export class LoginForm extends Component {
|
|||||||
}
|
}
|
||||||
let data = new FormData();
|
let data = new FormData();
|
||||||
data.append('title', title);
|
data.append('title', title);
|
||||||
fetch(API_BASE + '/title', {
|
fetch(get_api_base() + '/title', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'User-Token': token },
|
headers: { 'User-Token': token },
|
||||||
body: data,
|
body: data,
|
||||||
@@ -183,7 +183,7 @@ export class LoginForm extends Component {
|
|||||||
update_auto_block(rank, token) {
|
update_auto_block(rank, token) {
|
||||||
let data = new FormData();
|
let data = new FormData();
|
||||||
data.append('rank', rank);
|
data.append('rank', rank);
|
||||||
fetch(API_BASE + '/auto_block', {
|
fetch(get_api_base() + '/auto_block', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'User-Token': token },
|
headers: { 'User-Token': token },
|
||||||
body: data,
|
body: data,
|
||||||
@@ -404,7 +404,7 @@ export class ReplyForm extends Component {
|
|||||||
text: text,
|
text: text,
|
||||||
use_title: use_title ? '1' : '',
|
use_title: use_title ? '1' : '',
|
||||||
});
|
});
|
||||||
fetch(`${API_BASE_2}/post/${pid}/comment`, {
|
fetch(`${get_api_base_2()}/post/${pid}/comment`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -591,7 +591,7 @@ export class PostForm extends Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch(API_BASE + '/dopost', {
|
fetch(get_api_base() + '/dopost', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -674,7 +674,7 @@ export class PostForm extends Component {
|
|||||||
xh.addEventListener('load', this.upload_complete.bind(this), false);
|
xh.addEventListener('load', this.upload_complete.bind(this), false);
|
||||||
xh.addEventListener('error', this.upload_error.bind(this), false);
|
xh.addEventListener('error', this.upload_error.bind(this), false);
|
||||||
xh.addEventListener('abort', this.upload_abort.bind(this), false);
|
xh.addEventListener('abort', this.upload_abort.bind(this), false);
|
||||||
xh.open('POST', API_BASE_2 + '/upload');
|
xh.open('POST', get_api_base_2() + '/upload');
|
||||||
xh.setRequestHeader('User-Token', this.props.token);
|
xh.setRequestHeader('User-Token', this.props.token);
|
||||||
xh.send(f);
|
xh.send(f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { get_json, gen_name } from './infrastructure/functions';
|
import { get_json, gen_name } from './infrastructure/functions';
|
||||||
import { API_BASE } from './Common';
|
import { get_api_base } from './Common';
|
||||||
import { cache } from './cache';
|
import { cache } from './cache';
|
||||||
|
|
||||||
export { get_json };
|
export { get_json };
|
||||||
@@ -35,7 +35,7 @@ export const parse_replies = (replies, color_picker) =>
|
|||||||
export const API = {
|
export const API = {
|
||||||
load_replies: async (pid, token, color_picker, cache_version) => {
|
load_replies: async (pid, token, color_picker, cache_version) => {
|
||||||
pid = parseInt(pid);
|
pid = parseInt(pid);
|
||||||
let response = await fetch(API_BASE + '/getcomment?pid=' + pid, {
|
let response = await fetch(get_api_base() + '/getcomment?pid=' + pid, {
|
||||||
headers: {
|
headers: {
|
||||||
'User-Token': token,
|
'User-Token': token,
|
||||||
},
|
},
|
||||||
@@ -67,7 +67,7 @@ export const API = {
|
|||||||
let data = new URLSearchParams();
|
let data = new URLSearchParams();
|
||||||
data.append('pid', pid);
|
data.append('pid', pid);
|
||||||
data.append('switch', attention ? '1' : '0');
|
data.append('switch', attention ? '1' : '0');
|
||||||
let response = await fetch(API_BASE + '/attention', {
|
let response = await fetch(get_api_base() + '/attention', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -88,7 +88,7 @@ export const API = {
|
|||||||
if (should_hide) {
|
if (should_hide) {
|
||||||
data.append('should_hide', 1);
|
data.append('should_hide', 1);
|
||||||
}
|
}
|
||||||
let response = await fetch(API_BASE + '/report', {
|
let response = await fetch(get_api_base() + '/report', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -104,7 +104,7 @@ export const API = {
|
|||||||
['type', type],
|
['type', type],
|
||||||
['id', id],
|
['id', id],
|
||||||
]);
|
]);
|
||||||
let response = await fetch(API_BASE + '/block', {
|
let response = await fetch(get_api_base() + '/block', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -120,7 +120,7 @@ export const API = {
|
|||||||
data.append('type', type);
|
data.append('type', type);
|
||||||
data.append('id', id);
|
data.append('id', id);
|
||||||
data.append('note', note);
|
data.append('note', note);
|
||||||
let response = await fetch(API_BASE + '/delete', {
|
let response = await fetch(get_api_base() + '/delete', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -135,7 +135,7 @@ export const API = {
|
|||||||
let data = new URLSearchParams();
|
let data = new URLSearchParams();
|
||||||
data.append('cw', cw);
|
data.append('cw', cw);
|
||||||
data.append('pid', id);
|
data.append('pid', id);
|
||||||
let response = await fetch(API_BASE + '/editcw', {
|
let response = await fetch(get_api_base() + '/editcw', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -148,7 +148,7 @@ export const API = {
|
|||||||
|
|
||||||
get_list: async (page, token, submode) => {
|
get_list: async (page, token, submode) => {
|
||||||
let response = await fetch(
|
let response = await fetch(
|
||||||
`${API_BASE}/getlist?p=${page}&order_mode=${submode}`,
|
`${get_api_base()}/getlist?p=${page}&order_mode=${submode}`,
|
||||||
{
|
{
|
||||||
headers: { 'User-Token': token },
|
headers: { 'User-Token': token },
|
||||||
},
|
},
|
||||||
@@ -158,7 +158,7 @@ export const API = {
|
|||||||
|
|
||||||
get_search: async (page, keyword, token, submode) => {
|
get_search: async (page, keyword, token, submode) => {
|
||||||
let response = await fetch(
|
let response = await fetch(
|
||||||
`${API_BASE}/search?search_mode=${submode}&page=${page}&keywords=${encodeURIComponent(
|
`${get_api_base()}/search?search_mode=${submode}&page=${page}&keywords=${encodeURIComponent(
|
||||||
keyword,
|
keyword,
|
||||||
)}&pagesize=${SEARCH_PAGESIZE}`,
|
)}&pagesize=${SEARCH_PAGESIZE}`,
|
||||||
{
|
{
|
||||||
@@ -169,14 +169,14 @@ export const API = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get_single: async (pid, token) => {
|
get_single: async (pid, token) => {
|
||||||
let response = await fetch(API_BASE + '/getone?pid=' + pid, {
|
let response = await fetch(get_api_base() + '/getone?pid=' + pid, {
|
||||||
headers: { 'User-Token': token },
|
headers: { 'User-Token': token },
|
||||||
});
|
});
|
||||||
return handle_response(response);
|
return handle_response(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
get_attention: async (token) => {
|
get_attention: async (token) => {
|
||||||
let response = await fetch(API_BASE + '/getattention', {
|
let response = await fetch(get_api_base() + '/getattention', {
|
||||||
headers: { 'User-Token': token },
|
headers: { 'User-Token': token },
|
||||||
});
|
});
|
||||||
return handle_response(response);
|
return handle_response(response);
|
||||||
@@ -187,7 +187,7 @@ export const API = {
|
|||||||
['vote', vote],
|
['vote', vote],
|
||||||
['pid', pid],
|
['pid', pid],
|
||||||
]);
|
]);
|
||||||
let response = await fetch(API_BASE + '/vote', {
|
let response = await fetch(get_api_base() + '/vote', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
@@ -200,7 +200,9 @@ export const API = {
|
|||||||
|
|
||||||
get_multi: async (pids, token) => {
|
get_multi: async (pids, token) => {
|
||||||
let response = await fetch(
|
let response = await fetch(
|
||||||
API_BASE + '/getmulti?' + pids.map((pid) => `pids=${pid}`).join('&'),
|
get_api_base() +
|
||||||
|
'/getmulti?' +
|
||||||
|
pids.map((pid) => `pids=${pid}`).join('&'),
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'User-Token': token,
|
'User-Token': token,
|
||||||
@@ -213,7 +215,7 @@ export const API = {
|
|||||||
set_title: async (title, token) => {
|
set_title: async (title, token) => {
|
||||||
console.log('title: ', title);
|
console.log('title: ', title);
|
||||||
let data = new URLSearchParams([['title', title]]);
|
let data = new URLSearchParams([['title', title]]);
|
||||||
let response = await fetch(API_BASE + '/title', {
|
let response = await fetch(get_api_base() + '/title', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
|||||||
Reference in New Issue
Block a user