added post redirects

This commit is contained in:
mrfry 2023-04-02 13:20:15 +02:00
parent 182f003a65
commit 0f6b4050df
3 changed files with 50 additions and 7 deletions

View file

@ -103,6 +103,7 @@ interface SyncDataRes {
}
}
// FIXME: to utils/http.ts
function get<T>(options: http.RequestOptions): Promise<RequestResult<T>> {
return new Promise((resolve) => {
const req = http.get(options, function (res) {
@ -126,7 +127,27 @@ function get<T>(options: http.RequestOptions): Promise<RequestResult<T>> {
})
}
function peerToString(peer: PeerInfo) {
function updateThirdPartyPeers(
newVal: Omit<PeerInfo, 'publicKey' | 'name' | 'contact'>[]
) {
const prevVal = utils.ReadJSON<PeerInfo[]>(paths.thirdPartyPeersFile)
const dataToWrite = newVal.reduce((acc, peer) => {
const isIncluded = acc.find((x) => {
return peerToString(x) === peerToString(peer)
})
if (!isIncluded) {
return [...acc, peer]
}
return acc
}, prevVal)
utils.WriteFile(
JSON.stringify(dataToWrite, null, 2),
paths.thirdPartyPeersFile
)
}
function peerToString(peer: { host: string; port: string | number }) {
return `${peer.host}:${peer.port}`
}
@ -655,10 +676,7 @@ function setup(data: SubmoduleData): Submodule {
)
})
if (thirdPartyPeers.length > 0) {
utils.WriteFile(
JSON.stringify(thirdPartyPeers, null, 2),
paths.thirdPartyPeersFile
)
updateThirdPartyPeers(thirdPartyPeers)
logger.Log(
`\tPeers reported ${logger.C('green')}${
thirdPartyPeers.length
@ -899,7 +917,6 @@ function setup(data: SubmoduleData): Submodule {
// TODO: get all user files
app.get('/getnewdatasince', (req: Request, res: Response<SyncDataRes>) => {
// TODO: add to third party peers, and recieve self info
// FIXME: hash question db to see if different?
// it could help in determining if it should be checked for new data, but it would only save
// a getNewDataSince() call per question db
@ -968,6 +985,16 @@ function setup(data: SubmoduleData): Submodule {
'use it as a peer.',
'yellowbg'
)
if (remoteHost.includes(':')) {
const [host, port] = remoteHost.split(':')
updateThirdPartyPeers([
{
host: host,
port: +port,
},
])
logger.Log('Host info written to host info file')
}
}
}

View file

@ -186,6 +186,11 @@ function GetApp(): ModuleType {
from: '/patreon',
to: links.patreon,
},
// -----------------------------------
{
from: '/hasNewMsgs',
to: 'api/hasNewMsgs',
},
]
simpleRedirects.forEach((redirect) => {
@ -208,6 +213,17 @@ function GetApp(): ModuleType {
})
})
const postRedirects = [
{ from: '/ask', to: '/api/ask' },
{ from: '/isAdding', to: '/api/isAdding' },
]
postRedirects.forEach((redirect) => {
app.post(redirect.from, function (_req: Request, res) {
res.redirect(307, redirect.to)
})
})
// --------------------------------------------------------------
function AddHtmlRoutes(files: string[]) {

@ -1 +1 @@
Subproject commit b9abbbf03c111ea5b2534681d7b11de8f8a79baa
Subproject commit 39dfd7a0f48d850b543d38d607320236038f54fb